Module: DynamoRecord::Fields::ClassMethods
- Defined in:
- lib/dynamo_record/fields.rb
Instance Method Summary collapse
- #dump_field(value, options) ⇒ Object
- #dynamodb_type(type) ⇒ Object
- #field(name, type = :string, opts = {}) ⇒ Object
- #hash_key ⇒ Object
- #range_key ⇒ Object
- #secondary_indexes ⇒ Object
- #undump_field(value, options) ⇒ Object
- #unload(attrs) ⇒ Object
Instance Method Details
#dump_field(value, options) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/dynamo_record/fields.rb', line 55 def dump_field(value, ) return value if .nil? case [:type] when :datetime value.iso8601 else value # aws-sdk supports the rest of data yptes end end |
#dynamodb_type(type) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/dynamo_record/fields.rb', line 85 def dynamodb_type(type) case type when :integer, :big_decimal 'N' when :string, :datetime 'S' # else # 'S' end end |
#field(name, type = :string, opts = {}) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/dynamo_record/fields.rb', line 14 def field(name, type = :string, opts = {}) named = name.to_s self.attributes.merge!(name => {type: type, options: opts}) define_method("#{named}=") { |value| write_attribute(named, value) } define_method("#{name}") { read_attribute(named) } define_method("#{name}?") do value = read_attribute(named) return value != 'false' if value.is_a?(String) !!value end end |
#hash_key ⇒ Object
73 74 75 |
# File 'lib/dynamo_record/fields.rb', line 73 def hash_key :id # default hash key end |
#range_key ⇒ Object
77 78 79 |
# File 'lib/dynamo_record/fields.rb', line 77 def range_key @range_key ||= self.attributes.select { |k,v| v[:options][:range_key] }.keys.first rescue nil end |
#secondary_indexes ⇒ Object
81 82 83 |
# File 'lib/dynamo_record/fields.rb', line 81 def secondary_indexes @secondary_indexes ||= self.attributes.select { |k,v| v[:options][:index] }.keys rescue nil end |
#undump_field(value, options) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/dynamo_record/fields.rb', line 27 def undump_field(value, ) return nil if .nil? case [:type] when :integer value.to_i when :string value.to_s when :big_decimal value.to_d when :boolean if value == "true" || value == true true elsif value == "false" || value == false false else raise ArgumentError, "Boolean column neither true nor false" end when :datetime if value.is_a?(Date) || value.is_a?(DateTime) || value.is_a?(Time) value else DateTime.parse(value) end else raise ArgumentError, "Unknown type #{[:type]}" end end |
#unload(attrs) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/dynamo_record/fields.rb', line 65 def unload(attrs) Hash.new.tap do |hash| attrs.each do |key, value| hash[key] = dump_field(value, self.attributes[key.to_sym]) end end end |