Module: Kvbean::Base::InstanceMethods
- Defined in:
- lib/kvbean/base.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#new_record ⇒ Object
Returns the value of attribute new_record.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #create ⇒ Object
- #destroy ⇒ Object
- #eql?(other) ⇒ Boolean
- #exists? ⇒ Boolean (also: #persisted?)
- #generate_id ⇒ Object
- #id ⇒ Object
- #id=(value) ⇒ Object
- #initialize(attributes = {}) ⇒ Object
- #load_to_instance(attributes) ⇒ Object
- #new_record? ⇒ Boolean
- #raw_create ⇒ Object
- #raw_delete ⇒ Object
- #raw_update ⇒ Object
- #reload_to_instance(attributes) ⇒ Object
- #save ⇒ Object
- #saved ⇒ Object
- #update ⇒ Object
- #update_attributes(attributes) ⇒ Object
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
119 120 121 |
# File 'lib/kvbean/base.rb', line 119 def attributes @attributes end |
#new_record ⇒ Object
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 |
#create ⇒ Object
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 |
#destroy ⇒ Object
197 198 199 200 |
# File 'lib/kvbean/base.rb', line 197 def destroy raw_destroy true end |
#eql?(other) ⇒ Boolean
215 216 217 |
# File 'lib/kvbean/base.rb', line 215 def eql?(other) self == other end |
#exists? ⇒ Boolean Also known as: persisted?
202 203 204 |
# File 'lib/kvbean/base.rb', line 202 def exists? !new_record? end |
#generate_id ⇒ Object
207 208 209 |
# File 'lib/kvbean/base.rb', line 207 def generate_id SecureRandom.hex(13) end |
#id ⇒ Object
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
135 136 137 |
# File 'lib/kvbean/base.rb', line 135 def new_record? @new_record end |
#raw_create ⇒ Object
169 170 171 172 |
# File 'lib/kvbean/base.rb', line 169 def raw_create super self.class.records[self.id] = self.dup end |
#raw_delete ⇒ Object
180 181 182 183 |
# File 'lib/kvbean/base.rb', line 180 def raw_delete super self.class.records.delete(self.id) end |
#raw_update ⇒ Object
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 |
#save ⇒ Object
165 166 167 |
# File 'lib/kvbean/base.rb', line 165 def save new_record? ? create : update end |
#saved ⇒ Object
129 130 131 132 133 |
# File 'lib/kvbean/base.rb', line 129 def saved @previously_changed = changes @changed_attributes.clear @new_record = false end |
#update ⇒ Object
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 |