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 included from Relations::Scope

#scope

Attributes included from Relations::Parameters

#name, #plural_name, #qualified_name, #singular_name

Attributes included from Relations::Options

#options

Instance Method Summary collapse

Methods inherited from Collection

#==, #count, #matches?

Methods included from Relations::Scope

#with_scope

Methods included from Relations::PrimaryKeys

#primary_key_name, #primary_key_type

Methods included from Relations::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.

  • 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'.

  • primary_key_type (Class, Stannum::Constraint)

    the type of the primary key attribute. Defaults to Integer.

  • scope (Object)

    [Cuprum::Collections::Scopes::Base, Hash, Proc, nil] the configured scope for the relation.



29
30
31
32
33
# File 'lib/cuprum/collections/basic/collection.rb', line 29

def initialize(data: [], **parameters)
  super(default_entity_class: Hash, **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.



36
37
38
# File 'lib/cuprum/collections/basic/collection.rb', line 36

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.



76
77
78
# File 'lib/cuprum/collections/basic/collection.rb', line 76

def default_contract
  @options[:default_contract]
end

#queryCuprum::Collections::Basic::Query

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

Returns:



83
84
85
# File 'lib/cuprum/collections/basic/collection.rb', line 83

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