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, #key_schema_mismatch, #param, #validate!, #validate_hash_condition, #validate_hash_key, #validate_index_names, #validate_input, #validate_key_data, #validate_key_schema, #validate_operation, #validate_payload, #validate_projection, #validate_range_condition, #validate_range_key, #validate_request_size, #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, attribute_definitions) ⇒ Object
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/fake_dynamo/item.rb', line 7
def from_data(data, key_schema, attribute_definitions)
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.validate_attribute_types(attribute_definitions)
item
end
|
.from_key(key) ⇒ Object
22
23
24
25
26
27
|
# File 'lib/fake_dynamo/item.rb', line 22
def from_key(key)
item = Item.new
item.key = key
item.attributes = {}
item
end
|
Instance Method Details
#[](name) ⇒ Object
31
32
33
|
# File 'lib/fake_dynamo/item.rb', line 31
def [](name)
attributes[name] or key[name]
end
|
#add(name, value) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/fake_dynamo/item.rb', line 91
def add(name, value)
attribute = Attribute.from_hash(name, value)
unless ["N", "SS", "NS", "BS"].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.add(attribute.value)
else
old_attribute.value += attribute.value
old_attribute.value.uniq!
end
else
attributes[name] = attribute
end
end
|
#as_hash ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'lib/fake_dynamo/item.rb', line 35
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
67
68
69
|
# File 'lib/fake_dynamo/item.rb', line 67
def available_actions
%w[ PUT ADD DELETE ]
end
|
#collection_metrics(data) ⇒ Object
112
113
114
115
116
117
118
119
120
|
# File 'lib/fake_dynamo/item.rb', line 112
def collection_metrics(data)
if data['ReturnItemCollectionMetrics'] == 'SIZE'
{ 'ItemCollectionMetrics' =>
{ 'ItemCollectionKey' => key.primary.as_hash,
'SizeEstimateRangeGB' => [ 0, 1 ] } }
else
{}
end
end
|
#delete(name, value) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/fake_dynamo/item.rb', line 75
def delete(name, value)
if not value
attributes.delete(name)
elsif old_attribute = attributes[name]
validate_type(value, old_attribute)
unless ["SS", "NS", "BS"].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
if old_attribute.value.empty?
attributes.delete(name)
end
end
end
|
#put(name, value) ⇒ Object
71
72
73
|
# File 'lib/fake_dynamo/item.rb', line 71
def put(name, value)
attributes[name] = Attribute.from_hash(name, value)
end
|
#to_json ⇒ Object
44
45
46
|
# File 'lib/fake_dynamo/item.rb', line 44
def to_json(*)
as_hash.to_json
end
|
#update(name, data) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/fake_dynamo/item.rb', line 48
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
|
#validate_attribute_types(definitions) ⇒ Object
122
123
124
125
126
127
128
|
# File 'lib/fake_dynamo/item.rb', line 122
def validate_attribute_types(definitions)
definitions.each do |definition|
if attr = self[definition.name]
validate_type(attr.as_hash[definition.name], definition)
end
end
end
|