class Cuprum::Collections::Basic::Collection

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

Attributes

collection_name[R]

@return [String] the name of the collection.

data[R]

@return [Array<Hash>] the current data in the collection.

default_contract[R]

@return [Stannum::Constraints::Base, nil] the default contract for

validating items in the collection.
member_name[R]

@return [String] the name of a collection entity.

options[R]

@return [Hash<Symbol>] additional options for the command.

primary_key_name[R]

@return [Symbol] the name of the primary key attribute.

primary_key_type[R]

@return [Class, Stannum::Constraint] the type of the primary key

attribute.

Public Class Methods

new( collection_name:, data:, default_contract: nil, member_name: nil, primary_key_name: :id, primary_key_type: Integer, **options ) click to toggle source

@param collection_name [String, Symbol] The name of the collection. @param data [Array<Hash>] The current data in the collection. @param default_contract [Stannum::Constraints::Base, nil] The default

contract for validating items in the collection.

@param member_name [String] The name of a collection entity. @param primary_key_name [Symbol] The name of the primary key attribute.

Defaults to :id.

@param primary_key_type [Class, Stannum::Constraint] The type of the

primary key attribute. Defaults to Integer.

@param options [Hash<Symbol>] Additional options for the command.

Calls superclass method
# File lib/cuprum/collections/basic/collection.rb, line 21
def initialize( # rubocop:disable Metrics/ParameterLists
  collection_name:,
  data:,
  default_contract: nil,
  member_name:      nil,
  primary_key_name: :id,
  primary_key_type: Integer,
  **options
)
  super()

  @collection_name  = collection_name.to_s
  @data             = data
  @default_contract = default_contract
  @member_name      =
    member_name ? member_name.to_s : tools.str.singularize(@collection_name)
  @options          = options
  @primary_key_name = primary_key_name
  @primary_key_type = primary_key_type
end

Public Instance Methods

query() click to toggle source

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

@return [Cuprum::Collections::Basic::Query] the query.

# File lib/cuprum/collections/basic/collection.rb, line 113
def query
  Cuprum::Collections::Basic::Query.new(data)
end

Private Instance Methods

command_options() click to toggle source
# File lib/cuprum/collections/basic/collection.rb, line 119
def command_options
  @command_options ||= {
    collection_name:  collection_name,
    data:             data,
    default_contract: default_contract,
    member_name:      member_name,
    primary_key_name: primary_key_name,
    primary_key_type: primary_key_type,
    **options
  }
end
tools() click to toggle source
# File lib/cuprum/collections/basic/collection.rb, line 131
def tools
  SleepingKingStudios::Tools::Toolbelt.instance
end