Class: Cuprum::Collections::Basic::Command

Inherits:
Command
  • Object
show all
Defined in:
lib/cuprum/collections/basic/command.rb

Overview

Abstract base class for basic collection commands.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection_name:, data:, default_contract: nil, member_name: nil, primary_key_name: :id, primary_key_type: Integer, **options) ⇒ Command

Returns a new instance of Command.

Parameters:

  • collection_name (String, Symbol)

    The name of the collection.

  • data (Array<Hash>)

    The current data in the collection.

  • default_contract (Stannum::Constraints::Base, nil) (defaults to: nil)

    The default contract for validating items in the collection.

  • member_name (String) (defaults to: nil)

    The name of a collection entity.

  • primary_key_name (Symbol) (defaults to: :id)

    The name of the primary key attribute. Defaults to :id.

  • primary_key_type (Class, Stannum::Constraint) (defaults to: Integer)

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

  • options (Hash<Symbol>)

    Additional options for the command.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

Instance Attribute Details

#collection_nameString (readonly)

Returns the name of the collection.

Returns:

  • (String)

    the name of the collection.



51
52
53
# File 'lib/cuprum/collections/basic/command.rb', line 51

def collection_name
  @collection_name
end

#dataArray<Hash> (readonly)

Returns the current data in the collection.

Returns:

  • (Array<Hash>)

    the current data in the collection.



54
55
56
# File 'lib/cuprum/collections/basic/command.rb', line 54

def data
  @data
end

#default_contractStannum::Constraints::Base? (readonly)

Returns the default contract for validating items in the collection.

Returns:

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

    the default contract for validating items in the collection.



58
59
60
# File 'lib/cuprum/collections/basic/command.rb', line 58

def default_contract
  @default_contract
end

#member_nameString (readonly)

Returns the name of a collection entity.

Returns:

  • (String)

    the name of a collection entity.



61
62
63
# File 'lib/cuprum/collections/basic/command.rb', line 61

def member_name
  @member_name
end

#optionsHash<Symbol> (readonly)

Returns additional options for the command.

Returns:

  • (Hash<Symbol>)

    additional options for the command.



64
65
66
# File 'lib/cuprum/collections/basic/command.rb', line 64

def options
  @options
end

#primary_key_nameSymbol (readonly)

Returns the name of the primary key attribute.

Returns:

  • (Symbol)

    the name of the primary key attribute.



67
68
69
# File 'lib/cuprum/collections/basic/command.rb', line 67

def primary_key_name
  @primary_key_name
end

#primary_key_typeClass, Stannum::Constraint (readonly)

Returns the type of the primary key attribute.

Returns:

  • (Class, Stannum::Constraint)

    the type of the primary key attribute.



71
72
73
# File 'lib/cuprum/collections/basic/command.rb', line 71

def primary_key_type
  @primary_key_type
end

Class Method Details

.subclass(**default_options) ⇒ Object

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



11
12
13
14
15
16
17
# 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