Class: Cuprum::Collections::Errors::AbstractFindError

Inherits:
Error
  • Object
show all
Defined in:
lib/cuprum/collections/errors/abstract_find_error.rb

Overview

Abstract base class for failed query errors.

Direct Known Subclasses

AlreadyExists, NotFound, NotUnique

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute_name:, attribute_value:, name:, primary_key: false) ⇒ AbstractFindError #initialize(attributes:, name:) ⇒ AbstractFindError #initialize(query:, name:) ⇒ AbstractFindError

Returns a new instance of AbstractFindError.

Overloads:

  • #initialize(attribute_name:, attribute_value:, name:, primary_key: false) ⇒ AbstractFindError

    Parameters:

    • attribute_name (String)

      the name of the queried attribute.

    • attribute_value (Object)

      the value of the queried attribute.

    • name (String)

      the name of the collection.

    • primary_key (true, false) (defaults to: false)

      indicates that the queried attribute is the primary key for the collection.

  • #initialize(attributes:, name:) ⇒ AbstractFindError

    Parameters:

    • attributes (Hash<String=>Object>)

      the queried attributes.

    • name (String)

      the name of the collection.

  • #initialize(query:, name:) ⇒ AbstractFindError

    Parameters:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cuprum/collections/errors/abstract_find_error.rb', line 97

def initialize(**params) # rubocop:disable Metrics/MethodLength
  @collection      = self.class.resolve_collection(**params)
  @collection_name = @collection['name']
  @primary_key     = false

  resolve_options(**params.except(*COLLECTION_KEYWORDS))

  super(
    attribute_name:,
    attribute_value:,
    attributes:,
    message:         generate_message,
    scope:
  )
end

Instance Attribute Details

#attribute_nameString (readonly)

Returns the name of the queried attribute, if any.

Returns:

  • (String)

    the name of the queried attribute, if any.



114
115
116
# File 'lib/cuprum/collections/errors/abstract_find_error.rb', line 114

def attribute_name
  @attribute_name
end

#attribute_valueObject (readonly)

Returns the value of the queried attribute, if any.

Returns:

  • (Object)

    the value of the queried attribute, if any.



117
118
119
# File 'lib/cuprum/collections/errors/abstract_find_error.rb', line 117

def attribute_value
  @attribute_value
end

#attributesHash<String=>Object> (readonly)

Returns The queried attributes.

Returns:

  • (Hash<String=>Object>)

    The queried attributes.



120
121
122
# File 'lib/cuprum/collections/errors/abstract_find_error.rb', line 120

def attributes
  @attributes
end

#collectionHash (readonly)

Returns the resolved collection details.

Returns:

  • (Hash)

    the resolved collection details.



123
124
125
# File 'lib/cuprum/collections/errors/abstract_find_error.rb', line 123

def collection
  @collection
end

#scopeCuprum::Collections::Scopes::Base (readonly)

Returns the query scope, if any.

Returns:



126
127
128
# File 'lib/cuprum/collections/errors/abstract_find_error.rb', line 126

def scope
  @scope
end

Class Method Details

.resolve_collection(**params) ⇒ Hash

Resolves the details about the queried collection.

Parameters:

  • params (Hash)

    the parameters to resolve.

Returns:

  • (Hash)

    the collection details.

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cuprum/collections/errors/abstract_find_error.rb', line 36

def resolve_collection(**params) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  if params[:collection].is_a?(Cuprum::Collections::Collection)
    return collection_details(params[:collection])
  elsif collection_name?(params[:collection])
    return { 'name' => params[:collection].to_s }
  elsif collection_name?(params[:collection_name])
    # @deprecated 0.6.0
    tools.core_tools.deprecate(
      ':collection_name parameter is deprecated',
      message: 'Use the :name parameter instead.'
    )

    return { 'name' => params[:collection_name].to_s }
  elsif VALID_PARAMETERS.any? { |key| params.key?(key) }
    return Cuprum::Collections::Relations::Parameters
        .resolve_parameters(params)
        .slice(*VALID_PARAMETERS)
        .to_h { |key, value| [key.to_s, value.to_s] }
  end

  raise ArgumentError, "collection, name or entity class can't be blank"
end

Instance Method Details

#collection_nameString

Deprecated.

0.6.0

Returns the name of the collection.

Returns:

  • (String)

    the name of the collection.



131
132
133
134
135
136
137
138
# File 'lib/cuprum/collections/errors/abstract_find_error.rb', line 131

def collection_name
  tools.core_tools.deprecate(
    '#collection_name is deprecated',
    message: 'Use the #collection method instead.'
  )

  collection['name']
end

#detailsArray<Array>

Returns the details of the query, in scope format.

Returns:

  • (Array<Array>)

    the details of the query, in scope format.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/cuprum/collections/errors/abstract_find_error.rb', line 141

def details # rubocop:disable Metrics/MethodLength
  if attribute_name
    criteria = [[attribute_name, :equal, attribute_value]]

    { 'type' => :criteria, 'criteria' => criteria }
  elsif attributes
    criteria =
      attributes
        .map { |attr_name, attr_value| [attr_name, :equal, attr_value] }

    { 'type' => :criteria, 'criteria' => criteria }
  elsif scope
    scope
  end
end

#primary_key?true, false

Returns indicates that the queried attribute is the primary key for the collection.

Returns:

  • (true, false)

    indicates that the queried attribute is the primary key for the collection.



159
160
161
# File 'lib/cuprum/collections/errors/abstract_find_error.rb', line 159

def primary_key?
  @primary_key
end