Module: Polecat::Document

Defined in:
lib/polecat/document.rb

Constant Summary collapse

OPTIONS =
{
  :index => true,
  :lazy  => false,
  :value => nil
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

include the document

This includes the document into the target class.



15
16
17
18
# File 'lib/polecat/document.rb', line 15

def self.included klass #:nodoc:
  klass.extend(DocumentResource)
  klass.instance_variable_set :@attributes, {}
end

Instance Method Details

#attribute_get(name) ⇒ Object

get an attribute of the document



39
40
41
# File 'lib/polecat/document.rb', line 39

def attribute_get name
  attributes[name.to_sym][:value]
end

#attribute_set(name, value) ⇒ Object

set an attribute of the document



44
45
46
47
48
49
50
51
52
# File 'lib/polecat/document.rb', line 44

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

#attributesObject

get all attributes



55
56
57
58
59
# File 'lib/polecat/document.rb', line 55

def attributes
  return @attributes if @attributes
  @attributes = Marshal.load(Marshal.dump(
    self.class.instance_variable_get :@attributes))
end

#initialize(fields = {}) ⇒ Object

creates a new document

It is possible to create a new document with a hash, which has all values of the fields.

Examples:

initializing a document

class Foo
  include Polecat::Document

  field :id
  field :description
end
f = Foo.new :id => 1, :description => 'foo'


32
33
34
35
36
# File 'lib/polecat/document.rb', line 32

def initialize fields = {}
  fields.each do |key, value|
    attribute_set key, value
  end
end