Class: Dao::Errors
- Inherits:
-
Map
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
#object ⇒ Object
51
52
53
|
# File 'lib/dao/errors.rb', line 51
def object
@object
end
|
Class Method Details
.errors_to_html(*args) ⇒ Object
.errors_to_text(*args) ⇒ Object
329
330
331
332
333
334
335
336
337
|
# File 'lib/dao/errors.rb', line 329
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_key ⇒ Object
44
45
46
|
# File 'lib/dao/errors.rb', line 44
def global_key
[Global]
end
|
.to_hash(*args) ⇒ Object
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
# File 'lib/dao/errors.rb', line 306
def Errors.to_hash(*args)
error = args.shift
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
240
241
242
243
244
245
246
|
# File 'lib/dao/errors.rb', line 240
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
|
# 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!
self
end
|
#add_from_source(keys, callback, message) ⇒ Object
146
147
148
149
|
# File 'lib/dao/errors.rb', line 146
def add_from_source(keys, callback, message)
add(keys, message, :source => callback)
self
end
|
#clear ⇒ Object
Also known as:
clear!
159
160
161
|
# File 'lib/dao/errors.rb', line 159
def clear
super
end
|
#delete_from_source(keys, callback) ⇒ Object
151
152
153
154
155
156
157
|
# File 'lib/dao/errors.rb', line 151
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_message ⇒ Object
Also known as:
each_full
215
216
217
|
# File 'lib/dao/errors.rb', line 215
def each_full_message
full_messages.each{|msg| yield msg}
end
|
#each_message ⇒ Object
195
196
197
198
199
200
201
|
# File 'lib/dao/errors.rb', line 195
def each_message
depth_first_each do |keys, message|
_ = keys.pop
message = message.to_s.strip
yield(keys, message)
end
end
|
#empty? ⇒ Boolean
76
77
78
|
# File 'lib/dao/errors.rb', line 76
def empty?
size == 0
end
|
#errors ⇒ Object
62
63
64
|
# File 'lib/dao/errors.rb', line 62
def errors
self
end
|
#flatten ⇒ Object
203
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/dao/errors.rb', line 203
def flatten
hash = Hash.new
depth_first_each do |keys, value|
_ = keys.pop
hash[keys] ||= []
hash[keys].push("#{ value }")
end
hash
end
|
#full_messages ⇒ Object
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/dao/errors.rb', line 174
def full_messages
global_messages = []
full_messages = []
depth_first_each do |keys, value|
_ = 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
|
#global ⇒ Object
226
227
228
|
# File 'lib/dao/errors.rb', line 226
def global
reject{|k, v| k != Global}
end
|
#invalid?(*keys) ⇒ Boolean
Also known as:
on?
164
165
166
|
# File 'lib/dao/errors.rb', line 164
def invalid?(*keys)
has?(keys) and !get(keys).nil?
end
|
#key_prefixer ⇒ Object
298
299
300
|
# File 'lib/dao/errors.rb', line 298
def key_prefixer
@key_prefixer ||= KeyPrefixer.new(object)
end
|
#local ⇒ Object
230
231
232
|
# File 'lib/dao/errors.rb', line 230
def local
reject{|k, v| k == Global}
end
|
#messages ⇒ Object
221
222
223
224
|
# File 'lib/dao/errors.rb', line 221
def messages
(self[Global] || []).map{|message| message}
.select{|message| not message.strip.empty?}
end
|
#on(*args, &block) ⇒ Object
170
171
172
|
# File 'lib/dao/errors.rb', line 170
def on(*args, &block)
get(*args, &block)
end
|
#prefix_key(key) ⇒ Object
302
303
304
|
# File 'lib/dao/errors.rb', line 302
def prefix_key(key)
key_prefixer.prefix(key)
end
|
#relay(*args) ⇒ Object
FIXME - this should accept an errors object
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# 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
142
143
144
|
# File 'lib/dao/errors.rb', line 142
def relay?(arg)
[:each, :each_pair, :each_slice].any?{|method| arg.respond_to?(method)}
end
|
#size ⇒ Object
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_hash ⇒ Object
325
326
327
|
# File 'lib/dao/errors.rb', line 325
def to_hash
Errors.to_hash(self)
end
|
#to_html(*args) ⇒ Object
236
237
238
|
# File 'lib/dao/errors.rb', line 236
def to_html(*args)
Errors.to_html(self, *args)
end
|
#to_s(format = :html, *args, &block) ⇒ Object
252
253
254
255
256
257
258
259
260
|
# File 'lib/dao/errors.rb', line 252
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
|