put Document and DocumentResource into the Polecat namespace
This commit is contained in:
parent
b11090d145
commit
b9caf75d53
|
@ -1,54 +1,57 @@
|
|||
require 'polecat/document_resource'
|
||||
|
||||
module Document
|
||||
OPTIONS = {
|
||||
:index => true,
|
||||
:lazy => false,
|
||||
:value => nil
|
||||
}
|
||||
class Polecat
|
||||
module Document
|
||||
OPTIONS = {
|
||||
:index => true,
|
||||
:lazy => false,
|
||||
:value => nil
|
||||
}
|
||||
|
||||
def self.included klass # :nodoc:
|
||||
klass.extend(DocumentResource)
|
||||
klass.instance_variable_set :@attributes, {}
|
||||
end
|
||||
|
||||
# creates a new document
|
||||
#
|
||||
# It is possible to create a new document with a hash, which has all values
|
||||
# of the fields.
|
||||
# Example:
|
||||
# class Foo
|
||||
# include Polecat::Document
|
||||
#
|
||||
# field :id
|
||||
# field :description
|
||||
# end
|
||||
# f = Foo.new :id => 1, :description => 'foo'
|
||||
def initialize fields = {}
|
||||
fields.each do |key, value|
|
||||
attribute_set key, value
|
||||
def self.included klass # :nodoc:
|
||||
klass.extend(DocumentResource)
|
||||
klass.instance_variable_set :@attributes, {}
|
||||
end
|
||||
end
|
||||
|
||||
# get an attribute of the document
|
||||
def attribute_get name
|
||||
attributes[name.to_sym][:value]
|
||||
end
|
||||
|
||||
# set an attribute of the document
|
||||
def attribute_set name, value
|
||||
name = name.to_sym
|
||||
att = attributes
|
||||
if att.has_key? name
|
||||
att[name][:value] = value
|
||||
else
|
||||
raise ArgumentError, "attribute #{name} does not exist"
|
||||
# creates a new document
|
||||
#
|
||||
# It is possible to create a new document with a hash, which has all values
|
||||
# of the fields.
|
||||
# Example:
|
||||
# class Foo
|
||||
# include Polecat::Document
|
||||
#
|
||||
# field :id
|
||||
# field :description
|
||||
# end
|
||||
# f = Foo.new :id => 1, :description => 'foo'
|
||||
def initialize fields = {}
|
||||
fields.each do |key, value|
|
||||
attribute_set key, value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def attributes
|
||||
return @attributes if @attributes
|
||||
@attributes = Marshal.load(Marshal.dump(
|
||||
self.class.instance_variable_get :@attributes))
|
||||
# get an attribute of the document
|
||||
def attribute_get name
|
||||
attributes[name.to_sym][:value]
|
||||
end
|
||||
|
||||
# set an attribute of the document
|
||||
def attribute_set name, value
|
||||
name = name.to_sym
|
||||
att = attributes
|
||||
if att.has_key? name
|
||||
att[name][:value] = value
|
||||
else
|
||||
raise ArgumentError, "attribute #{name} does not exist"
|
||||
end
|
||||
end
|
||||
|
||||
# get all attributes
|
||||
def attributes
|
||||
return @attributes if @attributes
|
||||
@attributes = Marshal.load(Marshal.dump(
|
||||
self.class.instance_variable_get :@attributes))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,35 +1,37 @@
|
|||
module DocumentResource
|
||||
def field name, options = {}
|
||||
attributes = self.instance_variable_get :@attributes
|
||||
attributes[name.to_sym] = Document::OPTIONS.merge(options)
|
||||
class Polecat
|
||||
module DocumentResource
|
||||
def field name, options = {}
|
||||
attributes = self.instance_variable_get :@attributes
|
||||
attributes[name.to_sym] = Document::OPTIONS.merge(options)
|
||||
|
||||
create_reader_for name
|
||||
create_writer_for name
|
||||
end
|
||||
|
||||
def mod
|
||||
if !@mod
|
||||
@mod = Module.new
|
||||
self.class_eval do
|
||||
include @mod
|
||||
end
|
||||
create_reader_for name
|
||||
create_writer_for name
|
||||
end
|
||||
@mod
|
||||
end
|
||||
|
||||
def create_reader_for name
|
||||
mod.module_eval <<-RUBYCODE
|
||||
def #{name.to_s}
|
||||
attribute_get :#{name}
|
||||
def mod
|
||||
if !@mod
|
||||
@mod = Module.new
|
||||
self.class_eval do
|
||||
include @mod
|
||||
end
|
||||
end
|
||||
RUBYCODE
|
||||
end
|
||||
|
||||
def create_writer_for name
|
||||
mod.module_eval <<-RUBYCODE
|
||||
def #{name.to_s}= o
|
||||
attribute_set :#{name}, o
|
||||
end
|
||||
RUBYCODE
|
||||
@mod
|
||||
end
|
||||
|
||||
def create_reader_for name
|
||||
mod.module_eval <<-RUBYCODE
|
||||
def #{name.to_s}
|
||||
attribute_get :#{name}
|
||||
end
|
||||
RUBYCODE
|
||||
end
|
||||
|
||||
def create_writer_for name
|
||||
mod.module_eval <<-RUBYCODE
|
||||
def #{name.to_s}= o
|
||||
attribute_set :#{name}, o
|
||||
end
|
||||
RUBYCODE
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,11 +3,11 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
|||
describe "Document#attributes" do
|
||||
it "returns a hash with all attributes" do
|
||||
class FooDoc
|
||||
include Document
|
||||
include Polecat::Document
|
||||
|
||||
field :bar
|
||||
end
|
||||
d = FooDoc.new
|
||||
d.attributes.should == {:bar => Document::OPTIONS}
|
||||
d.attributes.should == {:bar => Polecat::Document::OPTIONS}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
|||
describe "DocumentRessource#field" do
|
||||
it "creates a method to get an attribute value" do
|
||||
class FooDoc
|
||||
include Document
|
||||
include Polecat::Document
|
||||
field :doc
|
||||
end
|
||||
FooDoc.new.respond_to?(:doc).should == true
|
||||
|
@ -11,7 +11,7 @@ describe "DocumentRessource#field" do
|
|||
|
||||
it "creates a method to set an attribute" do
|
||||
class FooDoc
|
||||
include Document
|
||||
include Polecat::Document
|
||||
field :doc
|
||||
end
|
||||
d = FooDoc.new
|
||||
|
|
|
@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
|||
describe "Document#initialize" do
|
||||
it "creates a new Document object" do
|
||||
d = Spec::FooDocument.new
|
||||
d.is_a?(Document).should == true
|
||||
d.is_a?(Polecat::Document).should == true
|
||||
end
|
||||
|
||||
it "takes a hash as argument" do
|
||||
|
|
|
@ -27,7 +27,7 @@ end
|
|||
|
||||
module Spec
|
||||
class FooDocument
|
||||
include Document
|
||||
include Polecat::Document
|
||||
|
||||
field :id
|
||||
field :name, :lazy => true
|
||||
|
|
Loading…
Reference in New Issue