Class: Cuprum::Rails::Command

Inherits:
Collections::Command
  • Object
show all
Defined in:
lib/cuprum/rails/command.rb

Overview

Abstract base class for Rails collection commands.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record_class:, collection_name: nil, member_name: nil, **options) ⇒ Command

Returns a new instance of Command.

Parameters:

  • collection_name (String, Symbol) (defaults to: nil)

    The name of the collection.

  • member_name (String) (defaults to: nil)

    The name of a collection entity.

  • options (Hash<Symbol>)

    Additional options for the command.

  • record_class (Class)

    The ActiveRecord class for the collection.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cuprum/rails/command.rb', line 23

def initialize(
  record_class:,
  collection_name: nil,
  member_name:     nil,
  **options
)
  super()

  @collection_name = resolve_collection_name(collection_name, record_class)
  @member_name     = resolve_member_name(@collection_name, member_name)
  @record_class    = record_class
  @options         = options
end

Instance Attribute Details

#collection_nameString (readonly)

Returns The name of the collection.

Returns:

  • (String)

    The name of the collection.



38
39
40
# File 'lib/cuprum/rails/command.rb', line 38

def collection_name
  @collection_name
end

#member_nameString (readonly)

Returns the name of a collection entity.

Returns:

  • (String)

    the name of a collection entity.



41
42
43
# File 'lib/cuprum/rails/command.rb', line 41

def member_name
  @member_name
end

#optionsHash<Symbol> (readonly)

Returns additional options for the command.

Returns:

  • (Hash<Symbol>)

    additional options for the command.



44
45
46
# File 'lib/cuprum/rails/command.rb', line 44

def options
  @options
end

#record_classClass (readonly)

Returns the ActiveRecord class for the collection.

Returns:

  • (Class)

    the ActiveRecord class for the collection.



47
48
49
# File 'lib/cuprum/rails/command.rb', line 47

def record_class
  @record_class
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/rails/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

Instance Method Details

#primary_key_nameSymbol

Returns the name of the primary key attribute.

Returns:

  • (Symbol)

    the name of the primary key attribute.



50
51
52
# File 'lib/cuprum/rails/command.rb', line 50

def primary_key_name
  @primary_key_name ||= record_class.primary_key
end

#primary_key_typeClass

Returns the type of the primary key attribute.

Returns:

  • (Class)

    the type of the primary key attribute.



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cuprum/rails/command.rb', line 55

def primary_key_type
  @primary_key_type ||=
    case primary_key_column_type
    when :integer
      Integer
    when :uuid
      String
    else
      # :nocov:
      raise "unknown primary key column type :#{primary_key_column_type}"
      # :nocov:
    end
end