Module: Dynamoid::Persistence::ClassMethods

Defined in:
lib/dynamoid/persistence.rb

Instance Method Summary collapse

Instance Method Details

#create_table(options = {}) ⇒ Object

Creates a table.

Parameters:

  • options (Hash) (defaults to: {})

    options to pass for table creation

Options Hash (options):

  • :id (Symbol)

    the id field for the table

  • :table_name (Symbol)

    the actual name for the table

  • :read_capacity (Integer)

    set the read capacity for the table; does not work on existing tables

  • :write_capacity (Integer)

    set the write capacity for the table; does not work on existing tables

  • {range_key (Hash)

    > :type} a hash of the name of the range key and a symbol of its type

Since:

  • 0.4.0



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dynamoid/persistence.rb', line 33

def create_table(options = {})
  if self.range_key
    range_key_hash = { range_key => dynamo_type(attributes[range_key][:type]) }
  else
    range_key_hash = nil
  end
  options = {
    :id => self.hash_key,
    :table_name => self.table_name,
    :write_capacity => self.write_capacity,
    :read_capacity => self.read_capacity,
    :range_key => range_key_hash
  }.merge(options)

  return true if table_exists?(options[:table_name])

  Dynamoid::Adapter.tables << options[:table_name] if Dynamoid::Adapter.create_table(options[:table_name], options[:id], options)
end

#dynamo_type(type) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/dynamoid/persistence.rb', line 115

def dynamo_type(type)
  case type
  when :integer, :float, :datetime
    :number
  when :string, :serialized
    :string
  else
    raise 'unknown type'
  end
end

#from_database(attrs = {}) ⇒ Object



59
60
61
# File 'lib/dynamoid/persistence.rb', line 59

def from_database(attrs = {})
  new(attrs).tap { |r| r.new_record = false }
end

#table_exists?(table_name) ⇒ Boolean

Does a table with this name exist?

Returns:

  • (Boolean)

Since:

  • 0.2.0



55
56
57
# File 'lib/dynamoid/persistence.rb', line 55

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

#table_nameObject

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}_#{options[:name] ? options[:name] : self.name.split('::').last.downcase.pluralize}"
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.

Since:

  • 0.2.0



66
67
68
69
70
71
72
73
74
# File 'lib/dynamoid/persistence.rb', line 66

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

#undump_field(value, options) ⇒ Object

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



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
# File 'lib/dynamoid/persistence.rb', line 80

def undump_field(value, options)
  if value.nil? && (default_value = options[:default])
    value = default_value.respond_to?(:call) ? default_value.call : default_value
  else
    return if value.nil? || (value.respond_to?(:empty?) && value.empty?)
  end

  case options[: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)
      options[:serializer] ? options[:serializer].load(value) : YAML.load(value)
    else
      value
    end
  end
end