# File lib/cuprum/rails/command.rb, line 48 def primary_key_name @primary_key_name ||= record_class.primary_key.intern end
class Cuprum::Rails::Command
Abstract base class for Rails
collection commands.
Attributes
collection_name[R]
@return [String] The name of the collection.
member_name[R]
@return [String] the name of a collection entity.
options[R]
@return [Hash<Symbol>] additional options for the command.
record_class[R]
@return [Class] the ActiveRecord class for the collection.
Public Class Methods
new( record_class:, collection_name: nil, member_name: nil, **options )
click to toggle source
@param collection_name
[String, Symbol] The name of the collection. @param member_name
[String] The name of a collection entity. @param options [Hash<Symbol>] Additional options for the command. @param record_class
[Class] The ActiveRecord class for the collection.
Calls superclass method
# File lib/cuprum/rails/command.rb, line 21 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
subclass(**default_options)
click to toggle source
Creates a subclass with the given parameters applied to the constructor.
Calls superclass method
# File lib/cuprum/rails/command.rb, line 9 def self.subclass(**default_options) Class.new(self) do define_method(:initialize) do |**options| super(**default_options.merge(options)) end end end
Public Instance Methods
primary_key_name()
click to toggle source
@return [Symbol] the name of the primary key attribute.
primary_key_type()
click to toggle source
@return [Class] the type of the primary key attribute.
# File lib/cuprum/rails/command.rb, line 53 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
Private Instance Methods
entity_contract()
click to toggle source
# File lib/cuprum/rails/command.rb, line 69 def entity_contract type = record_class @entity_contract ||= Stannum::Contracts::ParametersContract.new do keyword :entity, type end end
primary_key_column_type()
click to toggle source
# File lib/cuprum/rails/command.rb, line 85 def primary_key_column_type record_class .columns .find { |column| column.name == record_class.primary_key } .type end
primary_key_contract()
click to toggle source
# File lib/cuprum/rails/command.rb, line 77 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/rails/command.rb, line 92 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
resolve_collection_name(collection_name, record_class)
click to toggle source
# File lib/cuprum/rails/command.rb, line 101 def resolve_collection_name(collection_name, record_class) return collection_name.to_s unless collection_name.nil? record_class.name.underscore.pluralize end
resolve_member_name(collection_name, member_name)
click to toggle source
# File lib/cuprum/rails/command.rb, line 107 def resolve_member_name(collection_name, member_name) return member_name.to_s unless member_name.nil? collection_name.singularize end
validate_entity(entity)
click to toggle source
# File lib/cuprum/rails/command.rb, line 113 def validate_entity(entity) match_parameters_to_contract( contract: entity_contract, keywords: { entity: entity }, method_name: :call ) end
validate_primary_key(primary_key)
click to toggle source
# File lib/cuprum/rails/command.rb, line 121 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/rails/command.rb, line 129 def validate_primary_keys(primary_keys) match_parameters_to_contract( contract: primary_keys_contract, keywords: { primary_keys: primary_keys }, method_name: :call ) end