Class: FakeDynamo::Attribute
- Inherits:
-
Object
- Object
- FakeDynamo::Attribute
- Defined in:
- lib/fake_dynamo/attribute.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(attribute) ⇒ Object
- #as_hash ⇒ Object
- #description ⇒ Object
- #eql?(attribute) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(name, value, type) ⇒ Attribute
constructor
A new instance of Attribute.
- #numeric(n) ⇒ Object
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 29 30 31 32 33 34 35 36 |
# File 'lib/fake_dynamo/attribute.rb', line 5 def initialize(name, value, type) @name, @value, @type = name, value, type if @type == 'B' and value @value = Base64.decode64(value) end if @type == 'BS' @value = value.map { |v| Base64.decode64(v) } end 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 ['NS', 'N'].include? @type Array(@value).each do |n| numeric(n) end 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 if name == '' raise ValidationException, 'Empty attribute name' end end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/fake_dynamo/attribute.rb', line 3 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
3 4 5 |
# File 'lib/fake_dynamo/attribute.rb', line 3 def type @type end |
#value ⇒ Object
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
90 91 92 |
# File 'lib/fake_dynamo/attribute.rb', line 90 def from_data(data) Attribute.new(data['AttributeName'], nil, data['AttributeType']) end |
.from_hash(name, hash) ⇒ Object
94 95 96 |
# File 'lib/fake_dynamo/attribute.rb', line 94 def from_hash(name, hash) Attribute.new(name, hash.values.first, hash.keys.first) end |
Instance Method Details
#<=>(other) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/fake_dynamo/attribute.rb', line 71 def <=>(other) if @type == 'N' @value.to_f <=> other.value.to_f else @value <=> other.value end end |
#==(attribute) ⇒ Object
65 66 67 68 69 |
# File 'lib/fake_dynamo/attribute.rb', line 65 def ==(attribute) @name == attribute.name && @type == attribute.type && (@type == 'N' ? @value.to_f == attribute.value.to_f : @value == attribute.value) end |
#as_hash ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/fake_dynamo/attribute.rb', line 53 def as_hash value = if @type == 'B' Base64.encode64(@value) elsif @type == 'BS' @value.map { |v| Base64.encode64(v) } else @value end { @name => { @type => value } } end |
#description ⇒ Object
46 47 48 49 50 51 |
# File 'lib/fake_dynamo/attribute.rb', line 46 def description { 'AttributeName' => name, 'AttributeType' => type } end |
#eql?(attribute) ⇒ Boolean
79 80 81 82 83 |
# File 'lib/fake_dynamo/attribute.rb', line 79 def eql?(attribute) return false unless attribute.kind_of? Attribute self == attribute end |
#hash ⇒ Object
85 86 87 |
# File 'lib/fake_dynamo/attribute.rb', line 85 def hash name.hash ^ (type == 'N' ? value.to_f : value).hash ^ type.hash end |
#numeric(n) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/fake_dynamo/attribute.rb', line 38 def numeric(n) begin Float(n) rescue raise ValidationException, "The parameter cannot be converted to a numeric value: #{n}" end end |