Module: Cistern::Attributes::InstanceMethods
- Included in:
- Model
- Defined in:
- lib/cistern/attributes.rb
Instance Method Summary collapse
- #attributes ⇒ Object
- #changed ⇒ Object
- #clone_attributes ⇒ Object
- #dirty? ⇒ Boolean
- #dirty_attributes ⇒ Object
- #dirty_request_attributes ⇒ Object
- #dup ⇒ Object
- #identity ⇒ Object
- #identity=(new_identity) ⇒ Object
-
#merge_attributes(new_attributes = {}) ⇒ Object
(also: #attributes=)
Update model’s attributes.
- #new_record? ⇒ Boolean
- #read_attribute(name) ⇒ Object
- #request_attributes(set = attributes) ⇒ Object
-
#requires(*args, **kwargs) ⇒ Hash
Require specification of certain attributes.
-
#requires_one(*args, **kwargs) ⇒ 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
173 174 175 |
# File 'lib/cistern/attributes.rb', line 173 def attributes @attributes ||= {} end |
#changed ⇒ Object
266 267 268 |
# File 'lib/cistern/attributes.rb', line 266 def changed @changes ||= {} end |
#clone_attributes ⇒ Object
132 133 134 |
# File 'lib/cistern/attributes.rb', line 132 def clone_attributes Marshal.load Marshal.dump(attributes) end |
#dirty? ⇒ Boolean
258 259 260 |
# File 'lib/cistern/attributes.rb', line 258 def dirty? changed.any? end |
#dirty_attributes ⇒ Object
262 263 264 |
# File 'lib/cistern/attributes.rb', line 262 def dirty_attributes changed.inject({}) { |r, (k, (_, v))| r.merge(k => v) } end |
#dirty_request_attributes ⇒ Object
278 279 280 |
# File 'lib/cistern/attributes.rb', line 278 def dirty_request_attributes request_attributes(dirty_attributes) end |
#dup ⇒ Object
177 178 179 |
# File 'lib/cistern/attributes.rb', line 177 def dup super.tap { |m| m.set_attributes attributes.dup } end |
#identity ⇒ Object
181 182 183 184 185 |
# File 'lib/cistern/attributes.rb', line 181 def identity key = self.class.identity public_send(key) if key end |
#identity=(new_identity) ⇒ Object
187 188 189 190 191 192 193 194 195 |
# File 'lib/cistern/attributes.rb', line 187 def identity=(new_identity) key = self.class.identity if key public_send("#{key}=", new_identity) else fail ArgumentError, 'Identity not specified' end end |
#merge_attributes(new_attributes = {}) ⇒ Object Also known as: attributes=
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.
203 204 205 206 207 208 209 |
# File 'lib/cistern/attributes.rb', line 203 def merge_attributes(new_attributes = {}) _merge_attributes(new_attributes) changed.clear self end |
#new_record? ⇒ Boolean
224 225 226 |
# File 'lib/cistern/attributes.rb', line 224 def new_record? identity.nil? end |
#read_attribute(name) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/cistern/attributes.rb', line 136 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 |
#request_attributes(set = attributes) ⇒ Object
270 271 272 273 274 275 276 |
# File 'lib/cistern/attributes.rb', line 270 def request_attributes(set = attributes) set.inject({}) do |a,(k,v)| aliases = self.class.attributes[k.to_sym][:aliases] aliases << k if aliases.empty? aliases.each_with_object(a) { |n,r| r[n.to_s] = v } end end |
#requires(*args, **kwargs) ⇒ Hash
Require specification of certain attributes
232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/cistern/attributes.rb', line 232 def requires(*args, **kwargs) 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, **kwargs) ⇒ Hash
Require specification of one or more attributes.
248 249 250 251 252 253 254 255 256 |
# File 'lib/cistern/attributes.rb', line 248 def requires_one(*args, **kwargs) 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
219 220 221 222 |
# File 'lib/cistern/attributes.rb', line 219 def stage_attributes(new_attributes = {}) _merge_attributes(new_attributes) self end |
#write_attribute(name, value) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/cistern/attributes.rb', line 152 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 |