Module: Elasticsearch::DSL::Search::BaseComponent::InstanceMethods

Defined in:
lib/elasticsearch/dsl/search/base_component.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



172
173
174
175
176
177
178
# File 'lib/elasticsearch/dsl/search/base_component.rb', line 172

def method_missing(name, *args, &block)
  if @block
    @block.binding.eval('self').send(name, *args, &block)
  else
    super
  end
end

Instance Method Details

#callself

Evaluates any block passed to the query

Returns:

  • (self)


130
131
132
133
134
135
# File 'lib/elasticsearch/dsl/search/base_component.rb', line 130

def call
  if @block
    @block.arity < 1 ? self.instance_eval(&@block) : @block.call(self)
  end
  self
end

#empty?Boolean

Return true when the component definition is empty

Returns:

  • (Boolean)


139
140
141
# File 'lib/elasticsearch/dsl/search/base_component.rb', line 139

def empty?
  to_hash[name].respond_to?(:empty?) && to_hash[name].empty?
end

#nameString

Return the name for instance of the DSL class

Returns:

  • (String)


122
123
124
# File 'lib/elasticsearch/dsl/search/base_component.rb', line 122

def name
  self.class.name
end

#to_hash(options = {}) ⇒ Hash

Convert the query definition to a Hash

A default implementation, DSL classes can overload it.

Returns:

  • (Hash)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/elasticsearch/dsl/search/base_component.rb', line 149

def to_hash(options={})
  case
    # 1. Create hash from the block
    when @block
      @hash = (@args && ! @args.empty?) ? { name => { @args => {} } } : { name => {} }
      call
      @hash[self.name.to_sym].update @options unless @options.empty?
      @hash
    # 2. Hash created with option methods
    when @hash[self.name.to_sym] && ! @args.is_a?(Hash) && @hash[self.name.to_sym][@args]
      @hash[self.name.to_sym].update @options unless @options.empty?
      @hash
    # 3. Hash passsed as @args
    when @hash[self.name.to_sym] && @args.respond_to?(:to_hash) && ! @args.empty?
      { name => @args.to_hash }
    # 4. Hash already built
    else
      @hash
  end
end