Module: Zeng::Document::InstanceMethods

Defined in:
lib/zeng/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



72
73
74
# File 'lib/zeng/document.rb', line 72

def id
  @id
end

Instance Method Details

#assigned_attributesObject



101
102
103
# File 'lib/zeng/document.rb', line 101

def assigned_attributes
  self.class.assigned_attributes
end

#assigned_attributes_valuesObject



123
124
125
# File 'lib/zeng/document.rb', line 123

def assigned_attributes_values
  self.assigned_attributes.inject({}){|h,attr| h[attr] = self.send attr; h  }
end

#destroyObject

destroy self from database



119
120
121
# File 'lib/zeng/document.rb', line 119

def destroy
  self.class.destroy(self)
end

#initialize(attributes = {}, new_id = true) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



74
75
76
77
78
79
80
81
82
83
# File 'lib/zeng/document.rb', line 74

def initialize(attributes={}, new_id=true)
  if new_id
    @id = UUID.new.generate
    assigned_attrs = self.class.read_assigned_attributes_with_default_values
    attributes.each_key {|key| assigned_attrs.delete(key)}
    attributes.merge!(assigned_attrs)
  end
  attributes.each {|attr, value| self.send "#{attr}=", value if self.respond_to? "#{attr}=" }
  yield self if block_given?
end

#reloadObject

reload object from database



114
115
116
# File 'lib/zeng/document.rb', line 114

def reload
  self.class.reload(self)
end

#saveObject

we protect class’s backend from instance objects



86
87
88
# File 'lib/zeng/document.rb', line 86

def save
  self.class.save(self)
end

#serialized_attributesObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/zeng/document.rb', line 90

def serialized_attributes
  seri_attrs = assigned_attributes.inject({}){|h,attr|
    h[attr] = self.send attr if self.respond_to? attr
    h
  }
  #
  #don't use :id, or you will get @id=nil when you use marshal to serialize object's attributes
  seri_attrs["id"] = @id
  self.class.serialize seri_attrs
end

#update(attributes = {}) ⇒ Object

update object’s attributes attributesare the attributes needed to be updated



107
108
109
# File 'lib/zeng/document.rb', line 107

def update(attributes={})
  self.class.update(self, attributes)
end