diff --git a/lib/polecat/document.rb b/lib/polecat/document.rb deleted file mode 100644 index 91fb21e..0000000 --- a/lib/polecat/document.rb +++ /dev/null @@ -1,61 +0,0 @@ -require 'polecat/document_resource' - -class Polecat - module Document - OPTIONS = { - :index => true, - :lazy => false, - :value => nil - } - - # include the document - # - # This includes the document into the target class. - # @private - 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 initializing a document - # 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 - - # 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 deleted file mode 100644 index ddc395a..0000000 --- a/lib/polecat/document_resource.rb +++ /dev/null @@ -1,37 +0,0 @@ -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 - end - @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