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
-
#criteria ⇒ Array
readonly
The criteria used for filtering query data.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
True if the other object is a scope with matching type and criteria; otherwise false.
- #and(*args) ⇒ Object (also: #where)
-
#as_json ⇒ Hash{String=>Object}
A JSON-compatible representation of the scope.
- #debug ⇒ Object
-
#empty? ⇒ Boolean
True if the scope has no criteria; otherwise false.
- #initialize(criteria:, inverted: false, **options) ⇒ Object
-
#invert ⇒ Cuprum::Collections::Criteria
A copy of the scope with the #inverted? predicate flipped and the individual criteria negated.
-
#inverted? ⇒ Boolean
True if the scope is inverted; otherwise false.
- #or(*args) ⇒ Object
-
#type ⇒ Symbol
The scope type.
-
#with_criteria(criteria) ⇒ Scope
Creates a copy of the scope with the given criteria.
Instance Attribute Details
#criteria ⇒ Array
Returns 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.
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
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_json ⇒ Hash{String=>Object}
Returns 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 |
#debug ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/cuprum/collections/scopes/criteria.rb', line 89 def debug # :nocov: = "#{super} (#{criteria.count})" += ' (inverted)' if inverted? return if empty? criteria.reduce("#{}:") 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.
103 104 105 |
# File 'lib/cuprum/collections/scopes/criteria.rb', line 103 def empty? @criteria.empty? end |
#initialize(criteria:, inverted: false, **options) ⇒ Object
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 |
#invert ⇒ Cuprum::Collections::Criteria
Returns 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.
114 115 116 |
# File 'lib/cuprum/collections/scopes/criteria.rb', line 114 def inverted? @inverted end |
#and(hash = nil, &block) ⇒ Object #and(scope) ⇒ Object
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 |
#type ⇒ Symbol
Returns 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.
140 141 142 |
# File 'lib/cuprum/collections/scopes/criteria.rb', line 140 def with_criteria(criteria) dup.tap { |copy| copy.criteria = criteria } end |