Class: Cuprum::Collections::Basic::Collection

Inherits:
Collection
  • Object
show all
Defined in:
lib/cuprum/collections/basic/collection.rb

Overview

Wraps an in-memory array of hashes data store as a Cuprum collection.

Instance Attribute Summary collapse

Attributes inherited from Collection

#options

Attributes included from Relation::Parameters

#name, #plural_name, #qualified_name, #singular_name

Instance Method Summary collapse

Methods inherited from Collection

#==, #collection_name, #count, #matches?, #member_name

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(data: [], entity_class: nil, name: nil, qualified_name: nil, singular_name: nil, **options) ⇒ Collection

Returns a new instance of Collection.

Parameters:

  • data (Array<Hash>) (defaults to: [])

    the current data in the collection.

  • 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. Aliased as :collection_name.

  • 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. Aliased as :member_name.

  • options (Hash)

    additional options for the relation.

Options Hash (**options):

  • primary_key_name (String)

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cuprum/collections/basic/collection.rb', line 27

def initialize(data: [], entity_class: Hash, **parameters)
  qualified_name = parameters.fetch(:qualified_name) do
    next nil unless entity_class == Hash

    parameters.fetch(:collection_name, parameters[:name])
  end

  super(
    entity_class:   entity_class,
    qualified_name: qualified_name,
    **parameters
  )

  @data = data
end

Instance Attribute Details

#dataArray<Hash> (readonly)

Returns the current data in the collection.

Returns:

  • (Array<Hash>)

    the current data in the collection.



44
45
46
# File 'lib/cuprum/collections/basic/collection.rb', line 44

def data
  @data
end

Instance Method Details

#default_contractStannum::Constraints::Base?

Returns the # default contract for validating items in the collection.

Returns:

  • (Stannum::Constraints::Base, nil)

    the # default contract for validating items in the collection.



93
94
95
# File 'lib/cuprum/collections/basic/collection.rb', line 93

def default_contract
  @options[:default_contract]
end

#queryCuprum::Collections::Basic::Query

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

Returns:



100
101
102
# File 'lib/cuprum/collections/basic/collection.rb', line 100

def query
  Cuprum::Collections::Basic::Query.new(data)
end