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
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fake_dynamo/attribute.rb', line 5

def initialize(name, value, type)
  @name, @value, @type = name, value, type

  if ['NS', 'SS'].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 ['NS', 'N'].include? @type
    Array(@value).each do |n|
      numeric(n)
    end
  end

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

  if name == ''
    raise ValidationException, 'Empty attribute name'
  end
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



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

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

.from_hash(name, hash) ⇒ Object



78
79
80
# File 'lib/fake_dynamo/attribute.rb', line 78

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

Instance Method Details

#<=>(other) ⇒ Object



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

def <=>(other)
  if @type == 'N'
    @value.to_f <=> other.value.to_f
  else
    @value <=> other.value
  end
end

#==(attribute) ⇒ Object



49
50
51
52
53
# File 'lib/fake_dynamo/attribute.rb', line 49

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

#as_hashObject



45
46
47
# File 'lib/fake_dynamo/attribute.rb', line 45

def as_hash
  { @name => { @type => @value } }
end

#descriptionObject



38
39
40
41
42
43
# File 'lib/fake_dynamo/attribute.rb', line 38

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

#eql?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  self == attribute
end

#hashObject



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

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

#numeric(n) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/fake_dynamo/attribute.rb', line 30

def numeric(n)
  begin
    Float(n)
  rescue
    raise ValidationException, "The parameter cannot be converted to a numeric value: #{n}"
  end
end