class Cuprum::Collections::Queries::ParseStrategy
Command
to select the parsing strategy for parsing Query#where
parameters.
Constants
- STRATEGIES
- UNKNOWN_STRATEGY_ERROR
The :type of the error generated for an unknown parsing strategy.
Private Instance Methods
find_and_validate_strategy(strategy:, where:)
click to toggle source
# File lib/cuprum/collections/queries/parse_strategy.rb, line 21 def find_and_validate_strategy(strategy:, where:) command_class = step { find_strategy_by_key(strategy: strategy) } parameters = { arguments: [], block: nil, keywords: { where: where } } return command_class if command_class.matches?(parameters) errors = command_class.errors_for(parameters) failure(invalid_parameters_error(errors: errors, strategy: strategy)) end
find_strategy(strategy:, where:)
click to toggle source
# File lib/cuprum/collections/queries/parse_strategy.rb, line 36 def find_strategy(strategy:, where:) if strategy return find_and_validate_strategy(strategy: strategy, where: where) end command_class = find_strategy_by_parameters(where: where) return command_class if command_class failure(unknown_strategy_error(strategy: strategy)) end
find_strategy_by_key(strategy:)
click to toggle source
# File lib/cuprum/collections/queries/parse_strategy.rb, line 48 def find_strategy_by_key(strategy:) STRATEGIES.fetch(strategy) do failure(unknown_strategy_error(strategy: strategy)) end end
find_strategy_by_parameters(where:)
click to toggle source
# File lib/cuprum/collections/queries/parse_strategy.rb, line 54 def find_strategy_by_parameters(where:) STRATEGIES .values .find do |command_class| command_class.matches?( arguments: [], block: nil, keywords: { where: where } ) end end
invalid_parameters_error(errors:, strategy:)
click to toggle source
# File lib/cuprum/collections/queries/parse_strategy.rb, line 66 def invalid_parameters_error(errors:, strategy:) Cuprum::Collections::Errors::InvalidQuery.new( errors: errors, strategy: strategy ) end
process(strategy: nil, where: nil)
click to toggle source
# File lib/cuprum/collections/queries/parse_strategy.rb, line 73 def process(strategy: nil, where: nil) command_class = step do find_strategy(strategy: strategy, where: where) end command_class.new end
unknown_strategy_error(strategy:)
click to toggle source
# File lib/cuprum/collections/queries/parse_strategy.rb, line 81 def unknown_strategy_error(strategy:) errors = Stannum::Errors.new errors[:strategy].add(UNKNOWN_STRATEGY_ERROR, strategy: strategy) Cuprum::Collections::Errors::InvalidQuery.new( errors: errors, strategy: strategy ) end