Module: Cistern::Attributes::InstanceMethods
Instance Method Summary collapse
- #_dump(level) ⇒ Object
- #attributes ⇒ Object
- #attributes=(attributes) ⇒ Object
- #changed ⇒ Object
- #dirty? ⇒ Boolean
- #dirty_attributes ⇒ Object
- #dup ⇒ Object
- #identity ⇒ Object
- #identity=(new_identity) ⇒ Object
- #merge_attributes(new_attributes = {}) ⇒ Object
- #new_record? ⇒ Boolean
- #read_attribute(name) ⇒ Object
-
#requires(*args) ⇒ Object
check that the attributes specified in args exist and is not nil.
- #requires_one(*args) ⇒ Object
- #write_attribute(name, value) ⇒ Object
Instance Method Details
#_dump(level) ⇒ Object
119 120 121 |
# File 'lib/cistern/attributes.rb', line 119 def _dump(level) Marshal.dump(attributes) end |
#attributes ⇒ Object
155 156 157 |
# File 'lib/cistern/attributes.rb', line 155 def attributes @attributes ||= {} end |
#attributes=(attributes) ⇒ Object
159 160 161 |
# File 'lib/cistern/attributes.rb', line 159 def attributes=(attributes) @attributes = attributes end |
#changed ⇒ Object
247 248 249 |
# File 'lib/cistern/attributes.rb', line 247 def changed @changes ||= {} end |
#dirty? ⇒ Boolean
239 240 241 |
# File 'lib/cistern/attributes.rb', line 239 def dirty? changed.any? end |
#dirty_attributes ⇒ Object
243 244 245 |
# File 'lib/cistern/attributes.rb', line 243 def dirty_attributes changed.inject({}) { |r,(k,(_,v))| r.merge(k => v) } end |
#dup ⇒ Object
163 164 165 166 167 |
# File 'lib/cistern/attributes.rb', line 163 def dup copy = super copy.attributes = copy.attributes.dup copy end |
#identity ⇒ Object
169 170 171 |
# File 'lib/cistern/attributes.rb', line 169 def identity send(self.class.instance_variable_get('@identity')) end |
#identity=(new_identity) ⇒ Object
173 174 175 |
# File 'lib/cistern/attributes.rb', line 173 def identity=(new_identity) send("#{self.class.instance_variable_get('@identity')}=", new_identity) end |
#merge_attributes(new_attributes = {}) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/cistern/attributes.rb', line 177 def merge_attributes(new_attributes = {}) protected_methods = (Cistern::Model.instance_methods - [:service, :identity, :collection]) ignored_attributes = self.class.ignored_attributes class_attributes = self.class.attributes class_aliases = self.class.aliases new_attributes.each do |_key, value| string_key = _key.kind_of?(String) ? _key : _key.to_s symbol_key = case _key when String _key.to_sym when Symbol _key else string_key.to_sym end # find nested paths value.is_a?(::Hash) && class_attributes.each do |name, | if [:squash] && [:squash].first == string_key send("#{name}=", {symbol_key => value}) end end unless ignored_attributes.include?(symbol_key) if class_aliases.has_key?(symbol_key) class_aliases[symbol_key].each do |aliased_key| send("#{aliased_key}=", value) end end assignment_method = "#{string_key}=" if !protected_methods.include?(symbol_key) && self.respond_to?(assignment_method, true) send(assignment_method, value) end end end changed.clear self end |
#new_record? ⇒ Boolean
218 219 220 |
# File 'lib/cistern/attributes.rb', line 218 def new_record? !identity end |
#read_attribute(name) ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/cistern/attributes.rb', line 123 def read_attribute(name) = self.class.attributes[name] || {} # record the attribute was accessed self.class.attributes[name.to_s.to_sym][:coverage_hits] += 1 rescue nil attributes.fetch(name.to_s.to_sym, [:default]) end |
#requires(*args) ⇒ Object
check that the attributes specified in args exist and is not nil
223 224 225 226 227 228 229 230 |
# File 'lib/cistern/attributes.rb', line 223 def requires(*args) missing = missing_attributes(args) if missing.length == 1 raise(ArgumentError, "#{missing.first} is required for this operation") elsif missing.any? raise(ArgumentError, "#{missing[0...-1].join(", ")} and #{missing[-1]} are required for this operation") end end |
#requires_one(*args) ⇒ Object
232 233 234 235 236 237 |
# File 'lib/cistern/attributes.rb', line 232 def requires_one(*args) missing = missing_attributes(args) if missing.length == args.length raise(ArgumentError, "#{missing[0...-1].join(", ")} or #{missing[-1]} are required for this operation") end end |
#write_attribute(name, value) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/cistern/attributes.rb', line 131 def write_attribute(name, value) = self.class.attributes[name] || {} transform = Cistern::Attributes.transforms[[:squash] ? :squash : :none] || Cistern::Attributes.default_transform parser = Cistern::Attributes.parsers[[:type]] || [:parser] || Cistern::Attributes.default_parser transformed = transform.call(name, value, ) new_value = parser.call(transformed, ) attribute = name.to_s.to_sym previous_value = attributes[attribute] attributes[attribute] = new_value changed!(attribute, previous_value, new_value) new_value end |