delete the old document files, as they are not needed anymore
This commit is contained in:
parent
2c345cd877
commit
c4f5831e64
|
@ -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
|
|
|
@ -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
|
|
Loading…
Reference in New Issue