class Cuprum::Collections::Errors::AlreadyExists
Returned when an insert command is called with an existing record.
Constants
- TYPE
Short string used to identify the type of error.
Attributes
collection_name[R]
@return [String, Symbol] the name of the collection.
primary_key_name[R]
@return [String, Symbol] the name of the primary key attribute.
primary_key_values[R]
@return [Array] The expected values of the primary key attribute.
Public Class Methods
new(collection_name:, primary_key_name:, primary_key_values:)
click to toggle source
@param collection_name
[String, Symbol] The name of the collection. @param primary_key_name
[String, Symbol] The name of the primary key
attribute.
@param primary_key_values
[Object, Array] The expected values of the
primary key attribute.
Calls superclass method
# File lib/cuprum/collections/errors/already_exists.rb, line 18 def initialize(collection_name:, primary_key_name:, primary_key_values:) @collection_name = collection_name @primary_key_name = primary_key_name @primary_key_values = Array(primary_key_values) super( collection_name: collection_name, message: default_message, primary_key_name: primary_key_name, primary_key_values: primary_key_values ) end
Public Instance Methods
as_json()
click to toggle source
@return [Hash] a serializable hash representation of the error.
# File lib/cuprum/collections/errors/already_exists.rb, line 41 def as_json { 'data' => { 'collection_name' => collection_name, 'primary_key_name' => primary_key_name, 'primary_key_values' => primary_key_values }, 'message' => message, 'type' => type } end
type()
click to toggle source
@return [String] short string used to identify the type of error.
# File lib/cuprum/collections/errors/already_exists.rb, line 54 def type TYPE end
Private Instance Methods
default_message()
click to toggle source
# File lib/cuprum/collections/errors/already_exists.rb, line 60 def default_message primary_keys = primary_key_values.map(&:inspect).join(', ') "#{entity_name} already exist#{singular? ? 's' : ''} with" \ " #{primary_key_name} #{primary_keys}" end
entity_name()
click to toggle source
# File lib/cuprum/collections/errors/already_exists.rb, line 67 def entity_name entity_name = collection_name entity_name = tools.str.singularize(entity_name) if singular? titleize(entity_name) end
singular?()
click to toggle source
# File lib/cuprum/collections/errors/already_exists.rb, line 74 def singular? primary_key_values.size == 1 end
titleize(string)
click to toggle source
# File lib/cuprum/collections/errors/already_exists.rb, line 78 def titleize(string) tools.str.underscore(string).split('_').map(&:capitalize).join(' ') end
tools()
click to toggle source
# File lib/cuprum/collections/errors/already_exists.rb, line 82 def tools SleepingKingStudios::Tools::Toolbelt.instance end