Module: CuteKV::Document::InstanceMethods

Defined in:
lib/cute_kv/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



77
78
79
# File 'lib/cute_kv/document.rb', line 77

def id
  @id
end

Instance Method Details

#assigned_attributesObject



106
107
108
# File 'lib/cute_kv/document.rb', line 106

def assigned_attributes
  self.class.assigned_attributes
end

#assigned_attributes_valuesObject



128
129
130
# File 'lib/cute_kv/document.rb', line 128

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

#destroyObject

destroy self from database



124
125
126
# File 'lib/cute_kv/document.rb', line 124

def destroy
  self.class.destroy(self)
end

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

Yields:

  • (_self)

Yield Parameters:



79
80
81
82
83
84
85
86
87
88
# File 'lib/cute_kv/document.rb', line 79

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



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

def reload
  self.class.reload(self)
end

#saveObject

we protect class’s backend from instance objects



91
92
93
# File 'lib/cute_kv/document.rb', line 91

def save
  self.class.save(self)
end

#serialized_attributesObject



95
96
97
98
99
100
101
102
103
104
# File 'lib/cute_kv/document.rb', line 95

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



112
113
114
# File 'lib/cute_kv/document.rb', line 112

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