module Elasticsearch::DSL::Search::BaseComponent
Module containing common functionality for DSL
classes
Public Class Methods
included(base)
click to toggle source
# File lib/elasticsearch/dsl/search/base_component.rb, line 25 def self.included(base) base.__send__ :extend, ClassMethods base.__send__ :include, InstanceMethods base.instance_eval do # Defines an "inner" method for DSL classes # # @example Define a method `bar` for the MyQuery class which updates the query definition # # class MyQuery # include BaseComponent # # option_method :bar # end # # q = MyQuery.new :foo do # bar 'TEST' # end # # q.to_hash # # => {:myquery=>{:foo=>{:bar=>"TEST"}}} # # @example Define a method `bar` with custom logic for updating the Hash with query definition # # class MyCustomQuery # include BaseComponent # # option_method :bar, lambda { |*args| @hash[self.name.to_sym][@args].update custom: args.pop } # end # # q = MyCustomQuery.new :foo do # bar 'TEST' # end # # q.to_hash # # => {:mycustomquery=>{:foo=>{:custom=>"TEST"}}} # def option_method(name, block=nil) option_methods << name if block self.__send__ :define_method, name, &block else self.__send__ :define_method, name do |*args| # 1. Component has empty @args (ie. no user supplied name or @hash value) if @args && @args.respond_to?(:to_hash) && @args.empty? @hash[self.name.to_sym].update name.to_sym => args.first # 2. Component user-supplied name or @hash value passed in @args else @hash[self.name.to_sym] = { @args => {} } unless @hash[self.name.to_sym][@args] @hash[self.name.to_sym][@args].update name.to_sym => args.first end end end end end end
new(*args, &block)
click to toggle source
# File lib/elasticsearch/dsl/search/base_component.rb, line 82 def initialize(*args, &block) @hash = { name => {} } @args = args.first || {} @options = args.size > 1 ? args.last : {} @block = block end
Public Instance Methods
option_method(name, block=nil)
click to toggle source
Defines an “inner” method for DSL
classes
@example Define a method `bar` for the MyQuery class which updates the query definition class MyQuery include BaseComponent option_method :bar end q = MyQuery.new :foo do bar 'TEST' end q.to_hash # => {:myquery=>{:foo=>{:bar=>"TEST"}}} @example Define a method `bar` with custom logic for updating the Hash with query definition class MyCustomQuery include BaseComponent option_method :bar, lambda { |*args| @hash[self.name.to_sym][@args].update custom: args.pop } end q = MyCustomQuery.new :foo do bar 'TEST' end q.to_hash # => {:mycustomquery=>{:foo=>{:custom=>"TEST"}}}
# File lib/elasticsearch/dsl/search/base_component.rb, line 62 def option_method(name, block=nil) option_methods << name if block self.__send__ :define_method, name, &block else self.__send__ :define_method, name do |*args| # 1. Component has empty @args (ie. no user supplied name or @hash value) if @args && @args.respond_to?(:to_hash) && @args.empty? @hash[self.name.to_sym].update name.to_sym => args.first # 2. Component user-supplied name or @hash value passed in @args else @hash[self.name.to_sym] = { @args => {} } unless @hash[self.name.to_sym][@args] @hash[self.name.to_sym][@args].update name.to_sym => args.first end end end end