Class: Dao::Errors

Inherits:
Map
  • Object
show all
Defined in:
lib/dao/errors.rb

Defined Under Namespace

Classes: KeyPrefixer, Message

Constant Summary collapse

Global =

you can tweak these if you want

'*'
Separator =

this is an “Open-outlined rightward arrow”

"\342\207\222"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Errors

Returns a new instance of Errors.



53
54
55
# File 'lib/dao/errors.rb', line 53

def initialize(*args)
  @object = args.shift
end

Instance Attribute Details

#objectObject

instance methods



51
52
53
# File 'lib/dao/errors.rb', line 51

def object
  @object
end

Class Method Details

.errors_to_html(*args) ⇒ Object



241
242
243
# File 'lib/dao/errors.rb', line 241

def Errors.errors_to_html(*args)
  ::Errors2Html.to_html(*args)
end

.errors_to_text(*args) ⇒ Object



323
324
325
326
327
328
329
330
331
# File 'lib/dao/errors.rb', line 323

def Errors.errors_to_text(*args)
  hash = to_hash(*args)

  if hash.empty?
    nil
  else
    hash.to_yaml
  end
end

.for(*args, &block) ⇒ Object



11
12
13
14
15
16
# File 'lib/dao/errors.rb', line 11

def for(*args, &block)
  if args.size == 1 and args.first.is_a?(Errors)
    return args.first
  end
  new(*args, &block)
end

.global_keyObject



44
45
46
# File 'lib/dao/errors.rb', line 44

def global_key
  [Global]
end

.to_hash(*args) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/dao/errors.rb', line 300

def Errors.to_hash(*args)
  error = args.shift
  options = Map.options_for!(args)
  errors = [error, *args].flatten.compact

  map = Map.new

  errors.each do |e|
    e.full_messages.each do |key, message|
      k = e.key_prefixer.prefix(key)

      map.set(k, []) unless map.has?(k)
      map.get(k).push("#{ message }")
    end
  end

  map.to_hash
end

.to_html(*args, &block) ⇒ Object



233
234
235
236
237
238
239
# File 'lib/dao/errors.rb', line 233

def Errors.to_html(*args, &block)
  if block
    define_method(:to_html, &block)
  else
    errors_to_html(*args)
  end
end

Instance Method Details

#[](key) ⇒ Object



57
58
59
60
# File 'lib/dao/errors.rb', line 57

def [](key)
  return [] unless has_key?(key)
  super
end

#add(*args) ⇒ Object Also known as: add!, add_to_base



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/dao/errors.rb', line 80

def add(*args)
  return relay(args.first) if args.size == 1 and relay?(args.first)
  options = Map.options_for!(args)

  clear = options[:clear]
  source = options[:source]

  args.flatten!

  message = args.pop or raise(ArgumentError, 'no message!')
  key = args.empty? ? [Global] : args

  if message.is_a?(Hash) or message.respond_to?(:full_messages)
    message.each do |k, v|
      Array(v).each do |msg|
        add(key + [k], msg)
      end
    end
    return(self)
  end

  message = message.is_a?(Message) ? message : Message.new(message)
  message.source = source

  set(key => []) unless has?(key)
  list = get(key)
  list.clear if clear
  list.push(message)
  list.uniq!
  list
  self
end

#add_from_source(keys, callback, message) ⇒ Object



138
139
140
141
# File 'lib/dao/errors.rb', line 138

def add_from_source(keys, callback, message)
  add(keys, message, :source => callback)
  self
end

#clearObject Also known as: clear!



151
152
153
# File 'lib/dao/errors.rb', line 151

def clear
  super
end

#delete_from_source(keys, callback) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/dao/errors.rb', line 143

def delete_from_source(keys, callback)
  if((messages = errors.on(keys)))
    messages.delete_if{|m| m.respond_to?(:source) and m.source==callback}
    rm(*keys) if messages.empty?
  end
  self
end

#each_full_messageObject Also known as: each_full



207
208
209
# File 'lib/dao/errors.rb', line 207

def each_full_message
  full_messages.each{|msg| yield msg}
end

#each_messageObject



187
188
189
190
191
192
193
# File 'lib/dao/errors.rb', line 187

def each_message
  depth_first_each do |keys, message|
    index = keys.pop
    message = message.to_s.strip
    yield(keys, message)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/dao/errors.rb', line 76

def empty?
  size == 0
end

#errorsObject



62
63
64
# File 'lib/dao/errors.rb', line 62

def errors
  self
end

#flattenObject



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/dao/errors.rb', line 195

def flatten
  hash = Hash.new

  depth_first_each do |keys, value|
    index = keys.pop
    hash[keys] ||= []
    hash[keys].push("#{ value }")
  end

  hash
end

#full_messagesObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/dao/errors.rb', line 166

def full_messages
  global_messages = []
  full_messages = []

  depth_first_each do |keys, value|
    index = keys.pop
    key = keys
    value = value.to_s

    next if value.strip.empty?

    if key == Global
      global_messages.push([key, value])
    else
      full_messages.push([key, value])
    end
  end

  global_messages + full_messages
end

#globalObject



219
220
221
# File 'lib/dao/errors.rb', line 219

def global
  reject{|k, v| k != Global}
end

#invalid?(*keys) ⇒ Boolean Also known as: on?

Returns:

  • (Boolean)


156
157
158
# File 'lib/dao/errors.rb', line 156

def invalid?(*keys)
  has?(keys) and !get(keys).nil?
end

#key_prefixerObject



292
293
294
# File 'lib/dao/errors.rb', line 292

def key_prefixer
  @key_prefixer ||= KeyPrefixer.new(object)
end

#localObject



223
224
225
# File 'lib/dao/errors.rb', line 223

def local
  reject{|k, v| k == Global}
end

#messagesObject



213
214
215
216
217
# File 'lib/dao/errors.rb', line 213

def messages
  messages =
    (self[Global]||[]).map{|message| message}.
    select{|message| not message.strip.empty?}
end

#on(*args, &block) ⇒ Object



162
163
164
# File 'lib/dao/errors.rb', line 162

def on(*args, &block)
  get(*args, &block)
end

#prefix_key(key) ⇒ Object



296
297
298
# File 'lib/dao/errors.rb', line 296

def prefix_key(key)
  key_prefixer.prefix(key)
end

#relay(*args) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/dao/errors.rb', line 116

def relay(*args)
  options = args.size > 1 ? Map.options_for!(args) : Map.new

  prefix = Array(options.delete(:prefix))

  args.flatten.compact.each do |source|
    errors = source.respond_to?(:errors) ? source.errors : source

    errors.each do |*argv|
      msgs = Array(argv.pop)
      key = prefix + Array(argv.pop)
      msgs.each{|msg| add(Array(options[:key] || key), msg, options)}
    end
  end

  self
end

#relay?(arg) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/dao/errors.rb', line 134

def relay?(arg)
  [:each, :each_pair, :each_slice].any?{|method| arg.respond_to?(method)}
end

#sizeObject Also known as: count, length



66
67
68
69
70
71
72
# File 'lib/dao/errors.rb', line 66

def size
  size = 0
  depth_first_each do |keys, value|
    size += Array(value).size
  end
  size
end

#to_hashObject



319
320
321
# File 'lib/dao/errors.rb', line 319

def to_hash
  Errors.to_hash(self)
end

#to_html(*args) ⇒ Object

html generation methods



229
230
231
# File 'lib/dao/errors.rb', line 229

def to_html(*args)
  Errors.to_html(errors=self, *args)
end

#to_s(format = :html, *args, &block) ⇒ Object



245
246
247
248
249
250
251
252
253
# File 'lib/dao/errors.rb', line 245

def to_s(format = :html, *args, &block)
  case format.to_s
    when /html/
      to_html(*args, &block)

    when /text/
      to_text(*args, &block)
  end
end

#to_textObject



333
334
335
# File 'lib/dao/errors.rb', line 333

def to_text
  Errors.errors_to_text(self)
end