Module: Kvbean::Base::InstanceMethods

Defined in:
lib/kvbean/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



119
120
121
# File 'lib/kvbean/base.rb', line 119

def attributes
  @attributes
end

#new_recordObject

Returns the value of attribute new_record.



119
120
121
# File 'lib/kvbean/base.rb', line 119

def new_record
  @new_record
end

Instance Method Details

#==(other) ⇒ Object



211
212
213
# File 'lib/kvbean/base.rb', line 211

def ==(other)
  other.equal?(self) || (other.instance_of?(self.class) && other.id == id)
end

#createObject



185
186
187
188
189
190
# File 'lib/kvbean/base.rb', line 185

def create
  self.id ||= generate_id
  @new_record = false
  raw_create
  self.id
end

#destroyObject



197
198
199
200
# File 'lib/kvbean/base.rb', line 197

def destroy
  raw_destroy
  true
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/kvbean/base.rb', line 215

def eql?(other)
  self == other
end

#exists?Boolean Also known as: persisted?

Returns:

  • (Boolean)


202
203
204
# File 'lib/kvbean/base.rb', line 202

def exists?
  !new_record?
end

#generate_idObject



207
208
209
# File 'lib/kvbean/base.rb', line 207

def generate_id
  SecureRandom.hex(13)
end

#idObject



157
158
159
# File 'lib/kvbean/base.rb', line 157

def id
  attributes[self.class.primary_key]
end

#id=(value) ⇒ Object



161
162
163
# File 'lib/kvbean/base.rb', line 161

def id=(value)
  attributes[self.class.primary_key] = value
end

#initialize(attributes = {}) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/kvbean/base.rb', line 121

def initialize(attributes = {})
  @new_record = true
  @attributes = {}.with_indifferent_access
  @attributes.merge!(self.class.kv_fields.inject({}) {|h, n| h[n] = nil; h })
  @changed_attributes = {}
  load_to_instance(attributes)
end

#load_to_instance(attributes) ⇒ Object



139
140
141
142
143
144
# File 'lib/kvbean/base.rb', line 139

def load_to_instance(attributes)
  return unless attributes
  attributes.each do |key, value|
    self.send "#{key}=".to_sym, value
  end
end

#new_record?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/kvbean/base.rb', line 135

def new_record?
  @new_record
end

#raw_createObject



169
170
171
172
# File 'lib/kvbean/base.rb', line 169

def raw_create
  super
  self.class.records[self.id] = self.dup
end

#raw_deleteObject



180
181
182
183
# File 'lib/kvbean/base.rb', line 180

def raw_delete
  super
  self.class.records.delete(self.id)
end

#raw_updateObject



174
175
176
177
178
# File 'lib/kvbean/base.rb', line 174

def raw_update
  super
  item = self.class.raw_find(id)
  item.load_to_instance(attributes)
end

#reload_to_instance(attributes) ⇒ Object



146
147
148
149
150
151
# File 'lib/kvbean/base.rb', line 146

def reload_to_instance(attributes)
  return unless attributes
  attributes.each do |key, value|
    self.send "#{key}=".to_sym, value
  end
end

#saveObject



165
166
167
# File 'lib/kvbean/base.rb', line 165

def save
  new_record? ? create : update
end

#savedObject



129
130
131
132
133
# File 'lib/kvbean/base.rb', line 129

def saved
  @previously_changed = changes
  @changed_attributes.clear
  @new_record = false
end

#updateObject



192
193
194
195
# File 'lib/kvbean/base.rb', line 192

def update
  raw_update
  true
end

#update_attributes(attributes) ⇒ Object



153
154
155
# File 'lib/kvbean/base.rb', line 153

def update_attributes(attributes)
  load_to_instance(attributes) && save
end