Class: DynamoidAdvancedWhere::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamoid_advanced_where/query_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass:, start_hash: nil, root_node: nil, &blk) ⇒ QueryBuilder

Returns a new instance of QueryBuilder.



11
12
13
14
15
16
17
# File 'lib/dynamoid_advanced_where/query_builder.rb', line 11

def initialize(klass:, start_hash: nil, root_node: nil, &blk)
  self.klass = klass
  self.root_node = root_node || Nodes::RootNode.new(klass: klass, &blk)
  self.start_hash = start_hash

  freeze
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



7
8
9
# File 'lib/dynamoid_advanced_where/query_builder.rb', line 7

def klass
  @klass
end

#root_nodeObject

Returns the value of attribute root_node.



7
8
9
# File 'lib/dynamoid_advanced_where/query_builder.rb', line 7

def root_node
  @root_node
end

#start_hashObject

Returns the value of attribute start_hash.



7
8
9
# File 'lib/dynamoid_advanced_where/query_builder.rb', line 7

def start_hash
  @start_hash
end

Instance Method Details

#batch_updateObject



23
24
25
# File 'lib/dynamoid_advanced_where/query_builder.rb', line 23

def batch_update
  BatchedUpdater.new(query_builder: self)
end

#query_materializerObject



19
20
21
# File 'lib/dynamoid_advanced_where/query_builder.rb', line 19

def query_materializer
  QueryMaterializer.new(query_builder: self)
end

#start(key_hash) ⇒ Object



49
50
51
52
53
# File 'lib/dynamoid_advanced_where/query_builder.rb', line 49

def start(key_hash)
  return self if key_hash.nil? || key_hash.empty?

  self.class.new(klass: klass, start_hash: key_hash, root_node: root_node)
end

#upsert(*args) ⇒ Object



27
28
29
30
# File 'lib/dynamoid_advanced_where/query_builder.rb', line 27

def upsert(*args)
  update_fields = args.extract_options!
  batch_update.set_values(update_fields).apply(*args)
end

#where(other_builder = nil, &blk) ⇒ Object Also known as: and



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dynamoid_advanced_where/query_builder.rb', line 32

def where(other_builder = nil, &blk)
  raise 'cannot use a block and an argument' if other_builder && blk

  other_builder = self.class.new(klass: klass, &blk) if blk

  raise 'passed argument must be a query builder' unless other_builder.is_a?(self.class)

  local_root_node = root_node
  self.class.new(klass: klass) do
    Nodes::AndNode.new(
      other_builder.root_node.child_node,
      local_root_node.child_node
    )
  end
end