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

Defined in:
lib/cuprum/collections/scopes/criteria.rb

Overview

Class methods to extend when including the module.

Instance Method Summary collapse

Instance Method Details

#build(value = nil) { ... } ⇒ Criteria

Initializes a new criteria scope with the parsed criteria.

Parameters:

  • value (Hash, nil) (defaults to: nil)

    the keys and values to parse.

Yields:

  • the query block.

Yield Returns:

  • (Hash)

    a Hash with String keys.

Returns:

  • (Criteria)

    the scope with the generated criteria.



21
22
23
24
25
# File 'lib/cuprum/collections/scopes/criteria.rb', line 21

def build(...)
  criteria = parse(...)

  new(criteria:)
end

#parse(value = nil) { ... } ⇒ Array

Converts a valid query hash and/or block to criteria.

The block must return a Hash with String keys. The hash values must either be literal values (e.g. a String, an Integer, etc) or a call to an operator function.

Parameters:

  • value (Hash, nil) (defaults to: nil)

    the keys and values to parse.

Yields:

  • the query block.

Yield Returns:

  • (Hash)

    a Hash with String keys.

Returns:

  • (Array)

    the generated criteria.



29
30
31
32
33
# File 'lib/cuprum/collections/scopes/criteria.rb', line 29

def parse(*args, &)
  parser = Cuprum::Collections::Scopes::Criteria::Parser.instance

  args.empty? ? parser.parse(&) : parser.parse(args.first, &)
end