From b9caf75d5390b2f898fee558dec9e13d975036d0 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Tue, 24 May 2011 14:41:09 +0200 Subject: [PATCH] put Document and DocumentResource into the Polecat namespace --- lib/polecat/document.rb | 93 ++++++++++--------- lib/polecat/document_resource.rb | 60 ++++++------ spec/document/attributes_spec.rb | 4 +- .../document/document_ressource/field_spec.rb | 4 +- spec/document/new_spec.rb | 2 +- spec/spec_helper.rb | 2 +- 6 files changed, 85 insertions(+), 80 deletions(-) diff --git a/lib/polecat/document.rb b/lib/polecat/document.rb index a1001b5..ffc62eb 100644 --- a/lib/polecat/document.rb +++ b/lib/polecat/document.rb @@ -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 diff --git a/lib/polecat/document_resource.rb b/lib/polecat/document_resource.rb index f61c710..ddc395a 100644 --- a/lib/polecat/document_resource.rb +++ b/lib/polecat/document_resource.rb @@ -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 diff --git a/spec/document/attributes_spec.rb b/spec/document/attributes_spec.rb index eb0f44c..9dcf9f7 100644 --- a/spec/document/attributes_spec.rb +++ b/spec/document/attributes_spec.rb @@ -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 diff --git a/spec/document/document_ressource/field_spec.rb b/spec/document/document_ressource/field_spec.rb index 197bbf9..d74cfc5 100644 --- a/spec/document/document_ressource/field_spec.rb +++ b/spec/document/document_ressource/field_spec.rb @@ -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 diff --git a/spec/document/new_spec.rb b/spec/document/new_spec.rb index de505b9..5c14e2e 100644 --- a/spec/document/new_spec.rb +++ b/spec/document/new_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c02fbdd..5759ba8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -27,7 +27,7 @@ end module Spec class FooDocument - include Document + include Polecat::Document field :id field :name, :lazy => true