Sha256: 6e245f8957acbc59460d002ca398a9a938eb36ada4fda85bd2f178cb28c81be4

Contents?: true

Size: 1.15 KB

Versions: 6

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

require 'forwardable'
require_relative './null_node'

module DynamoidAdvancedWhere
  module Nodes
    class RootNode < BaseNode
      extend Forwardable
      attr_accessor :klass, :child_node

      def initialize(klass:, &blk)
        self.klass = klass
        evaluate_block(blk) if blk
        self.child_node ||= NullNode.new
        freeze
      end

      def method_missing(method, *args, &blk)
        if allowed_field?(method)
          FieldNode.create_node(attr_config: klass.attributes[method], field_path: method)
        else
          super
        end
      end

      def respond_to_missing?(method, _i)
        allowed_field?(method)
      end

      def allowed_field?(method)
        klass.attributes.key?(method.to_sym)
      end

      private

      def evaluate_block(blk)
        self.child_node = if blk.arity.zero?
                            Dynamoid.logger.warn 'Using DynamoidAdvancedWhere builder without an argument is now deprecated'
                            instance_eval(&blk)
                          else
                            blk.call(self)
                       end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
dynamoid_advanced_where-1.3.0 lib/dynamoid_advanced_where/nodes/root_node.rb
dynamoid_advanced_where-1.2.0 lib/dynamoid_advanced_where/nodes/root_node.rb
dynamoid_advanced_where-1.1.0 lib/dynamoid_advanced_where/nodes/root_node.rb
dynamoid_advanced_where-1.0.1 lib/dynamoid_advanced_where/nodes/root_node.rb
dynamoid_advanced_where-1.0.0 lib/dynamoid_advanced_where/nodes/root_node.rb
dynamoid-advanced-where-1.0.0 lib/dynamoid_advanced_where/nodes/root_node.rb