Class: DynamoDbFramework::Query

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

Instance Method Summary collapse

Constructor Details

#initialize(table_name:, partition_key:, partition_value:, index_name: nil) ⇒ Query

Returns a new instance of Query.



4
5
6
7
8
9
10
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 4

def initialize(table_name:, partition_key:, partition_value:, index_name: nil)
  @table_name = table_name
  @partition_key = partition_key
  @partition_value = partition_value
  @index_name = index_name
  @parts = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



12
13
14
15
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 12

def method_missing(name)
  @parts << { type: :field, value: name }
  self
end

Instance Method Details

#andObject



47
48
49
50
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 47

def and
  @parts << { type: :and }
  self
end

#buildObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 64

def build
  @expression_string = ''
  @expression_params = {}

  counter = 0
  @parts.each do |p|
    case p[:type]
      when :field
        field_param = '#' + p[:value].to_s
        @expression_string += ' ' + field_param
        @expression_params[field_param] = p[:value].to_s
      when :condition
        param_name = ':p' + counter.to_s
        counter = counter + 1
        @expression_string += ' ' + p[:expression].to_s + ' ' + param_name
        @expression_params[param_name] = clean_value(p[:value])
      when :and
        @expression_string += ' and'
      when :or
        @expression_string += ' or'
      else
        raise 'Invalid query part'
    end
  end

  return @expression_string.strip, @expression_params
end

#clean_value(value) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 102

def clean_value(value)
  if value.is_a?(Time) || value.is_a?(DateTime)
    convert_date(value)
  else
    value
  end
end

#condition(expression:, value:) ⇒ Object



92
93
94
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 92

def condition(expression:, value:)
  @parts << { type: :condition, expression: expression, value: value }
end

#convert_date(value) ⇒ Object



96
97
98
99
100
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 96

def convert_date(value)
  klass = value.class
  return value.iso8601 if klass == DateTime
  return value.to_i if klass == Time
end

#eq(value) ⇒ Object



17
18
19
20
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 17

def eq(value)
  condition(expression: '==', value: value)
  self
end

#execute(store:, limit: nil, count: false) ⇒ Object



57
58
59
60
61
62
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 57

def execute(store:, limit: nil, count: false)
  build
  repository = DynamoDbFramework::Repository.new(store)
  repository.table_name = @table_name
  repository.query(@partition_key, @partition_value, nil, nil, @expression_string, @expression_params, @index_name, limit, count)
end

#gt(value) ⇒ Object



27
28
29
30
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 27

def gt(value)
  condition(expression: '>', value: value)
  self
end

#gt_eq(value) ⇒ Object



32
33
34
35
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 32

def gt_eq(value)
  condition(expression: '>=', value: value)
  self
end

#lt(value) ⇒ Object



37
38
39
40
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 37

def lt(value)
  condition(expression: '<', value: value)
  self
end

#lt_eq(value) ⇒ Object



42
43
44
45
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 42

def lt_eq(value)
  condition(expression: '<=', value: value)
  self
end

#not_eq(value) ⇒ Object



22
23
24
25
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 22

def not_eq(value)
  condition(expression: '!=', value: value)
  self
end

#orObject



52
53
54
55
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 52

def or
  @parts << { type: :or }
  self
end