Class: FlowCore::Violations
- Inherits:
-
Object
- Object
- FlowCore::Violations
- Includes:
- Enumerable
- Defined in:
- lib/flow_core/violations.rb
Constant Summary collapse
- MESSAGE_OPTIONS =
i[].freeze
Instance Attribute Summary collapse
-
#details ⇒ Object
readonly
Returns the value of attribute details.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
Instance Method Summary collapse
- #[](record) ⇒ Object
- #add(record, message = :invalid, options = {}) ⇒ Object
- #added?(record, message = :invalid, options = {}) ⇒ Boolean
- #as_json(options = nil) ⇒ Object
- #clear ⇒ Object
-
#copy!(other) ⇒ Object
:nodoc:.
- #delete(record) ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean (also: #blank?)
- #full_message(record_key, message) ⇒ Object
- #full_messages ⇒ Object (also: #to_a)
- #full_messages_for(record) ⇒ Object
- #generate_message(record, type = :invalid, options = {}) ⇒ Object
- #include?(record) ⇒ Boolean (also: #has_key?, #key?)
-
#init_with(coder) ⇒ Object
:nodoc:.
-
#initialize ⇒ Violations
constructor
A new instance of Violations.
-
#initialize_dup(other) ⇒ Object
:nodoc:.
- #keys ⇒ Object
-
#marshal_dump ⇒ Object
:nodoc:.
-
#marshal_load(array) ⇒ Object
:nodoc:.
- #merge!(other) ⇒ Object
- #of_kind?(record, message = :invalid) ⇒ Boolean
- #size ⇒ Object (also: #count)
- #slice!(*keys) ⇒ Object
- #to_hash(full_messages = false) ⇒ Object
- #to_xml(options = {}) ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize ⇒ Violations
Returns a new instance of Violations.
16 17 18 19 20 |
# File 'lib/flow_core/violations.rb', line 16 def initialize = apply_default_array({}) @details = apply_default_array({}) @records = apply_default_array({}) end |
Instance Attribute Details
#details ⇒ Object (readonly)
Returns the value of attribute details.
14 15 16 |
# File 'lib/flow_core/violations.rb', line 14 def details @details end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
14 15 16 |
# File 'lib/flow_core/violations.rb', line 14 def end |
#records ⇒ Object (readonly)
Returns the value of attribute records.
14 15 16 |
# File 'lib/flow_core/violations.rb', line 14 def records @records end |
Instance Method Details
#[](record) ⇒ Object
68 69 70 71 |
# File 'lib/flow_core/violations.rb', line 68 def [](record) record = "#{record.class}/#{record.id}" [record] end |
#add(record, message = :invalid, options = {}) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/flow_core/violations.rb', line 122 def add(record, = :invalid, = {}) = .call if .respond_to?(:call) detail = normalize_detail(, ) = (record, , ) n_record = normalize_record(record) record_key = "#{record.class}/#{record.id}" details[record_key] << detail [record_key] << records[record_key] = n_record if exception = [:strict] exception = FlowCore::StrictViolationFailed if exception == true raise exception, (record_key, ) end end |
#added?(record, message = :invalid, options = {}) ⇒ Boolean
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/flow_core/violations.rb', line 139 def added?(record, = :invalid, = {}) = .call if .respond_to?(:call) record_key = "#{record.class}/#{record.id}" if .is_a? Symbol details[record_key].include? normalize_detail(, ) else self[record_key].include? end end |
#as_json(options = nil) ⇒ Object
108 109 110 |
# File 'lib/flow_core/violations.rb', line 108 def as_json( = nil) to_hash( && [:full_messages]) end |
#clear ⇒ Object
48 49 50 51 52 |
# File 'lib/flow_core/violations.rb', line 48 def clear .clear details.clear records.clear end |
#copy!(other) ⇒ Object
:nodoc:
29 30 31 32 33 |
# File 'lib/flow_core/violations.rb', line 29 def copy!(other) # :nodoc: = other..dup @details = other.details.dup @records = other.records.dup end |
#delete(record) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/flow_core/violations.rb', line 61 def delete(record) record = "#{record.class}/#{record.id}" details.delete(record) .delete(record) records.delete(record) end |
#each ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/flow_core/violations.rb', line 73 def each .each_key do |record_key| model = records[record_key][:model] id = records[record_key][:id] name = records[record_key][:name] [record_key].each { |error| yield record_key, model, id, name, error } end end |
#empty? ⇒ Boolean Also known as: blank?
99 100 101 |
# File 'lib/flow_core/violations.rb', line 99 def empty? size.zero? end |
#full_message(record_key, message) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/flow_core/violations.rb', line 173 def (record_key, ) model = records[record_key][:model] model_name = records[record_key][:model].model_name.human id = records[record_key][:id] name = records[record_key][:name] defaults = [:"flow_core.violations.format"] defaults << "%<name>s %<message>s" I18n.t(defaults.shift, default: defaults, model: model, model_name: model_name, id: id, name: name, message: ) end |
#full_messages ⇒ Object Also known as: to_a
161 162 163 164 165 |
# File 'lib/flow_core/violations.rb', line 161 def map do |record_key, _, _, _, | (record_key, ) end end |
#full_messages_for(record) ⇒ Object
168 169 170 171 |
# File 'lib/flow_core/violations.rb', line 168 def (record) record_key = "#{record.class}/#{record.id}" [record].map { || (record_key, ) } end |
#generate_message(record, type = :invalid, options = {}) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/flow_core/violations.rb', line 186 def (record, type = :invalid, = {}) type = .delete(:message) if [:message].is_a?(Symbol) = { model: record.class, model_name: record.model_name.human, id: record.id, name: record.name }.merge!() [:default] = .delete(:message) if [:message] I18n.translate("flow_core.violations.#{type}", ) end |
#include?(record) ⇒ Boolean Also known as: has_key?, key?
54 55 56 57 |
# File 'lib/flow_core/violations.rb', line 54 def include?(record) record = "#{record.class}/#{record.id}" .key?(record) && [record].present? end |
#init_with(coder) ⇒ Object
:nodoc:
212 213 214 215 216 217 218 |
# File 'lib/flow_core/violations.rb', line 212 def init_with(coder) # :nodoc: coder.map.each { |k, v| instance_variable_set(:"@#{k}", v) } @details ||= {} apply_default_array() apply_default_array(@details) apply_default_array(@records) end |
#initialize_dup(other) ⇒ Object
:nodoc:
22 23 24 25 26 27 |
# File 'lib/flow_core/violations.rb', line 22 def initialize_dup(other) # :nodoc: = other..dup @details = other.details.deep_dup @records = other.records.deep_dup super end |
#keys ⇒ Object
93 94 95 96 97 |
# File 'lib/flow_core/violations.rb', line 93 def keys .reject do |_key, value| value.empty? end.keys end |
#marshal_dump ⇒ Object
:nodoc:
201 202 203 |
# File 'lib/flow_core/violations.rb', line 201 def marshal_dump # :nodoc: [without_default_proc(), without_default_proc(@details), without_default_proc(@records)] end |
#marshal_load(array) ⇒ Object
:nodoc:
205 206 207 208 209 210 |
# File 'lib/flow_core/violations.rb', line 205 def marshal_load(array) # :nodoc: , @details, @records = array apply_default_array() apply_default_array(@details) apply_default_array(@records) end |
#merge!(other) ⇒ Object
35 36 37 38 39 |
# File 'lib/flow_core/violations.rb', line 35 def merge!(other) .merge!(other.) { |_, ary1, ary2| ary1 + ary2 } @details.merge!(other.details) { |_, ary1, ary2| ary1 + ary2 } @records.merge!(other.records) { |_, ary1, ary2| ary1 + ary2 } end |
#of_kind?(record, message = :invalid) ⇒ Boolean
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/flow_core/violations.rb', line 150 def of_kind?(record, = :invalid) = .call if .respond_to?(:call) record_key = "#{record.class}/#{record.id}" if .is_a? Symbol details[record_key].map { |e| e[:error] }.include? else self[record_key].include? end end |
#size ⇒ Object Also known as: count
82 83 84 |
# File 'lib/flow_core/violations.rb', line 82 def size values.flatten.size end |
#slice!(*keys) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/flow_core/violations.rb', line 41 def slice!(*keys) keys = keys.map(&:to_sym) @details.slice!(*keys) .slice!(*keys) @records.slice!(*keys) end |
#to_hash(full_messages = false) ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/flow_core/violations.rb', line 112 def to_hash( = false) if .each_with_object({}) do |(record_key, array), | [record_key] = array.map { || (record_key, ) } end else without_default_proc() end end |
#to_xml(options = {}) ⇒ Object
104 105 106 |
# File 'lib/flow_core/violations.rb', line 104 def to_xml( = {}) to_a.to_xml({ root: "errors", skip_types: true }.merge!()) end |
#values ⇒ Object
87 88 89 90 91 |
# File 'lib/flow_core/violations.rb', line 87 def values .reject do |_key, value| value.empty? end.values end |