module OceanDynamo::Attributes
Attributes
attributes[R]
The hash of attributes and their values. Keys are strings.
Public Class Methods
included(base)
click to toggle source
# File lib/ocean-dynamo/attributes.rb, line 4 def self.included(base) base.extend(ClassMethods) end
new(attrs={}) { |self| ... }
click to toggle source
Calls superclass method
# File lib/ocean-dynamo/attributes.rb, line 60 def initialize(attrs={}) @attributes = Hash.new fields.each do |name, md| write_attribute(name, evaluate_default(md[:default], md[:type])) end raise UnknownPrimaryKey unless table_hash_key set_belongs_to_association(attrs) # Barf on unknown attributes here? attrs && attrs.delete_if { |k, v| !fields.has_key?(k) } super(attrs) yield self if block_given? end
Public Instance Methods
[](attribute)
click to toggle source
# File lib/ocean-dynamo/attributes.rb, line 91 def [](attribute) read_attribute attribute end
[]=(attribute, value)
click to toggle source
# File lib/ocean-dynamo/attributes.rb, line 96 def []=(attribute, value) write_attribute attribute, value end
assign_attributes(values, without_protection: false)
click to toggle source
# File lib/ocean-dynamo/attributes.rb, line 151 def assign_attributes(values, without_protection: false) return if values.blank? values = values.stringify_keys set_belongs_to_association(values) # if values.respond_to?(:permitted?) # unless values.permitted? # raise ActiveModel::ForbiddenAttributesError # end # end values.each { |k, v| _assign_attribute(k, v) } end
hash_key()
click to toggle source
Returns the value of the hash key attribute
# File lib/ocean-dynamo/attributes.rb, line 77 def hash_key read_attribute(table_hash_key) end
id()
click to toggle source
# File lib/ocean-dynamo/attributes.rb, line 101 def id hash_key end
id=(value)
click to toggle source
# File lib/ocean-dynamo/attributes.rb, line 106 def id=(value) write_attribute(table_hash_key, value) end
id?()
click to toggle source
# File lib/ocean-dynamo/attributes.rb, line 111 def id? hash_key.present? end
range_key()
click to toggle source
Returns the value of the range key attribute or false if the table doesn't have a range_key.
# File lib/ocean-dynamo/attributes.rb, line 86 def range_key table_range_key && read_attribute(table_range_key) end
read_attribute(attr_name)
click to toggle source
# File lib/ocean-dynamo/attributes.rb, line 121 def read_attribute(attr_name) attr_name = attr_name.to_s attr_name = table_hash_key.to_s if attr_name == 'id' if fields.has_key?(attr_name) @attributes[attr_name] else raise ActiveModel::MissingAttributeError, "can't read unknown attribute '#{attr_name}" end end
read_attribute_for_validation(key)
click to toggle source
# File lib/ocean-dynamo/attributes.rb, line 116 def read_attribute_for_validation(key) @attributes[key.to_s] end
to_key()
click to toggle source
# File lib/ocean-dynamo/attributes.rb, line 143 def to_key return nil unless persisted? key = respond_to?(:id) && id return nil unless key table_range_key ? [key, range_key] : [key] end
type_cast_attribute_for_write(name, value, metadata=fields[name], type: metadata[:type])
click to toggle source
# File lib/ocean-dynamo/attributes.rb, line 164 def type_cast_attribute_for_write(name, value, metadata=fields[name], type: metadata[:type]) case type when :reference return value when :string return nil if value == nil return value.collect(&:to_s) if value.is_a?(Array) value when :integer return nil if value == nil || value == false || value.is_a?(String) && value.blank? return value.collect(&:to_i) if value.is_a?(Array) value.to_i when :float return nil if value == nil || value == false || value.is_a?(String) && value.blank? return value.collect(&:to_f) if value.is_a?(Array) value.to_f when :boolean return nil if value == nil return true if value == true return true if value == "true" false when :datetime return value.to_time(:utc) if value.is_a?(String) return nil if value == nil || !value.kind_of?(Time) value when :serialized return nil if value == nil value else raise UnsupportedType.new(type.to_s) end end
write_attribute(attr_name, value)
click to toggle source
# File lib/ocean-dynamo/attributes.rb, line 132 def write_attribute(attr_name, value) attr_name = attr_name.to_s attr_name = table_hash_key.to_s if attr_name == 'id' if fields.has_key?(attr_name) @attributes[attr_name] = type_cast_attribute_for_write(attr_name, value) else raise ActiveModel::MissingAttributeError, "can't write unknown attribute '#{attr_name}'" end end