Module: OceanDynamo::Attributes
- Included in:
- Table
- Defined in:
- lib/ocean-dynamo/attributes.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
The hash of attributes and their values.
-
#destroyed ⇒ Object
readonly
:nodoc:.
-
#new_record ⇒ Object
readonly
:nodoc:.
Class Method Summary collapse
Instance Method Summary collapse
- #[](attribute) ⇒ Object
- #[]=(attribute, value) ⇒ Object
- #assign_attributes(values, without_protection: false) ⇒ Object
-
#hash_key ⇒ Object
Returns the value of the hash key attribute.
- #id ⇒ Object
- #id=(value) ⇒ Object
- #id? ⇒ Boolean
- #initialize(attrs = {}) {|_self| ... } ⇒ Object
-
#range_key ⇒ Object
Returns the value of the range key attribute or false if the table doesn’t have a range_key.
- #read_attribute(attr_name) ⇒ Object
- #read_attribute_for_validation(key) ⇒ Object
- #to_key ⇒ Object
- #type_cast_attribute_for_write(name, value, metadata = , type: ) ⇒ Object
- #write_attribute(attr_name, value) ⇒ Object
Instance Attribute Details
#attributes ⇒ Object (readonly)
The hash of attributes and their values. Keys are strings.
54 55 56 |
# File 'lib/ocean-dynamo/attributes.rb', line 54 def attributes @attributes end |
#destroyed ⇒ Object (readonly)
:nodoc:
56 57 58 |
# File 'lib/ocean-dynamo/attributes.rb', line 56 def destroyed @destroyed end |
#new_record ⇒ Object (readonly)
:nodoc:
57 58 59 |
# File 'lib/ocean-dynamo/attributes.rb', line 57 def new_record @new_record end |
Class Method Details
.included(base) ⇒ Object
4 5 6 |
# File 'lib/ocean-dynamo/attributes.rb', line 4 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#[](attribute) ⇒ Object
91 92 93 |
# File 'lib/ocean-dynamo/attributes.rb', line 91 def [](attribute) read_attribute attribute end |
#[]=(attribute, value) ⇒ Object
96 97 98 |
# File 'lib/ocean-dynamo/attributes.rb', line 96 def []=(attribute, value) write_attribute attribute, value end |
#assign_attributes(values, without_protection: false) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 |
# 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 ⇒ Object
Returns the value of the hash key attribute
77 78 79 |
# File 'lib/ocean-dynamo/attributes.rb', line 77 def hash_key read_attribute(table_hash_key) end |
#id ⇒ Object
101 102 103 |
# File 'lib/ocean-dynamo/attributes.rb', line 101 def id hash_key end |
#id=(value) ⇒ Object
106 107 108 |
# File 'lib/ocean-dynamo/attributes.rb', line 106 def id=(value) write_attribute(table_hash_key, value) end |
#id? ⇒ Boolean
111 112 113 |
# File 'lib/ocean-dynamo/attributes.rb', line 111 def id? hash_key.present? end |
#initialize(attrs = {}) {|_self| ... } ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# 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 |
#range_key ⇒ Object
Returns the value of the range key attribute or false if the table doesn’t have a range_key.
86 87 88 |
# File 'lib/ocean-dynamo/attributes.rb', line 86 def range_key table_range_key && read_attribute(table_range_key) end |
#read_attribute(attr_name) ⇒ Object
121 122 123 124 125 126 127 128 129 |
# 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) ⇒ Object
116 117 118 |
# File 'lib/ocean-dynamo/attributes.rb', line 116 def read_attribute_for_validation(key) @attributes[key.to_s] end |
#to_key ⇒ Object
143 144 145 146 147 148 |
# 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 = , type: ) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/ocean-dynamo/attributes.rb', line 164 def type_cast_attribute_for_write(name, value, =fields[name], type: [: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) ⇒ Object
132 133 134 135 136 137 138 139 140 |
# 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 |