Module: OceanDynamo::Attributes

Included in:
Table
Defined in:
lib/ocean-dynamo/attributes.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject (readonly)

The hash of attributes and their values. Keys are strings.



53
54
55
# File 'lib/ocean-dynamo/attributes.rb', line 53

def attributes
  @attributes
end

#destroyedObject (readonly)

:nodoc:



55
56
57
# File 'lib/ocean-dynamo/attributes.rb', line 55

def destroyed
  @destroyed
end

#new_recordObject (readonly)

:nodoc:



56
57
58
# File 'lib/ocean-dynamo/attributes.rb', line 56

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



90
91
92
# File 'lib/ocean-dynamo/attributes.rb', line 90

def [](attribute)
  read_attribute attribute
end

#[]=(attribute, value) ⇒ Object



95
96
97
# File 'lib/ocean-dynamo/attributes.rb', line 95

def []=(attribute, value)
  write_attribute attribute, value
end

#assign_attributes(values, without_protection: false) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ocean-dynamo/attributes.rb', line 150

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_keyObject

Returns the value of the hash key attribute



76
77
78
# File 'lib/ocean-dynamo/attributes.rb', line 76

def hash_key
  read_attribute(table_hash_key)
end

#idObject



100
101
102
# File 'lib/ocean-dynamo/attributes.rb', line 100

def id
  hash_key
end

#id=(value) ⇒ Object



105
106
107
# File 'lib/ocean-dynamo/attributes.rb', line 105

def id=(value)
  write_attribute(table_hash_key, value)
end

#id?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/ocean-dynamo/attributes.rb', line 110

def id?
  hash_key.present?
end

#initialize(attrs = {}) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

Raises:



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ocean-dynamo/attributes.rb', line 59

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_keyObject

Returns the value of the range key attribute or false if the table doesn’t have a range_key.



85
86
87
# File 'lib/ocean-dynamo/attributes.rb', line 85

def range_key
  table_range_key && read_attribute(table_range_key)
end

#read_attribute(attr_name) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/ocean-dynamo/attributes.rb', line 120

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



115
116
117
# File 'lib/ocean-dynamo/attributes.rb', line 115

def read_attribute_for_validation(key)
  @attributes[key.to_s]
end

#to_keyObject



142
143
144
145
146
147
# File 'lib/ocean-dynamo/attributes.rb', line 142

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



163
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
# File 'lib/ocean-dynamo/attributes.rb', line 163

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 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



131
132
133
134
135
136
137
138
139
# File 'lib/ocean-dynamo/attributes.rb', line 131

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