Class: Cuprum::Collections::Collection

Inherits:
Cuprum::CommandFactory
  • Object
show all
Includes:
Relation::Disambiguation, Relation::Parameters, Relation::PrimaryKeys
Defined in:
lib/cuprum/collections/collection.rb

Overview

Provides a base implementation for collections.

Direct Known Subclasses

Basic::Collection

Defined Under Namespace

Classes: AbstractCollectionError

Instance Attribute Summary collapse

Attributes included from Relation::Parameters

#name, #plural_name, #qualified_name, #singular_name

Instance Method Summary collapse

Methods included from Relation::Disambiguation

disambiguate_keyword, #disambiguate_keyword, resolve_parameters, #resolve_parameters

Methods included from Relation::PrimaryKeys

#primary_key_name, #primary_key_type

Methods included from Relation::Parameters

#entity_class, resolve_parameters, #resolve_parameters

Constructor Details

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

Returns a new instance of Collection.

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.

  • options (Hash)

    additional options for the relation.

Options Hash (**options):

  • primary_key_name (String)

    the name of the primary key attribute. Defaults to ‘id’.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cuprum/collections/collection.rb', line 40

def initialize(**parameters) # rubocop:disable Metrics/MethodLength
  super()

  relation_params = resolve_parameters(
    parameters,
    name:          :collection_name,
    singular_name: :member_name
  )
  @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]

  @options = ignore_parameters(**parameters)
end

Instance Attribute Details

#optionsHash<Symbol> (readonly)

Returns additional options for the collection.

Returns:

  • (Hash<Symbol>)

    additional options for the collection.



58
59
60
# File 'lib/cuprum/collections/collection.rb', line 58

def options
  @options
end

Instance Method Details

#==(other) ⇒ true, false

Returns true if the other object is a collection with the same options, otherwise false.

Parameters:

  • other (Object)

    The object to compare.

Returns:

  • (true, false)

    true if the other object is a collection with the same options, otherwise false.



64
65
66
67
68
# File 'lib/cuprum/collections/collection.rb', line 64

def ==(other)
  return false unless self.class == other.class

  comparable_options == other.comparable_options
end

#collection_nameString

Returns the name of the collection.

Returns:

  • (String)

    the name of the collection.



71
72
73
74
75
76
# File 'lib/cuprum/collections/collection.rb', line 71

def collection_name
  tools.core_tools.deprecate '#collection_name method',
    message: 'Use #name instead'

  name
end

#countInteger Also known as: size

Returns the count of items in the collection.

Returns:

  • (Integer)

    the count of items in the collection.



79
80
81
# File 'lib/cuprum/collections/collection.rb', line 79

def count
  query.count
end

#matches?(**expected) ⇒ Boolean

Checks if the collection matches the expected options.

Parameters:

  • expected (Hash)

    the options to compare.

Returns:

  • (Boolean)

    true if all of the expected options match, otherwise false.



90
91
92
93
94
95
96
97
98
# File 'lib/cuprum/collections/collection.rb', line 90

def matches?(**expected)
  if expected[:entity_class].is_a?(String)
    expected = expected.merge(
      entity_class: Object.const_get(expected[:entity_class])
    )
  end

  comparable_options >= expected
end

#member_nameString

Returns the name of an entity in the relation.

Returns:

  • (String)

    the name of an entity in the relation.



101
102
103
104
105
106
# File 'lib/cuprum/collections/collection.rb', line 101

def member_name
  tools.core_tools.deprecate '#member_name method',
    message: 'Use #singular_name instead'

  singular_name
end

#queryObject

A new Query instance, used for querying against the collection data.

Returns:

  • (Object)

    the query.

Raises:



111
112
113
114
115
# File 'lib/cuprum/collections/collection.rb', line 111

def query
  raise AbstractCollectionError,
    "#{self.class.name} is an abstract class. Define a repository " \
    'subclass and implement the #query method.'
end