module ActiveEnumerable::EnglishDsl

Constants

UnmetCondition

Public Instance Methods

and(matches=nil) click to toggle source

@param [Hash, Object, NilClass] matches is list of sub conditions or associations to query.

Or this can by any value to compare the result from attr.
Or passing nothing and provide a different has(attr)
  has(attr).of(matches).and.has(other_attr)
# File lib/active_enumerable/english_dsl.rb, line 54
def and(matches=nil)
  if matches
    all_conditions.last[1].merge!(matches)
    evaluation_results << english_where
  end
  self
end
has(attr) click to toggle source

@param [String, Symbol] attr is either a method name a Hash key.

has(:name)
# File lib/active_enumerable/english_dsl.rb, line 22
def has(attr)
  all_conditions << [attr]
  self
end
of(matches=nil, &block) click to toggle source

@param [Hash, Object] matches is list of sub conditions or associations to query.

Or this can by any value to compare the result from attr.
# File lib/active_enumerable/english_dsl.rb, line 29
def of(matches=nil, &block)
  raise ArgumentError if matches.nil? && block.nil?
  if all_conditions.empty? || !(all_conditions.last.count == 1)
    raise UnmetCondition, ".has(attr) must be call before calling #of."
  else
    all_conditions.last << (matches || block)
    self
  end
end
or(matches) click to toggle source

After calling of(matches) providing additional matches to or(matches) build a either or query. @param [Hash, Object] matches is list of sub conditions or associations to query.

Or this can by any value to compare the result from attr.
# File lib/active_enumerable/english_dsl.rb, line 42
def or(matches)
  raise UnmetCondition, ".has(attr).of(matches) must be call before calling #or(matches)." if all_conditions.empty? || !(all_conditions.last.count == 2)
  evaluation_results << english_where
  all_conditions.last[1] = matches
  evaluation_results << english_where
  self
end
where(*args, &block) click to toggle source

@param [Hash] @yield takes block to evaluate English Dsl

<ActiveEnumerable>#where{ has(:name).of("Dustin") }

@return [ActiveEnumerable]

Calls superclass method ActiveEnumerable::Where#where
# File lib/active_enumerable/english_dsl.rb, line 12
def where(*args, &block)
  if block_given?
    scope(&block).send(:_english_eval__)
  else
    super
  end
end

Private Instance Methods

_english_eval__() click to toggle source
# File lib/active_enumerable/english_dsl.rb, line 68
def _english_eval__
  if evaluation_results.empty?
    english_where
  else
    __new_relation__ evaluation_results.flat_map(&:to_a).uniq
  end
end
all_conditions() click to toggle source
# File lib/active_enumerable/english_dsl.rb, line 64
def all_conditions
  @all_conditions ||= []
end
english_where() click to toggle source
# File lib/active_enumerable/english_dsl.rb, line 76
def english_where
  where all_conditions.each_with_object({}) { |e, h| h[e[0]] = e[1] }
end
evaluation_results() click to toggle source
# File lib/active_enumerable/english_dsl.rb, line 80
def evaluation_results
  @evaluation_results ||= []
end