Class: FakeDynamo::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/fake_dynamo/attribute.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, type) ⇒ Attribute

Returns a new instance of Attribute.



5
6
7
8
9
10
11
12
# File 'lib/fake_dynamo/attribute.rb', line 5

def initialize(name, value, type)
  @name, @type = name, type
  validate_name!
  return unless value

  @value = decode(value)
  validate_value!
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/fake_dynamo/attribute.rb', line 3

def name
  @name
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/fake_dynamo/attribute.rb', line 3

def type
  @type
end

#valueObject

Returns the value of attribute value.



3
4
5
# File 'lib/fake_dynamo/attribute.rb', line 3

def value
  @value
end

Class Method Details

.from_data(data) ⇒ Object



85
86
87
# File 'lib/fake_dynamo/attribute.rb', line 85

def from_data(data)
  Attribute.new(data['AttributeName'], nil, data['AttributeType'])
end

.from_hash(name, hash) ⇒ Object



89
90
91
# File 'lib/fake_dynamo/attribute.rb', line 89

def from_hash(name, hash)
  Attribute.new(name, hash.values.first, hash.keys.first)
end

Instance Method Details

#<=>(other) ⇒ Object



70
71
72
# File 'lib/fake_dynamo/attribute.rb', line 70

def <=>(other)
  @value <=> other.value
end

#==(attribute) ⇒ Object



64
65
66
67
68
# File 'lib/fake_dynamo/attribute.rb', line 64

def ==(attribute)
  @name == attribute.name &&
    @type == attribute.type &&
    @value == attribute.value
end

#as_hashObject



60
61
62
# File 'lib/fake_dynamo/attribute.rb', line 60

def as_hash
  { @name => { @type => encode(@value) } }
end

#decode(value) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/fake_dynamo/attribute.rb', line 40

def decode(value)
  case @type
  when 'B' then Base64.strict_decode64(value)
  when 'BS' then value.map { |v| Base64.strict_decode64(v) }
  when 'N' then Num.new(value)
  when 'NS' then value.map { |v| Num.new(v) }
  else value
  end
end

#descriptionObject



33
34
35
36
37
38
# File 'lib/fake_dynamo/attribute.rb', line 33

def description
  {
    'AttributeName' => name,
    'AttributeType' => type
  }
end

#encode(value) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/fake_dynamo/attribute.rb', line 50

def encode(value)
  case @type
  when 'B' then Base64.strict_encode64(value)
  when 'BS' then value.map { |v| Base64.strict_encode64(v) }
  when 'N' then value.to_s
  when 'NS' then value.map(&:to_s)
  else value
  end
end

#eql?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
# File 'lib/fake_dynamo/attribute.rb', line 74

def eql?(attribute)
  return false unless attribute.kind_of? Attribute

  self == attribute
end

#hashObject



80
81
82
# File 'lib/fake_dynamo/attribute.rb', line 80

def hash
  name.hash ^ value.hash ^ type.hash
end

#validate_name!Object



14
15
16
17
18
# File 'lib/fake_dynamo/attribute.rb', line 14

def validate_name!
  if name == ''
    raise ValidationException, 'Empty attribute name'
  end
end

#validate_value!Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fake_dynamo/attribute.rb', line 20

def validate_value!
  if ['NS', 'SS', 'BS'].include? @type
    raise ValidationException, 'An AttributeValue may not contain an empty set' if @value.empty?
    raise ValidationException, 'Input collection contains duplicates' if value.uniq!
  end

  if ['S', 'SS', 'S', 'BS'].include? @type
    Array(@value).each do |v|
      raise ValidationException, 'An AttributeValue may not contain an empty string or empty binary' if v == ''
    end
  end
end