Module: Cuprum::Collections::Scopes::Building

Included in:
Basic::Scopes::Builder, Builder
Defined in:
lib/cuprum/collections/scopes/building.rb

Overview

Abstraction for generating scopes for a given collection.

Defined Under Namespace

Modules: ClassMethods Classes: AbstractBuilderError, UnknownScopeTypeError

Instance Method Summary collapse

Instance Method Details

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

Overloads:

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

    Parses the hash or block and returns a criteria scope.

    See Also:

    • Criteria::Parser#parse.
  • #build(scope) ⇒ Object

    Returns a new scope with the same scope type and properties.



41
42
43
44
45
46
47
# File 'lib/cuprum/collections/scopes/building.rb', line 41

def build(*args, &)
  if args.first.is_a?(Cuprum::Collections::Scopes::Base)
    return transform_scope(scope: args.first)
  end

  criteria_scope_class.build(*args, &)
end

#build_all_scopeObject

Creates a new all scope.



50
51
52
# File 'lib/cuprum/collections/scopes/building.rb', line 50

def build_all_scope
  all_scope_class.new
end

#build_conjunction_scope(scopes:, safe: true) ⇒ Object

Creates a new logical AND scope wrapping the given scopes.

Parameters:

  • scopes (Array<Cuprum::Collections::Scopes::Base>)

    the scopes to wrap in an AND scope.

  • safe (Boolean) (defaults to: true)

    if true, validates and converts the scopes to match the builder's scope classes. Defaults to true.



60
61
62
63
64
65
66
67
68
# File 'lib/cuprum/collections/scopes/building.rb', line 60

def build_conjunction_scope(scopes:, safe: true)
  if safe
    validate_scopes!(scopes)

    scopes = transform_scopes(scopes)
  end

  conjunction_scope_class.new(scopes:)
end

#build_criteria_scope(criteria:, inverted: false, safe: true) ⇒ Object

Creates a new scope wrapping the given criteria.

Parameters:

  • criteria (Array)

    the criteria for the scope.

  • inverted (Boolean) (defaults to: false)

    true if the criteria scope is inverted.

  • safe (Boolean) (defaults to: true)

    if true, validates the criteria. Defaults to true.



75
76
77
78
79
# File 'lib/cuprum/collections/scopes/building.rb', line 75

def build_criteria_scope(criteria:, inverted: false, safe: true)
  validate_criteria!(criteria) if safe

  criteria_scope_class.new(criteria:, inverted:)
end

#build_disjunction_scope(scopes:, safe: true) ⇒ Object

Creates a new logical OR scope wrapping the given scopes.

Parameters:

  • scopes (Array<Cuprum::Collections::Scopes::Base>)

    the scopes to wrap in an AND scope.

  • safe (Boolean) (defaults to: true)

    if true, validates and converts the scopes to match the builder's scope classes. Defaults to true.



87
88
89
90
91
92
93
94
95
# File 'lib/cuprum/collections/scopes/building.rb', line 87

def build_disjunction_scope(scopes:, safe: true)
  if safe
    validate_scopes!(scopes)

    scopes = transform_scopes(scopes)
  end

  disjunction_scope_class.new(scopes:)
end

#build_none_scopeObject

Creates a new none scope.



98
99
100
# File 'lib/cuprum/collections/scopes/building.rb', line 98

def build_none_scope
  none_scope_class.new
end

#transform_scope(scope:) ⇒ Object

Creates a new scope with the same scope type and properties.



103
104
105
106
107
# File 'lib/cuprum/collections/scopes/building.rb', line 103

def transform_scope(scope:)
  validate_scope!(scope)

  build_transformed_scope(scope)
end