Module: Cuprum::Collections::Scopes::Criteria

Included in:
Basic::Scopes::CriteriaScope, CriteriaScope
Defined in:
lib/cuprum/collections/scopes/criteria.rb,
lib/cuprum/collections/scopes/criteria/parser.rb

Overview

Functionality for implementing a criteria scope.

Defined Under Namespace

Modules: ClassMethods Classes: Parser

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#criteriaArray

Returns the criteria used for filtering query data.

Returns:

  • (Array)

    the criteria used for filtering query data.



59
60
61
# File 'lib/cuprum/collections/scopes/criteria.rb', line 59

def criteria
  @criteria
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if the other object is a scope with matching type and criteria; otherwise false.

Parameters:

  • other (Object)

    the object to compare.

Returns:

  • (Boolean)

    true if the other object is a scope with matching type and criteria; otherwise false.



65
66
67
68
69
# File 'lib/cuprum/collections/scopes/criteria.rb', line 65

def ==(other)
  return false unless super

  other.criteria == criteria && other.inverted? == inverted?
end

#and(hash = nil, &block) ⇒ Object #and(scope) ⇒ Object Also known as: where

Overloads:

  • #and(hash = nil, &block) ⇒ Object

    Parses the hash or block and combines using a logical AND.

    See Also:

    • Parser#parse.
  • #and(scope) ⇒ Object

    Combines with the current scope using a logical AND.

    Returns self if the given scope is empty.



72
73
74
75
76
77
78
79
80
# File 'lib/cuprum/collections/scopes/criteria.rb', line 72

def and(*args, &)
  return super if scope?(args.first)

  return self.class.build(*args, &) if empty?

  return super if inverted?

  with_criteria([*criteria, *self.class.parse(*args, &)])
end

#as_jsonHash{String=>Object}

Returns a JSON-compatible representation of the scope.

Returns:

  • (Hash{String=>Object})

    a JSON-compatible representation of the scope.



84
85
86
# File 'lib/cuprum/collections/scopes/criteria.rb', line 84

def as_json
  super.merge({ 'criteria' => criteria, 'inverted' => inverted? })
end

#debugObject



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cuprum/collections/scopes/criteria.rb', line 89

def debug
  # :nocov:
  message = "#{super} (#{criteria.count})"
  message += ' (inverted)' if inverted?

  return message if empty?

  criteria.reduce("#{message}:") do |str, (attribute, operator, value)|
    str + "\n- #{attribute.inspect} #{operator} #{value.inspect}"
  end
  # :nocov:
end

#empty?Boolean

Returns true if the scope has no criteria; otherwise false.

Returns:

  • (Boolean)

    true if the scope has no criteria; otherwise false.



103
104
105
# File 'lib/cuprum/collections/scopes/criteria.rb', line 103

def empty?
  @criteria.empty?
end

#initialize(criteria:, inverted: false, **options) ⇒ Object

Parameters:

  • criteria (Array)

    the criteria used for filtering query data.

  • inverted (Boolean) (defaults to: false)

    if true, the criteria are inverted and should match on any criterion (per DeMorgan's Laws).

  • options (Hash)

    additional options for the scope.



51
52
53
54
55
56
# File 'lib/cuprum/collections/scopes/criteria.rb', line 51

def initialize(criteria:, inverted: false, **)
  super(**)

  @criteria = criteria
  @inverted = inverted
end

#invertCuprum::Collections::Criteria

Returns a copy of the scope with the #inverted? predicate flipped and the individual criteria negated.

Returns:

  • (Cuprum::Collections::Criteria)

    a copy of the scope with the #inverted? predicate flipped and the individual criteria negated.



109
110
111
# File 'lib/cuprum/collections/scopes/criteria.rb', line 109

def invert
  with_criteria(invert_criteria).tap { |copy| copy.inverted = !inverted? }
end

#inverted?Boolean

Returns true if the scope is inverted; otherwise false.

Returns:

  • (Boolean)

    true if the scope is inverted; otherwise false.



114
115
116
# File 'lib/cuprum/collections/scopes/criteria.rb', line 114

def inverted?
  @inverted
end

#and(hash = nil, &block) ⇒ Object #and(scope) ⇒ Object

Overloads:

  • #and(hash = nil, &block) ⇒ Object

    Parses the hash or block and combines using a logical OR.

    See Also:

    • Parser#parse.
  • #and(scope) ⇒ Object

    Combines with the current scope using a logical OR.

    Returns self if the given scope is empty.



119
120
121
122
123
124
125
126
127
128
# File 'lib/cuprum/collections/scopes/criteria.rb', line 119

def or(*args, &)
  return super if scope?(args.first)

  return self.class.build(*args, &) if empty?

  builder.build_disjunction_scope(
    safe:   false,
    scopes: [self, self.class.build(*args, &)]
  )
end

#typeSymbol

Returns the scope type.

Returns:

  • (Symbol)

    the scope type.



131
132
133
# File 'lib/cuprum/collections/scopes/criteria.rb', line 131

def type
  :criteria
end

#with_criteria(criteria) ⇒ Scope

Creates a copy of the scope with the given criteria.

Parameters:

  • criteria (Array)

    the criteria used for filtering query data.

Returns:

  • (Scope)

    the copied scope.



140
141
142
# File 'lib/cuprum/collections/scopes/criteria.rb', line 140

def with_criteria(criteria)
  dup.tap { |copy| copy.criteria = criteria }
end