Class: FlowCore::Violations

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/flow_core/violations.rb

Constant Summary collapse

MESSAGE_OPTIONS =
i[message].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeViolations

Returns a new instance of Violations.



16
17
18
19
20
# File 'lib/flow_core/violations.rb', line 16

def initialize
  @messages = apply_default_array({})
  @details = apply_default_array({})
  @records = apply_default_array({})
end

Instance Attribute Details

#detailsObject (readonly)

Returns the value of attribute details.



14
15
16
# File 'lib/flow_core/violations.rb', line 14

def details
  @details
end

#messagesObject (readonly)

Returns the value of attribute messages.



14
15
16
# File 'lib/flow_core/violations.rb', line 14

def messages
  @messages
end

#recordsObject (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}"
  messages[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, message = :invalid, options = {})
  message = message.call if message.respond_to?(:call)
  detail  = normalize_detail(message, options)
  message = normalize_message(record, message, options)
  n_record = normalize_record(record)

  record_key = "#{record.class}/#{record.id}"
  details[record_key] << detail
  messages[record_key] << message
  records[record_key] = n_record

  if exception = options[:strict]
    exception = FlowCore::StrictViolationFailed if exception == true
    raise exception, full_message(record_key, message)
  end
end

#added?(record, message = :invalid, options = {}) ⇒ Boolean

Returns:



139
140
141
142
143
144
145
146
147
148
# File 'lib/flow_core/violations.rb', line 139

def added?(record, message = :invalid, options = {})
  message = message.call if message.respond_to?(:call)

  record_key = "#{record.class}/#{record.id}"
  if message.is_a? Symbol
    details[record_key].include? normalize_detail(message, options)
  else
    self[record_key].include? message
  end
end

#as_json(options = nil) ⇒ Object



108
109
110
# File 'lib/flow_core/violations.rb', line 108

def as_json(options = nil)
  to_hash(options && options[:full_messages])
end

#clearObject



48
49
50
51
52
# File 'lib/flow_core/violations.rb', line 48

def clear
  messages.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:
  @messages = other.messages.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)
  messages.delete(record)
  records.delete(record)
end

#eachObject



73
74
75
76
77
78
79
80
# File 'lib/flow_core/violations.rb', line 73

def each
  messages.each_key do |record_key|
    model = records[record_key][:model]
    id = records[record_key][:id]
    name = records[record_key][:name]
    messages[record_key].each { |error| yield record_key, model, id, name, error }
  end
end

#empty?Boolean Also known as: blank?

Returns:



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 full_message(record_key, message)
  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: message)
end

#full_messagesObject Also known as: to_a



161
162
163
164
165
# File 'lib/flow_core/violations.rb', line 161

def full_messages
  map do |record_key, _, _, _, message|
    full_message(record_key, message)
  end
end

#full_messages_for(record) ⇒ Object



168
169
170
171
# File 'lib/flow_core/violations.rb', line 168

def full_messages_for(record)
  record_key = "#{record.class}/#{record.id}"
  messages[record].map { |message| full_message(record_key, message) }
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 generate_message(record, type = :invalid, options = {})
  type = options.delete(:message) if options[:message].is_a?(Symbol)

  options = {
    model: record.class,
    model_name: record.model_name.human,
    id: record.id,
    name: record.name
  }.merge!(options)

  options[:default] = options.delete(:message) if options[:message]

  I18n.translate("flow_core.violations.#{type}", options)
end

#include?(record) ⇒ Boolean Also known as: has_key?, key?

Returns:



54
55
56
57
# File 'lib/flow_core/violations.rb', line 54

def include?(record)
  record = "#{record.class}/#{record.id}"
  messages.key?(record) && messages[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(@messages)
  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:
  @messages = other.messages.dup
  @details  = other.details.deep_dup
  @records = other.records.deep_dup
  super
end

#keysObject



93
94
95
96
97
# File 'lib/flow_core/violations.rb', line 93

def keys
  messages.reject do |_key, value|
    value.empty?
  end.keys
end

#marshal_dumpObject

:nodoc:



201
202
203
# File 'lib/flow_core/violations.rb', line 201

def marshal_dump # :nodoc:
  [without_default_proc(@messages), 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:
  @messages, @details, @records = array
  apply_default_array(@messages)
  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)
  @messages.merge!(other.messages) { |_, 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

Returns:



150
151
152
153
154
155
156
157
158
159
# File 'lib/flow_core/violations.rb', line 150

def of_kind?(record, message = :invalid)
  message = message.call if message.respond_to?(:call)

  record_key = "#{record.class}/#{record.id}"
  if message.is_a? Symbol
    details[record_key].map { |e| e[:error] }.include? message
  else
    self[record_key].include? message
  end
end

#sizeObject 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)
  @messages.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(full_messages = false)
  if full_messages
    messages.each_with_object({}) do |(record_key, array), messages|
      messages[record_key] = array.map { |message| full_message(record_key, message) }
    end
  else
    without_default_proc(messages)
  end
end

#to_xml(options = {}) ⇒ Object



104
105
106
# File 'lib/flow_core/violations.rb', line 104

def to_xml(options = {})
  to_a.to_xml({ root: "errors", skip_types: true }.merge!(options))
end

#valuesObject



87
88
89
90
91
# File 'lib/flow_core/violations.rb', line 87

def values
  messages.reject do |_key, value|
    value.empty?
  end.values
end