Module: Cuprum::Collections::Relation::Disambiguation

Included in:
Collection, Cuprum::Collections::Resource
Defined in:
lib/cuprum/collections/relation.rb

Overview

Methods for disambiguating parameters with multiple keywords.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.disambiguate_keyword(params, key, *alternatives) ⇒ Hash

Helper method for resolving an ambiguous keyword.

Parameters:

  • params (Hash)

    the original method keywords.

  • key (Symbol)

    the original key to resolve.

  • alternatives (Symbol, Array<Symbol>)

    the additional keywords.

Returns:

  • (Hash)

    the disambiguated keywords.

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cuprum/collections/relation.rb', line 66

def disambiguate_keyword(params, key, *alternatives) # rubocop:disable Metrics/MethodLength
  params = params.dup
  values = keyword_values(params, key, *alternatives)

  return params if values.empty?

  if values.size == 1
    match, value = values.first

    unless match == key
      tools.core_tools.deprecate(
        "#{match.inspect} keyword",
        message: "Use #{key.inspect} instead"
      )
    end

    return params.merge(key => value)
  end

  raise ArgumentError, ambiguous_keywords_error(values)
end

.resolve_parameters(params, **ambiguous) ⇒ Hash

Helper method for resolving a Relation’s required parameters.

The returned Hash will define the :entity_class, :singular_name, :name, and :qualified_name keys.

Parameters:

  • params (Hash)

    the parameters to resolve.

  • ambiguous (Hash{Symbol => Symbol, Array<Symbol>})

    ambiguous keywords to resolve. Each key-value pair is passed to .disambiguate_keyword before the parameters are resolved.

Returns:

  • (Hash)

    the resolved parameters.

See Also:



102
103
104
105
106
107
108
# File 'lib/cuprum/collections/relation.rb', line 102

def resolve_parameters(params, **ambiguous)
  params = ambiguous.reduce(params) do |hsh, (key, alternatives)|
    disambiguate_keyword(hsh, key, *alternatives)
  end

  Cuprum::Collections::Relation::Parameters.resolve_parameters(params)
end

Instance Method Details

#disambiguate_keyword(params, key, *alternatives) ⇒ Hash

Helper method for resolving an ambiguous keyword.

Parameters:

  • params (Hash)

    the original method keywords.

  • key (Symbol)

    the original key to resolve.

  • alternatives (Symbol, Array<Symbol>)

    the additional keywords.

Returns:

  • (Hash)

    the disambiguated keywords.



135
136
137
# File 'lib/cuprum/collections/relation.rb', line 135

def disambiguate_keyword(params, key, *alternatives)
  Disambiguation.disambiguate_keyword(params, key, *alternatives)
end

#resolve_parameters(params, **ambiguous) ⇒ Hash

Helper method for resolving a Relation’s required parameters.

The returned Hash will define the :entity_class, :singular_name, :name, and :qualified_name keys.

Parameters:

  • params (Hash)

    the parameters to resolve.

  • ambiguous (Hash{Symbol => Symbol, Array<Symbol>})

    ambiguous keywords to resolve. Each key-value pair is passed to .disambiguate_keyword before the parameters are resolved.

Returns:

  • (Hash)

    the resolved parameters.

See Also:



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

def resolve_parameters(params, **ambiguous)
  Disambiguation.resolve_parameters(params, **ambiguous)
end