Sha256: 4fbda32f2787537bad3b5ef06a113f373664f4ddd47338958b60c2da5440d273

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

require_relative './nodes'
require_relative './query_materializer'
require_relative './batched_updater'

module DynamoidAdvancedWhere
  class QueryBuilder
    attr_accessor :klass, :root_node, :start_hash

    delegate :all, :each, to: :query_materializer

    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

    def query_materializer
      QueryMaterializer.new(query_builder: self)
    end

    def batch_update
      BatchedUpdater.new(query_builder: self)
    end

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

    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
    alias and where

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dynamoid_advanced_where-1.2.0 lib/dynamoid_advanced_where/query_builder.rb
dynamoid_advanced_where-1.1.0 lib/dynamoid_advanced_where/query_builder.rb