Class: Cuprum::Collections::Collection
- Inherits:
-
Cuprum::CommandFactory
- Object
- Cuprum::CommandFactory
- Cuprum::Collections::Collection
- Defined in:
- lib/cuprum/collections/collection.rb
Overview
Provides a base implementation for collections.
Direct Known Subclasses
Defined Under Namespace
Classes: AbstractCollectionError
Instance Attribute Summary collapse
-
#options ⇒ Hash<Symbol>
readonly
Additional options for the collection.
Attributes included from Relation::Parameters
#name, #plural_name, #qualified_name, #singular_name
Instance Method Summary collapse
-
#==(other) ⇒ true, false
True if the other object is a collection with the same options, otherwise false.
-
#collection_name ⇒ String
The name of the collection.
-
#count ⇒ Integer
(also: #size)
The count of items in the collection.
-
#initialize(entity_class: nil, name: nil, qualified_name: nil, singular_name: nil, **options) ⇒ Collection
constructor
A new instance of Collection.
-
#matches?(**expected) ⇒ Boolean
Checks if the collection matches the expected options.
-
#member_name ⇒ String
The name of an entity in the relation.
-
#query ⇒ Object
A new Query instance, used for querying against the collection data.
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.
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
#options ⇒ Hash<Symbol> (readonly)
Returns additional options for the collection.
58 59 60 |
# File 'lib/cuprum/collections/collection.rb', line 58 def @options end |
Instance Method Details
#==(other) ⇒ true, false
Returns 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 == other. end |
#collection_name ⇒ String
Returns 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 |
#count ⇒ Integer Also known as: size
Returns 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.
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 >= expected end |
#member_name ⇒ String
Returns 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 |
#query ⇒ Object
A new Query instance, used for querying against the collection data.
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 |