Class: FakeDynamo::DB

Inherits:
Object
  • Object
show all
Includes:
Validation
Defined in:
lib/fake_dynamo/db.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validation

#add_errors, #api_config, #api_config_path, #api_input_spec, #available_operations, #param, #validate!, #validate_input, #validate_key_data, #validate_key_schema, #validate_operation, #validate_payload, #validate_spec, #validate_type

Constructor Details

#initializeDB

Returns a new instance of DB.



14
15
16
# File 'lib/fake_dynamo/db.rb', line 14

def initialize
  @tables = {}
end

Instance Attribute Details

#tablesObject

Returns the value of attribute tables.



6
7
8
# File 'lib/fake_dynamo/db.rb', line 6

def tables
  @tables
end

Class Method Details

.delegate_to_table(*methods) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/fake_dynamo/db.rb', line 76

def self.delegate_to_table(*methods)
  methods.each do |method|
    define_method(method) do |data|
      find_table(data['TableName']).send(method, data)
    end
  end
end

.instanceObject



9
10
11
# File 'lib/fake_dynamo/db.rb', line 9

def instance
  @db ||= DB.new
end

Instance Method Details

#batch_get_item(data) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fake_dynamo/db.rb', line 87

def batch_get_item(data)
  response = {}

  data['RequestItems'].each do |table_name, table_data|
    table = find_table(table_name)

    unless response[table_name]
      response[table_name] = { 'ConsumedCapacityUnits' => 1, 'Items' => [] }
    end

    table_data['Keys'].each do |key|
      if item_hash = table.get_raw_item(key, table_data['AttributesToGet'])
        response[table_name]['Items'] << item_hash
      end
    end
  end

  { 'Responses' => response, 'UnprocessedKeys' => {}}
end

#create_table(data) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/fake_dynamo/db.rb', line 24

def create_table(data)
  table_name = data['TableName']
  raise ResourceInUseException, "Duplicate table name: #{table_name}" if tables[table_name]

  table = Table.new(data)
  tables[table_name] = table
  response = table.description
  table.activate
  response
end

#delete_table(data) ⇒ Object



40
41
42
43
44
45
# File 'lib/fake_dynamo/db.rb', line 40

def delete_table(data)
  table_name = data['TableName']
  table = find_table(table_name)
  tables.delete(table_name)
  table.delete
end

#describe_table(data) ⇒ Object



35
36
37
38
# File 'lib/fake_dynamo/db.rb', line 35

def describe_table(data)
  table = find_table(data['TableName'])
  table.describe_table
end

#list_tables(data) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fake_dynamo/db.rb', line 47

def list_tables(data)
  start_table = data['ExclusiveStartTableName']
  limit = data['Limit']

  all_tables = tables.keys
  start = 0

  if start_table
    if i = all_tables.index(start_table)
      start = i + 1
    end
  end

  limit ||= all_tables.size
  result_tables = all_tables[start, limit]
  response = { 'TableNames' => result_tables }

  if (start + limit ) < all_tables.size
    last_table = all_tables[start + limit -1]
    response.merge!({ 'LastEvaluatedTableName' => last_table })
  end
  response
end

#process(operation, data) ⇒ Object



18
19
20
21
22
# File 'lib/fake_dynamo/db.rb', line 18

def process(operation, data)
  validate_payload(operation, data)
  operation = operation.underscore
  self.send operation, data
end

#update_table(data) ⇒ Object



71
72
73
74
# File 'lib/fake_dynamo/db.rb', line 71

def update_table(data)
  table = find_table(data['TableName'])
  table.update(data['ProvisionedThroughput']['ReadCapacityUnits'], data['ProvisionedThroughput']['WriteCapacityUnits'])
end