Module: Dynameek::Model::DynamoDb

Included in:
ClassMethods
Defined in:
lib/dynameek/model/dynamo_db.rb

Defined Under Namespace

Classes: StackFrame

Instance Method Summary collapse

Instance Method Details

#build!Object



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
# File 'lib/dynameek/model/dynamo_db.rb', line 85

def build!
  return if dynamo_db.tables[table_name].exists?         
  opts = {:hash_key => { hash_key_info.field => [:datetime, :integer, :float].include?(hash_key_info.type) ? :number : hash_key_info.type }}
  if range?
    opts[:range_key] = { range_info.field => [:datetime, :integer, :float].include?(range_info.type) ? :number : range_info.type } 
  end
  new_table = nil
  idx_table = nil
  if index_table? && !dynamo_db.tables[index_table_name].exists?
    dynamo_db.tables.create(
                    table_name+"_INDEX", read_units, 
                    write_units, opts)  
    new_table     = dynamo_db.tables.create(
                    table_name, read_units, write_units, {
                      hash_key: {
                        hash_key_info.field.to_s+"_"+range_info.field.to_s => :string
                      },
                      range_key: {"dynameek_index" => :number}
                    })
  else
    new_table = dynamo_db.tables.create(table_name, read_units, write_units, opts)
  end
  while new_table.status == :creating || (!idx_table.nil? && idx_table.status == :creating)
    sleep 1
  end
end

#convert_from_dynamodb(type, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dynameek/model/dynamo_db.rb', line 4

def convert_from_dynamodb(type, value)
  return nil if value.nil?
  case type
    when :integer
      value.to_i
    when :float
      value.to_f
    when :string
      value.to_s
    when :datetime
      Time.at(value).to_datetime
    when :boolean
      value.to_i == 1
    when :binary
      begin 
        Marshal.load(value)
      rescue => e
        puts "[WARNING] Failed to load value #{value}"

      end
    else
      value
  end
end

#convert_to_dynamodb(type, value) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dynameek/model/dynamo_db.rb', line 28

def convert_to_dynamodb(type, value)
  return nil if value.nil?
  case type
    when :datetime
      value.to_time.to_f
    when :binary
      dump_safe(value)
      Marshal.dump(value)
    when :boolean
      value ? 1 : 0
    else
      value
  end
end

#dump_safe(obj) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dynameek/model/dynamo_db.rb', line 44

def dump_safe(obj)
  return obj if(!obj.is_a?(Hash) && !obj.is_a?(Array))
  make_safe = lambda do |thing|
    if thing.is_a?(Hash)
      #thing.default_proc = nil
      thing.default = nil
    end
  end
  stack = []
  safe = obj
  make_safe.call(safe)
  stack.push(StackFrame.new(safe, 0))
  
  while(current_frame = stack.pop)
    current = current_frame.current
    index   = current_frame.index
    obj     = current.is_a?(Hash) ? current[current.keys[index]] : current[index]
    make_safe.call(obj) 
    if index - 1 < current.size
      # Re-add current enumerable if still elements to check
      stack.push(StackFrame.new(current, index + 1))
    end
    if obj.is_a?(Hash) || obj.is_a?(Array)
      #If obj is enumerable add to stack
      stack.push(StackFrame.new(obj, 0))
    end
  end
  safe
end

#dynamo_dbObject



112
113
114
115
# File 'lib/dynameek/model/dynamo_db.rb', line 112

def dynamo_db
  @dynamo_db ||= AWS::DynamoDB.new 
  @dynamo_db
end

#exists?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/dynameek/model/dynamo_db.rb', line 81

def exists?
  table.exists?
end

#index_tableObject



123
124
125
126
127
128
129
130
# File 'lib/dynameek/model/dynamo_db.rb', line 123

def index_table
  build!
  @index_table ||= dynamo_db.tables[index_table_name]
  if @index_table.exists? && !@index_table.schema_loaded?
    @index_table.load_schema
  end
  @index_table
end

#index_table_exists?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/dynameek/model/dynamo_db.rb', line 77

def index_table_exists?
  !index_table.nil? && index_table.exists?
end

#tableObject



117
118
119
120
121
122
# File 'lib/dynameek/model/dynamo_db.rb', line 117

def table
  build!
  @table ||= dynamo_db.tables[table_name]
  @table.load_schema if !@table.schema_loaded?
  @table
end