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