Class: FakeDynamo::Key

Inherits:
Object
  • Object
show all
Extended by:
Validation
Defined in:
lib/fake_dynamo/key.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

Instance Attribute Details

#primaryObject

Returns the value of attribute primary.



5
6
7
# File 'lib/fake_dynamo/key.rb', line 5

def primary
  @primary
end

#rangeObject

Returns the value of attribute range.



5
6
7
# File 'lib/fake_dynamo/key.rb', line 5

def range
  @range
end

Class Method Details

.create_attribute(key, data) ⇒ Object



30
31
32
33
34
# File 'lib/fake_dynamo/key.rb', line 30

def create_attribute(key, data)
  name = key.name
  attr = Attribute.from_hash(name, data[name])
  attr
end

.from_data(key_data, key_schema) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/fake_dynamo/key.rb', line 8

def from_data(key_data, key_schema)
  key = Key.new
  validate_key_data(key_data, key_schema)
  key.primary = Attribute.from_hash(key_schema.hash_key.name, key_data['HashKeyElement'])

  if key_schema.range_key
    key.range = Attribute.from_hash(key_schema.range_key.name, key_data['RangeKeyElement'])
  end
  key
end

.from_schema(data, key_schema) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/fake_dynamo/key.rb', line 19

def from_schema(data, key_schema)
  key = Key.new
  validate_key_schema(data, key_schema)
  key.primary = create_attribute(key_schema.hash_key, data)

  if key_schema.range_key
    key.range = create_attribute(key_schema.range_key, data)
  end
  key
end

Instance Method Details

#[](name) ⇒ Object



37
38
39
40
41
# File 'lib/fake_dynamo/key.rb', line 37

def [](name)
  return @primary if @primary.name == name
  return @range if @range and @range.name == name
  nil
end

#as_hashObject



54
55
56
57
58
59
60
# File 'lib/fake_dynamo/key.rb', line 54

def as_hash
  result = @primary.as_hash
  if @range
    result.merge!(@range.as_hash)
  end
  result
end

#as_key_hashObject



62
63
64
65
66
67
68
# File 'lib/fake_dynamo/key.rb', line 62

def as_key_hash
  result = { 'HashKeyElement' => { @primary.type => @primary.value }}
  if @range
    result.merge!({'RangeKeyElement' => { @range.type => @range.value }})
  end
  result
end

#eql?(key) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'lib/fake_dynamo/key.rb', line 43

def eql?(key)
  return false unless key.kind_of? Key

  @primary == key.primary &&
    @range == key.range
end

#hashObject



50
51
52
# File 'lib/fake_dynamo/key.rb', line 50

def hash
  primary.hash ^ range.hash
end