Module: Dynamoid::Persistence::ClassMethods

Defined in:
lib/dynamoid/persistence.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) create_table(table_name, id = :id, options = {})

Creates a table for a given table name, hash key, and range key.

Since:

  • 0.2.0



26
27
28
# File 'lib/dynamoid/persistence.rb', line 26

def create_table(table_name, id = :id, options = {})
  Dynamoid::Adapter.tables << table_name if Dynamoid::Adapter.create_table(table_name, id.to_sym, options)
end

- (Boolean) table_exists?(table_name)

Does a table with this name exist?

Returns:

  • (Boolean)

Since:

  • 0.2.0



33
34
35
# File 'lib/dynamoid/persistence.rb', line 33

def table_exists?(table_name)
  Dynamoid::Adapter.tables.include?(table_name)
end

- (Object) table_name

Returns the name of the table the class is for.

Since:

  • 0.2.0



19
20
21
# File 'lib/dynamoid/persistence.rb', line 19

def table_name
  "#{Dynamoid::Config.namespace}_#{self.to_s.downcase.pluralize}"
end

- (Object) undump(incoming = nil)

Undump an object into a hash, converting each type from a string representation of itself into the type specified by the field.

Since:

  • 0.2.0



40
41
42
43
44
45
46
47
# File 'lib/dynamoid/persistence.rb', line 40

def undump(incoming = nil)
  (incoming ||= {}).symbolize_keys!
  Hash.new.tap do |hash|
    self.attributes.each do |attribute, options|
      hash[attribute] = undump_field(incoming[attribute], options[:type])
    end
  end
end

- (Object) undump_field(value, type)

Undump a value for a given type. Given a string, it'll determine (based on the type provided) whether to turn it into a string, integer, float, set, array, datetime, or serialized return value.

Since:

  • 0.2.0



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/dynamoid/persistence.rb', line 53

def undump_field(value, type)
  return if value.nil? || (value.respond_to?(:empty?) && value.empty?)

  case type
  when :string
    value.to_s
  when :integer
    value.to_i
  when :float
    value.to_f
  when :set, :array
    if value.is_a?(Set) || value.is_a?(Array)
      value
    else
      Set[value]
    end
  when :datetime
    if value.is_a?(Date) || value.is_a?(DateTime) || value.is_a?(Time)
      value
    else
      Time.at(value).to_datetime
    end
  when :serialized
    if value.is_a?(String)
      YAML.load(value)
    else
      value
    end
  end
end