# 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
class Cuprum::Collections::Basic::Command
Abstract base class for basic collection commands.
Attributes
@return [String] the name of the collection.
@return [Array<Hash>] the current data in the collection.
@return [Stannum::Constraints::Base, nil] the default contract for
validating items in the collection.
@return [String] the name of a collection entity.
@return [Hash<Symbol>] additional options for the command.
@return [Symbol] the name of the primary key attribute.
@return [Class, Stannum::Constraint] the type of the primary key
attribute.
Public Class Methods
@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.
# 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
Creates a subclass with the given parameters applied to the constructor.
# 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
# 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
# File lib/cuprum/collections/basic/command.rb, line 92 def tools SleepingKingStudios::Tools::Toolbelt.instance end
# 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
# 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