Module: ActiveEnumerable::EnglishDsl

Includes:
ScopeMethod, Where
Included in:
ActiveEnumerable
Defined in:
lib/active_enumerable/english_dsl.rb

Constant Summary collapse

UnmetCondition =
Class.new(StandardError)

Instance Method Summary collapse

Methods included from Where

#where_conditions

Methods included from ScopeMethod

#scope

Instance Method Details

#and(matches = nil) ⇒ Object

Parameters:

  • (defaults to: nil)

    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)
    


54
55
56
57
58
59
60
# 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) ⇒ Object

Parameters:

  • is either a method name a Hash key. has(:name)



22
23
24
25
# File 'lib/active_enumerable/english_dsl.rb', line 22

def has(attr)
  all_conditions << [attr]
  self
end

#of(matches = nil, &block) ⇒ Object

Raises:

Parameters:

  • (defaults to: nil)

    is list of sub conditions or associations to query. Or this can by any value to compare the result from attr.



29
30
31
32
33
34
35
36
37
# 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) ⇒ Object

After calling #of(matches) providing additional matches to #or(matches) build a either or query.

Raises:

Parameters:

  • is list of sub conditions or associations to query. Or this can by any value to compare the result from attr.



42
43
44
45
46
47
48
# 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) { ... } ⇒ ActiveEnumerable

Yields:

  • takes block to evaluate English Dsl <ActiveEnumerable>#where{ has(:name).of(“Dustin”) }

Parameters:

Returns:



12
13
14
15
16
17
18
# 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