Class: Operandi::Messages
- Inherits:
-
Object
- Object
- Operandi::Messages
- Extended by:
- Forwardable
- Defined in:
- lib/operandi/messages.rb
Overview
Collection of error or warning messages, organized by key.
Instance Method Summary collapse
-
#[](key) ⇒ Array<Message>?
Get messages for a specific key.
-
#add(key, texts, opts = {}) ⇒ void
Add a message to the collection.
-
#any? ⇒ Boolean
Check if there are any messages.
-
#break? ⇒ Boolean
Check if step execution should stop.
-
#copy_from(entity, opts = {}) ⇒ void
(also: #from_record)
Copy messages from another source.
-
#count ⇒ Integer
Get total count of all messages across all keys.
-
#empty? ⇒ Boolean
Check if the collection is empty.
-
#initialize(config = {}, service:) ⇒ Messages
constructor
Initialize a new messages collection.
-
#key?(key) ⇒ Boolean
(also: #has_key?)
Check if a key has messages.
-
#keys ⇒ Array<Symbol>
Get all keys with messages.
-
#size ⇒ Integer
Get number of keys with messages.
-
#to_h ⇒ Hash{Symbol => Array<String>}
Convert messages to a hash with string values.
Constructor Details
#initialize(config = {}, service:) ⇒ Messages
Initialize a new messages collection.
59 60 61 62 63 64 |
# File 'lib/operandi/messages.rb', line 59 def initialize(config = {}, service:) @break = false @config = config @messages = {} @service = service end |
Instance Method Details
#[](key) ⇒ Array<Message>?
Get messages for a specific key.
|
|
# File 'lib/operandi/messages.rb', line 14
|
#add(key, texts, opts = {}) ⇒ void
This method returns an undefined value.
Add a message to the collection.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/operandi/messages.rb', line 88 def add(key, texts, opts = {}) raise_error("Error must be a non-empty string") unless texts = nil [*texts].each do |text| = text.is_a?(Message) ? text : Message.new(key, text, opts) raise_error("Error must be a non-empty string") unless valid_error_text?(.text) @messages[key] ||= [] @messages[key] << end raise!() break!(opts.key?(:break) ? opts[:break] : .break?) rollback!(opts.key?(:rollback) ? opts[:rollback] : .rollback?) if !opts.key?(:last) || opts[:last] end |
#any? ⇒ Boolean
Check if there are any messages.
|
|
# File 'lib/operandi/messages.rb', line 19
|
#break? ⇒ Boolean
Check if step execution should stop.
110 111 112 |
# File 'lib/operandi/messages.rb', line 110 def break? @break end |
#copy_from(entity, opts = {}) ⇒ void Also known as: from_record
This method returns an undefined value.
Copy messages from another source.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/operandi/messages.rb', line 126 def copy_from(entity, opts = {}) if defined?(ActiveRecord::Base) && entity.is_a?(ActiveRecord::Base) copy_from(entity.errors., opts) elsif entity.is_a?(Operandi::Base) copy_from(entity.errors, opts) elsif entity.respond_to?(:each) last_index = entity.size - 1 entity.each_with_index do |(key, ), index| add(key, , opts.merge(last: index == last_index)) end else raise_error("Don't know how to import errors from #{entity}") end end |
#count ⇒ Integer
Get total count of all messages across all keys.
69 70 71 |
# File 'lib/operandi/messages.rb', line 69 def count @messages.values.sum(&:size) end |
#empty? ⇒ Boolean
Check if the collection is empty.
|
|
# File 'lib/operandi/messages.rb', line 23
|
#key?(key) ⇒ Boolean Also known as: has_key?
Check if a key has messages.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/operandi/messages.rb', line 39 def_delegators :@messages, :[], :any?, :empty?, :size, :keys, :values, :each, :each_with_index, :each_with_object, :key? |
#keys ⇒ Array<Symbol>
Get all keys with messages.
|
|
# File 'lib/operandi/messages.rb', line 31
|
#size ⇒ Integer
Get number of keys with messages.
|
|
# File 'lib/operandi/messages.rb', line 27
|
#to_h ⇒ Hash{Symbol => Array<String>}
Convert messages to a hash with string values.
146 147 148 |
# File 'lib/operandi/messages.rb', line 146 def to_h @messages.to_h.transform_values { |value| value.map(&:to_s) } end |