1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
module Elasticsearch
module DSL
module Search
# Module containing common functionality for DSL classes
#
module BaseComponent
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
def initialize(*args, &block)
@hash = { name => {} }
@args = args.first || {}
@options = args.size > 1 ? args.last : {}
@block = block
end
module ClassMethods
# Get or set the name for the DSL class
#
# @example Set the index name for the `Article` model and re-evaluate it on each call
#
# class MyQuery
# include BaseComponent
# name :my_special_query
# end
#
# MyQuery.name
# # => :my_special_query
#
def name(value=nil)
if value
@name = value.to_sym
else
@name ||= begin
value = self.to_s.split('::').last
value.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
value.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
value.tr!("-", "_")
value.downcase!
value.to_sym
end
end
end
# Set the name for the DSL class
#
def name=(value)
@name = value.to_sym
end
def option_methods
@option_methods ||= []
end
end
module InstanceMethods
# Return the name for instance of the DSL class
#
# @return [String]
#
def name
self.class.name
end
# Evaluates any block passed to the query
#
# @return [self]
#
def call
@block.arity < 1 ? self.instance_eval(&@block) : @block.call(self) if @block
self
end
# Return true when the component definition is empty
#
def empty?
to_hash[name].respond_to?(:empty?) && to_hash[name].empty?
end
# Convert the query definition to a Hash
#
# A default implementation, DSL classes can overload it.
#
# @return [Hash]
#
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
end
end
end
end
end
|