Class: FakeDynamo::Item
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
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
4
5
6
|
# File 'lib/fake_dynamo/item.rb', line 4
def attributes
@attributes
end
|
#key ⇒ Object
Returns the value of attribute key.
4
5
6
|
# File 'lib/fake_dynamo/item.rb', line 4
def key
@key
end
|
Class Method Details
.from_data(data, key_schema) ⇒ Object
7
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/fake_dynamo/item.rb', line 7
def from_data(data, key_schema)
item = Item.new
item.key = Key.from_schema(data, key_schema)
item.attributes = {}
data.each do |name, value|
unless item.key[name]
item.attributes[name] = Attribute.from_hash(name, value)
end
end
item
end
|
.from_key(key) ⇒ Object
20
21
22
23
24
25
|
# File 'lib/fake_dynamo/item.rb', line 20
def from_key(key)
item = Item.new
item.key = key
item.attributes = {}
item
end
|
Instance Method Details
#[](name) ⇒ Object
29
30
31
|
# File 'lib/fake_dynamo/item.rb', line 29
def [](name)
attributes[name] or key[name]
end
|
#add(name, value) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/fake_dynamo/item.rb', line 82
def add(name, value)
attribute = Attribute.from_hash(name, value)
unless ["N", "SS", "NS"].include? attribute.type
raise ValidationException, "Action ADD is not supported for type #{attribute.type}"
end
if old_attribute = attributes[name]
validate_type(value, old_attribute)
case attribute.type
when "N"
old_attribute.value = (old_attribute.value.to_i + attribute.value.to_i).to_s
else
old_attribute.value += attribute.value
old_attribute.value.uniq!
end
else
attributes[name] = attribute
end
end
|
#as_hash ⇒ Object
33
34
35
36
37
38
39
40
|
# File 'lib/fake_dynamo/item.rb', line 33
def as_hash
result = {}
result.merge!(key.as_hash)
@attributes.each do |name, attribute|
result.merge!(attribute.as_hash)
end
result
end
|
#available_actions ⇒ Object
61
62
63
|
# File 'lib/fake_dynamo/item.rb', line 61
def available_actions
%w[ PUT ADD DELETE ]
end
|
#delete(name, value) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/fake_dynamo/item.rb', line 69
def delete(name, value)
if not value
attributes.delete(name)
elsif old_attribute = attributes[name]
validate_type(value, old_attribute)
unless ["SS", "NS"].include? old_attribute.type
raise ValidationException, "Action DELETE is not supported for type #{old_attribute.type}"
end
attribute = Attribute.from_hash(name, value)
old_attribute.value -= attribute.value
end
end
|
#put(name, value) ⇒ Object
65
66
67
|
# File 'lib/fake_dynamo/item.rb', line 65
def put(name, value)
attributes[name] = Attribute.from_hash(name, value)
end
|
#update(name, data) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/fake_dynamo/item.rb', line 42
def update(name, data)
if key[name]
raise ValidationException, "Cannot update attribute #{name}. This attribute is part of the key"
end
new_value = data['Value']
action = data['Action'] || 'PUT'
unless available_actions.include? action
raise ValidationException, "Unknown action '#{action}' in AttributeUpdates.#{name}"
end
if (not new_value) and action != 'DELETE'
raise ValidationException, "Only DELETE action is allowed when no attribute value is specified"
end
self.send(action.downcase, name, new_value)
end
|