class Cuprum::Collections::Basic::Command

Abstract base class for basic collection commands.

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/command.rb, line 29
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
subclass(**default_options) click to toggle source

Creates a subclass with the given parameters applied to the constructor.

Calls superclass method
# File lib/cuprum/collections/basic/command.rb, line 11
def self.subclass(**default_options)
  Class.new(self) do
    define_method(:initialize) do |**options|
      super(**default_options.merge(options))
    end
  end
end

Private Instance Methods

primary_key_contract() click to toggle source
# File lib/cuprum/collections/basic/command.rb, line 75
def primary_key_contract
  type = primary_key_type

  @primary_key_contract ||= Stannum::Contracts::ParametersContract.new do
    keyword :primary_key, type
  end
end
primary_keys_contract() click to toggle source
# File lib/cuprum/collections/basic/command.rb, line 83
def primary_keys_contract
  type = primary_key_type

  @primary_keys_contract ||= Stannum::Contracts::ParametersContract.new do
    keyword :primary_keys,
      Stannum::Constraints::Types::ArrayType.new(item_type: type)
  end
end
tools() click to toggle source
# File lib/cuprum/collections/basic/command.rb, line 92
def tools
  SleepingKingStudios::Tools::Toolbelt.instance
end
validate_primary_key(primary_key) click to toggle source
# File lib/cuprum/collections/basic/command.rb, line 96
def validate_primary_key(primary_key)
  match_parameters_to_contract(
    contract:    primary_key_contract,
    keywords:    { primary_key: primary_key },
    method_name: :call
  )
end
validate_primary_keys(primary_keys) click to toggle source
# File lib/cuprum/collections/basic/command.rb, line 104
def validate_primary_keys(primary_keys)
  match_parameters_to_contract(
    contract:    primary_keys_contract,
    keywords:    { primary_keys: primary_keys },
    method_name: :call
  )
end