Class: DynamoDbFramework::AttributesBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamodb_framework/dynamodb_attributes_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAttributesBuilder

Returns a new instance of AttributesBuilder.



5
6
7
# File 'lib/dynamodb_framework/dynamodb_attributes_builder.rb', line 5

def initialize
  @attributes = []
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/dynamodb_framework/dynamodb_attributes_builder.rb', line 3

def attributes
  @attributes
end

Instance Method Details

#add(*options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dynamodb_framework/dynamodb_attributes_builder.rb', line 51

def add(*options)

  if options.length == 1 && options[0].is_a?(Hash)
    add_from_hash(options[0])
  elsif options.length == 3
    add_from_args(options[0], options[1], options[2])
  else
    add_from_args(options[0], options[1])
  end

end

#add_from_args(name, type, key = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dynamodb_framework/dynamodb_attributes_builder.rb', line 9

def add_from_args(name, type, key = nil)
  type_symbol = :S
  if type == 'number' || type == :N || type == :number
    type_symbol = :N
  elsif type == 'binary' || type == :B || type == :binary
    type_symbol = :B
  elsif type == 'bool' || type == 'boolean' || type == :BOOL || type == :bool || type == :boolean
    type_symbol = :BOOL
  end

  if key == 'hash' || key == :hash || key == 'partition' || key == :partition
    key = :hash
  elsif key == 'range' || key == :range
    key = :range
  else
    key = nil
  end

  @attributes.push({ :name => name, :type => type_symbol, :key => key })
end

#add_from_hash(options) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dynamodb_framework/dynamodb_attributes_builder.rb', line 30

def add_from_hash(options)
  type_symbol = :S
  if options[:type] == 'number' || options[:type] == :N || options[:type] == :number
    type_symbol = :N
  elsif options[:type] == 'binary' || options[:type] == :B || options[:type] == :binary
    type_symbol = :B
  elsif options[:type] == 'bool' || options[:type] == 'boolean' || options[:type] == :BOOL || options[:type] == :bool || options[:type] == :boolean
    type_symbol = :BOOL
  end

  if options[:key] == 'hash' || options[:key] == :hash || options[:key] == 'partition' || options[:key] == :partition
    key = :hash
  elsif options[:key] == 'range' || options[:key] == :range
    key = :range
  else
    key = nil
  end

  @attributes.push({ :name => options[:name], :type => type_symbol, :key => key })
end

#contains(name:) ⇒ Object



63
64
65
# File 'lib/dynamodb_framework/dynamodb_attributes_builder.rb', line 63

def contains(name:)
  @attributes.detect { |a| a[:name] == name } != nil
end