Sha256: 5956a113c6b95535ffba9a435ff0b64b2eb4d771f11f286aa9c835a0686a9332

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

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

module DynamoidAdvancedWhere
  class QueryBuilder
    attr_accessor :klass, :root_node

    delegate :all, :each, to: :query_materializer

    def initialize(klass:, &blk)
      self.klass = klass
      self.root_node = Nodes::RootNode.new(klass: klass, &blk)
    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

  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
dynamoid_advanced_where-1.0.1 lib/dynamoid_advanced_where/query_builder.rb
dynamoid_advanced_where-1.0.0 lib/dynamoid_advanced_where/query_builder.rb
dynamoid-advanced-where-1.0.0 lib/dynamoid_advanced_where/query_builder.rb