Sha256: c8095cd015a105a03737f24ef7760ed66d71dd42c6aa95e3eaa47e7ef2e474ef

Contents?: true

Size: 1.81 KB

Versions: 12

Compression:

Stored size: 1.81 KB

Contents

# =========================================================
# field array methods enables field array to chain
# operators after calling the field expression
# accessor
# =========================================================
module CloudSesame
	module Query
		module DSL
			module FieldArrayMethods

				# NOT
				# =======================================
				def not(*values)
					parents[1] = { klass: AST::Not, options: extract_options(values) }
				  insert values
				end

				alias_method :is_not, :not

				# NEAR
				# =======================================
				def near(*values)
					parents[0] = { klass: AST::Near, options: extract_options(values) }
				  insert values
				end

				alias_method :sloppy, :near

				# PREFIX
				# =======================================
				def prefix(*values)
				  parents[0] = { klass: AST::Prefix, options: extract_options(values) }
				  insert values
				end

				alias_method :start_with, :prefix
				alias_method :begin_with, :prefix

				def insert(values = [])
					values.each do |value|
						dsl_scope << create_parents(build_literal(value))
					end
					parents.clear unless values.empty?
				  dsl_return != dsl_scope ? dsl_return : self
				end

				private

				def extract_options(values)
					values[-1].is_a?(Hash) ? values.delete_at(-1) : {}
				end

				def create_parents(child)
					parents.compact.each do |parent|
						node = parent[:klass].new dsl_context, parent[:options]
						node << child
						child = node
					end
					child
				end

				def build_literal(value)
					if value.kind_of?(AST::SingleExpressionOperator) || value.is_a?(AST::Literal)
						value.is_for field, field_options
						value
					else
						AST::Literal.new field, value, field_options
					end
				end

				def field_options
				  dsl_context[:fields][field]
				end

			end
		end
	end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
CloudSesame-0.4.2 lib/cloud_sesame/query/dsl/field_array_methods.rb
CloudSesame-0.4.1 lib/cloud_sesame/query/dsl/field_array_methods.rb
CloudSesame-0.4.0 lib/cloud_sesame/query/dsl/field_array_methods.rb
CloudSesame-0.3.6 lib/cloud_sesame/query/dsl/field_array_methods.rb
CloudSesame-0.3.5 lib/cloud_sesame/query/dsl/field_array_methods.rb
CloudSesame-0.3.4 lib/cloud_sesame/query/dsl/field_array_methods.rb
CloudSesame-0.3.3 lib/cloud_sesame/query/dsl/field_array_methods.rb
CloudSesame-0.3.2 lib/cloud_sesame/query/dsl/field_array_methods.rb
CloudSesame-0.3.1 lib/cloud_sesame/query/dsl/field_array_methods.rb
CloudSesame-0.3.0 lib/cloud_sesame/query/dsl/field_array_methods.rb
CloudSesame-0.2.5 lib/cloud_sesame/query/dsl/field_array_methods.rb
CloudSesame-0.2.4 lib/cloud_sesame/query/dsl/field_array_methods.rb