Module: Cistern::Attributes::InstanceMethods
- Included in:
- Model
- Defined in:
- lib/cistern/attributes.rb
Instance Method Summary collapse
- #attributes ⇒ Object
- #attributes=(attributes) ⇒ Object
- #changed ⇒ Object
- #dirty? ⇒ Boolean
- #dirty_attributes ⇒ Object
- #dump ⇒ Object
- #dup ⇒ Object
- #identity ⇒ Object
- #identity=(new_identity) ⇒ Object
-
#merge_attributes(new_attributes = {}) ⇒ Object
Update model’s attributes.
- #new_record? ⇒ Boolean
- #read_attribute(name) ⇒ Object
-
#requires(*args) ⇒ Hash
Require specification of certain attributes.
-
#requires_one(*args) ⇒ Hash
Require specification of one or more attributes.
-
#stage_attributes(new_attributes = {}) ⇒ Object
Update model’s attributes.
- #write_attribute(name, value) ⇒ Object
Instance Method Details
#attributes ⇒ Object
164 165 166 |
# File 'lib/cistern/attributes.rb', line 164 def attributes @attributes ||= {} end |
#attributes=(attributes) ⇒ Object
168 169 170 |
# File 'lib/cistern/attributes.rb', line 168 def attributes=(attributes) @attributes = attributes end |
#changed ⇒ Object
259 260 261 |
# File 'lib/cistern/attributes.rb', line 259 def changed @changes ||= {} end |
#dirty? ⇒ Boolean
251 252 253 |
# File 'lib/cistern/attributes.rb', line 251 def dirty? changed.any? end |
#dirty_attributes ⇒ Object
255 256 257 |
# File 'lib/cistern/attributes.rb', line 255 def dirty_attributes changed.inject({}) { |r, (k, (_, v))| r.merge(k => v) } end |
#dump ⇒ Object
123 124 125 |
# File 'lib/cistern/attributes.rb', line 123 def dump Marshal.dump(attributes) end |
#dup ⇒ Object
172 173 174 |
# File 'lib/cistern/attributes.rb', line 172 def dup super.tap { |m| m.attributes = attributes.dup } end |
#identity ⇒ Object
176 177 178 179 180 |
# File 'lib/cistern/attributes.rb', line 176 def identity key = self.class.instance_variable_get('@identity') public_send(key) if key end |
#identity=(new_identity) ⇒ Object
182 183 184 185 186 187 188 189 190 |
# File 'lib/cistern/attributes.rb', line 182 def identity=(new_identity) key = self.class.instance_variable_get('@identity') if key public_send("#{key}=", new_identity) else fail ArgumentError, 'Identity not specified' end end |
#merge_attributes(new_attributes = {}) ⇒ Object
Update model’s attributes. New attributes take precedence over existing attributes.
This is bst called within a Model#save, when #new_attributes represents a recently presented remote resource. #dirty_attributes is cleared after merging.
198 199 200 201 202 203 204 |
# File 'lib/cistern/attributes.rb', line 198 def merge_attributes(new_attributes = {}) _merge_attributes(new_attributes) changed.clear self end |
#new_record? ⇒ Boolean
217 218 219 |
# File 'lib/cistern/attributes.rb', line 217 def new_record? identity.nil? end |
#read_attribute(name) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/cistern/attributes.rb', line 127 def read_attribute(name) key = name.to_sym = self.class.attributes[key] default = [:default] # record the attribute was accessed if defined?(Cistern::Coverage) && [:coverage_hits] [:coverage_hits] += 1 end default = Marshal.load(Marshal.dump(default)) unless default.nil? attributes.fetch(key, default) end |
#requires(*args) ⇒ Hash
Require specification of certain attributes
225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/cistern/attributes.rb', line 225 def requires(*args) missing, required = missing_attributes(args) if missing.length == 1 fail(ArgumentError, "#{missing.keys.first} is required for this operation") elsif missing.any? fail(ArgumentError, "#{missing.keys[0...-1].join(', ')} and #{missing.keys[-1]} are required for this operation") end required end |
#requires_one(*args) ⇒ Hash
Require specification of one or more attributes.
241 242 243 244 245 246 247 248 249 |
# File 'lib/cistern/attributes.rb', line 241 def requires_one(*args) missing, required = missing_attributes(args) if missing.length == args.length fail(ArgumentError, "#{missing.keys[0...-1].join(', ')} or #{missing.keys[-1]} are required for this operation") end required end |
#stage_attributes(new_attributes = {}) ⇒ Object
Update model’s attributes. New attributes take precedence over existing attributes.
This is best called within a Model#update, when #new_attributes represents attributes to be presented to a remote service. #dirty_attributes will contain the valid portion of #new_attributes
212 213 214 215 |
# File 'lib/cistern/attributes.rb', line 212 def stage_attributes(new_attributes = {}) _merge_attributes(new_attributes) self end |
#write_attribute(name, value) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/cistern/attributes.rb', line 143 def write_attribute(name, value) = self.class.attributes[name] || {} transform = [:transform] parser = [:parser] transformed = transform.call(name, value, ) new_value = parser.call(transformed, ) attribute = name.to_s.to_sym previous_value = read_attribute(name) attributes[attribute] = new_value changed!(attribute, previous_value, new_value) new_value end |