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
- #decode(value) ⇒ Object
- #description ⇒ Object
- #encode(value) ⇒ Object
- #eql?(attribute) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(name, value, type) ⇒ Attribute
constructor
A new instance of Attribute.
- #validate_name! ⇒ Object
- #validate_value! ⇒ Object
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
#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
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_hash ⇒ Object
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 |
#description ⇒ Object
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
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 |
#hash ⇒ Object
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 |