module Dynamini::Attributes
Constants
- ADDABLE_TYPES
- DELETED_TOKEN
Attributes
attributes[R]
Public Instance Methods
add_to(attribute, value)
click to toggle source
# File lib/dynamini/attributes.rb, line 26 def add_to(attribute, value) complain_about(attribute) unless self.class.handles[attribute] old_value = read_attribute(attribute) add_value = self.class.attribute_callback(TypeHandler::SETTER_PROCS, self.class.handles[attribute], value, true) if ADDABLE_TYPES.include? self.class.handles[attribute][:format] @attributes[attribute] ? @attributes[attribute] += add_value : @attributes[attribute] = add_value else complain_about(attribute) end record_change(attribute, old_value, add_value, 'ADD') self end
assign_attributes(attributes)
click to toggle source
# File lib/dynamini/attributes.rb, line 9 def assign_attributes(attributes) attributes.each do |key, value| write_attribute(key, value) end nil end
delete_attribute(attribute)
click to toggle source
# File lib/dynamini/attributes.rb, line 39 def delete_attribute(attribute) if @attributes[attribute] old_value = read_attribute(attribute) record_change(attribute, old_value, DELETED_TOKEN, 'DELETE') @attributes.delete(attribute) end end
delete_attribute!(attribute)
click to toggle source
# File lib/dynamini/attributes.rb, line 47 def delete_attribute!(attribute) delete_attribute(attribute) save! end
handled_attributes()
click to toggle source
# File lib/dynamini/attributes.rb, line 52 def handled_attributes attributes.each_with_object({}) do |(attribute_name, _value), result| result[attribute_name.to_sym] = send(attribute_name.to_sym) end end
inspect()
click to toggle source
# File lib/dynamini/attributes.rb, line 58 def inspect attrib_string = handled_attributes.map { |(a, v)| "#{a}: #{v.inspect}" }.join(', ') "#<#{self.class} #{attrib_string}>" end
update_attribute(key, value, options = {})
click to toggle source
# File lib/dynamini/attributes.rb, line 16 def update_attribute(key, value, options = {}) write_attribute(key, value) save!(options) end
update_attributes(attributes, options = {})
click to toggle source
# File lib/dynamini/attributes.rb, line 21 def update_attributes(attributes, options = {}) assign_attributes(attributes) save!(options) end
Private Instance Methods
attribute_name(name)
click to toggle source
# File lib/dynamini/attributes.rb, line 87 def attribute_name(name) name[0..-2].to_sym end
attribute_updates()
click to toggle source
# File lib/dynamini/attributes.rb, line 65 def attribute_updates changes.reduce({}) do |updates, (key, value)| # TODO: remove this ternary once aws-sdk accepts empty set pull request current_value = value[1].is_a?(Set) && value[1].empty? ? nil : value[1] updates[key] = { action: value[2] || 'PUT' } updates[key][:value] = current_value unless current_value == DELETED_TOKEN updates end end
complain_about(attribute)
click to toggle source
# File lib/dynamini/attributes.rb, line 99 def complain_about(attribute) raise ArgumentError, "#{attribute.capitalize} is not handled as an addable type. Addable types are set, array, integer, float, time, and date." end
method_missing(name, *args, &block)
click to toggle source
Calls superclass method
# File lib/dynamini/attributes.rb, line 75 def method_missing(name, *args, &block) if write_method?(name) write_attribute(attribute_name(name), args.first) elsif was_method?(name) __was(name) elsif args.empty? && read_method?(name) read_attribute(name) else super end end
process_change(attribute, new_value, old_value, options)
click to toggle source
# File lib/dynamini/attributes.rb, line 117 def process_change(attribute, new_value, old_value, options) if new_value == @original_values[attribute] clear_change(attribute) else record_change(attribute, old_value, new_value, options[:action]) end end
read_attribute(name)
click to toggle source
# File lib/dynamini/attributes.rb, line 130 def read_attribute(name) value = @attributes[name] if (handle = self.class.handles[name.to_sym]) value = handle[:options][:default] if value.nil? value = self.class.attribute_callback(TypeHandler::GETTER_PROCS, handle, value, false) unless value.nil? end value end
read_method?(name)
click to toggle source
# File lib/dynamini/attributes.rb, line 91 def read_method?(name) name =~ /^([a-zA-Z][-_\w]*)[^=?]*$/ end
respond_to_missing?(name, include_private = false)
click to toggle source
Calls superclass method
# File lib/dynamini/attributes.rb, line 103 def respond_to_missing?(name, include_private = false) @attributes.keys.include?(name) || write_method?(name) || was_method?(name) || super end
set_original_value(attribute, old_value)
click to toggle source
# File lib/dynamini/attributes.rb, line 125 def set_original_value(attribute, old_value) @original_values ||= {} @original_values[attribute] = old_value unless @original_values.keys.include?(attribute) end
write_attribute(attribute, new_value, change: true, **options)
click to toggle source
# File lib/dynamini/attributes.rb, line 107 def write_attribute(attribute, new_value, change: true, **options) old_value = read_attribute(attribute) if (handle = self.class.handles[attribute.to_sym]) new_value = self.class.attribute_callback(TypeHandler::SETTER_PROCS, handle, new_value, change) end @attributes[attribute] = new_value set_original_value(attribute, old_value) if change process_change(attribute, new_value, old_value, options) if change == true && new_value != old_value end
write_method?(name)
click to toggle source
# File lib/dynamini/attributes.rb, line 95 def write_method?(name) name =~ /^([a-zA-Z][-_\w]*)=.*$/ end