Module: Dynamoid::Persistence::ClassMethods
- Defined in:
- lib/dynamoid/persistence.rb
Instance Method Summary collapse
-
#create_table(options = {}) ⇒ Object
Creates a table.
- #dynamo_type(type) ⇒ Object
- #from_database(attrs = {}) ⇒ Object
- #table_name ⇒ Object
-
#undump(incoming = nil) ⇒ Object
Undump an object into a hash, converting each type from a string representation of itself into the type specified by the field.
-
#undump_field(value, options) ⇒ Object
Undump a string value for a given type.
Instance Method Details
#create_table(options = {}) ⇒ Object
Creates a table.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/dynamoid/persistence.rb', line 35 def create_table( = {}) if self.range_key range_key_hash = { range_key => dynamo_type(attributes[range_key][:type]) } else range_key_hash = nil end = { :id => self.hash_key, :table_name => self.table_name, :write_capacity => self.write_capacity, :read_capacity => self.read_capacity, :range_key => range_key_hash, :hash_key_type => dynamo_type(attributes[self.hash_key][:type]), :local_secondary_indexes => self.local_secondary_indexes.values, :global_secondary_indexes => self.global_secondary_indexes.values }.merge() Dynamoid.adapter.create_table([:table_name], [:id], ) end |
#dynamo_type(type) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/dynamoid/persistence.rb', line 134 def dynamo_type(type) if type.is_a?(Class) type.respond_to?(:dynamoid_field_type) ? type.dynamoid_field_type : :string else case type when :integer, :number, :datetime :number when :string, :serialized :string else raise 'unknown type' end end end |
#from_database(attrs = {}) ⇒ Object
55 56 57 58 |
# File 'lib/dynamoid/persistence.rb', line 55 def from_database(attrs = {}) clazz = attrs[:type] ? obj = attrs[:type].constantize : self clazz.new(attrs).tap { |r| r.new_record = false } end |
#table_name ⇒ Object
18 19 20 21 22 23 |
# File 'lib/dynamoid/persistence.rb', line 18 def table_name table_base_name = [:name] || base_class.name.split('::').last .downcase.pluralize @table_name ||= [Dynamoid::Config.namespace.to_s,table_base_name].reject(&:empty?).join("_") end |
#undump(incoming = nil) ⇒ Object
Undump an object into a hash, converting each type from a string representation of itself into the type specified by the field.
63 64 65 66 67 68 69 70 71 |
# File 'lib/dynamoid/persistence.rb', line 63 def undump(incoming = nil) incoming = (incoming || {}).symbolize_keys Hash.new.tap do |hash| self.attributes.each do |attribute, | hash[attribute] = undump_field(incoming[attribute], ) end incoming.each {|attribute, value| hash[attribute] = value unless hash.has_key? attribute } end end |
#undump_field(value, options) ⇒ Object
Undump a string value for a given type.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/dynamoid/persistence.rb', line 76 def undump_field(value, ) if (field_class = [:type]).is_a?(Class) raise 'Dynamoid class-type fields do not support default values' if [:default] if field_class.respond_to?(:dynamoid_load) field_class.dynamoid_load(value) end elsif [:type] == :serialized if value.is_a?(String) [:serializer] ? [:serializer].load(value) : YAML.load(value) else value end else if value.nil? && (default_value = [:default]) value = default_value.respond_to?(:call) ? default_value.call : default_value end unless value.nil? case [:type] when :string value.to_s when :integer Integer(value) when :number BigDecimal.new(value.to_s) when :array value.to_a when :raw if value.is_a?(Hash) undump_hash(value) else value end when :set Set.new(value) when :datetime if value.is_a?(Date) || value.is_a?(DateTime) || value.is_a?(Time) value else Time.at(value).to_datetime end when :boolean # persisted as 't', but because undump is called during initialize it can come in as true if value == 't' || value == true true elsif value == 'f' || value == false false else raise ArgumentError, "Boolean column neither true nor false" end else raise ArgumentError, "Unknown type #{[:type]}" end end end end |