Module: Cuprum::Collections::Relations::Parameters

Included in:
Collection, Cuprum::Collections::Relation
Defined in:
lib/cuprum/collections/relations/parameters.rb

Overview

Methods for resolving a relations's naming and entity class from options.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameString (readonly)

Returns the name of the relation.

Returns:

  • (String)

    the name of the relation.



246
247
248
# File 'lib/cuprum/collections/relations/parameters.rb', line 246

def name
  @name
end

#plural_nameString (readonly)

Returns the pluralized name of the relation.

Returns:

  • (String)

    the pluralized name of the relation.



249
250
251
# File 'lib/cuprum/collections/relations/parameters.rb', line 249

def plural_name
  @plural_name
end

#qualified_nameString (readonly)

Returns a scoped name for the relation.

Returns:

  • (String)

    a scoped name for the relation.



252
253
254
# File 'lib/cuprum/collections/relations/parameters.rb', line 252

def qualified_name
  @qualified_name
end

#singular_nameString (readonly)

Returns the name of an entity in the relation.

Returns:

  • (String)

    the name of an entity in the relation.



255
256
257
# File 'lib/cuprum/collections/relations/parameters.rb', line 255

def singular_name
  @singular_name
end

Class Method Details

.resolve_parameters(**options) ⇒ Hash

Helper method for resolving a Relation's required parameters.

At least one of the following options must be provided: name, qualified_name, or entity_class. The returned Hash will include the above keys as well as :singular_name and :plural_name.

Parameters:

  • options (Hash)

    the parameters to resolve.

Options Hash (**options):

  • default_entity_class (Class, String, nil)

    if given, this value will be assigned to the entity class if the entity_class is absent; it will not be used to derive other properties.

  • entity_class (Class, String)

    the class of entity represented by the relation.

  • name (String)

    the name of the relation.

  • plural_name (String)

    the name of a group of entities in the relation.

  • qualified_name (String)

    a scoped name for the relation.

  • singular_name (String)

    the name of an entity in the relation.

Returns:

  • (Hash)

    the resolved parameters.



50
51
52
53
54
55
56
57
58
# File 'lib/cuprum/collections/relations/parameters.rb', line 50

def resolve_parameters(params)
  validate_parameters(**params)

  if has_key?(params, :entity_class)
    resolve_parameters_from_entity_class(**params)
  else
    resolve_parameters_from_name(**params)
  end
end

Instance Method Details

#entity_classClass

Returns the class of entity represented by the relation.

Returns:

  • (Class)

    the class of entity represented by the relation.



258
259
260
261
262
# File 'lib/cuprum/collections/relations/parameters.rb', line 258

def entity_class
  return @entity_class if @entity_class.is_a?(Class)

  @entity_class = Object.const_get(@entity_class)
end

#initialize(entity_class: nil, name: nil, qualified_name: nil, singular_name: nil, **) ⇒ Object

Parameters:

  • entity_class (Class, String) (defaults to: nil)

    the class of entity represented by the relation.

  • name (String) (defaults to: nil)

    the name of the relation.

  • qualified_name (String) (defaults to: nil)

    a scoped name for the relation.

  • singular_name (String) (defaults to: nil)

    the name of an entity in the relation.



233
234
235
236
237
238
239
240
241
242
243
# File 'lib/cuprum/collections/relations/parameters.rb', line 233

def initialize(**parameters)
  super(**parameters.except(*IGNORED_PARAMETERS))

  relation_params = resolve_parameters(parameters)

  @entity_class   = relation_params[:entity_class]
  @name           = relation_params[:name]
  @plural_name    = relation_params[:plural_name]
  @qualified_name = relation_params[:qualified_name]
  @singular_name  = relation_params[:singular_name]
end

#resolve_parameters(**options) ⇒ Hash

Helper method for resolving a Relation's required parameters.

At least one of the following options must be provided: name, qualified_name, or entity_class. The returned Hash will include the above keys as well as :singular_name and :plural_name.

Parameters:

  • options (Hash)

    the parameters to resolve.

Options Hash (**options):

  • default_entity_class (Class, String, nil)

    if given, this value will be assigned to the entity class if the entity_class is absent; it will not be used to derive other properties.

  • entity_class (Class, String)

    the class of entity represented by the relation.

  • name (String)

    the name of the relation.

  • plural_name (String)

    the name of a group of entities in the relation.

  • qualified_name (String)

    a scoped name for the relation.

  • singular_name (String)

    the name of an entity in the relation.

Returns:

  • (Hash)

    the resolved parameters.



265
266
267
# File 'lib/cuprum/collections/relations/parameters.rb', line 265

def resolve_parameters(parameters)
  Parameters.resolve_parameters(parameters)
end