Class: Dao::Errors
- Inherits:
-
Map
- Object
- Map
- Dao::Errors
- Defined in:
- lib/dao/errors.rb
Defined Under Namespace
Classes: Message
Constant Summary collapse
- Global =
'*'
- Separator =
'⇒'
Class Method Summary collapse
- .cast(*args) ⇒ Object
- .default_errors_to_html(*args) ⇒ Object
- .for(*args, &block) ⇒ Object
- .global_key ⇒ Object
- .to_html(*args, &block) ⇒ Object
Instance Method Summary collapse
- #add(*args) ⇒ Object (also: #add_to_base)
- #add!(*args) ⇒ Object (also: #add_to_base!)
- #clear ⇒ Object
- #clear! ⇒ Object
- #clone ⇒ Object
- #each_full_message ⇒ Object (also: #each_full)
- #each_message ⇒ Object
- #full_messages ⇒ Object
- #invalid?(*keys) ⇒ Boolean (also: #on?)
- #messages ⇒ Object
- #on(*args, &block) ⇒ Object
- #size ⇒ Object (also: #count, #length)
- #to_html(*args) ⇒ Object
- #to_s(*args, &block) ⇒ Object
- #update(other, options = {}) ⇒ Object
Class Method Details
.cast(*args) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/dao/errors.rb', line 40 def cast(*args) if args.size == 1 value = args.first value.is_a?(self) ? value : self.for(value) else self.for(*args) end end |
.default_errors_to_html(*args) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/dao/errors.rb', line 212 def Errors.default_errors_to_html(*args) error = args.shift = Dao.map_for(args.last.is_a?(Hash) ? args.pop : {}) errors = [error, *args].flatten.compact at_least_one_error = false css_class = [:class] || 'dao errors' to_html = table_(:class => css_class){ { "We're so sorry, but can you please fix the following errors?" } errors.each do |e| e..each do |key, | at_least_one_error = true tr_{ td_(:class => :key){ key } td_(:class => :separator){ Separator } td_(:class => :message){ } } end end } at_least_one_error ? to_html : '' end |
.for(*args, &block) ⇒ Object
36 37 38 |
# File 'lib/dao/errors.rb', line 36 def for(*args, &block) new(*args, &block) end |
.global_key ⇒ Object
32 33 34 |
# File 'lib/dao/errors.rb', line 32 def global_key [Global] end |
.to_html(*args, &block) ⇒ Object
204 205 206 207 208 209 210 |
# File 'lib/dao/errors.rb', line 204 def Errors.to_html(*args, &block) if block define_method(:to_html, &block) else default_errors_to_html(*args) end end |
Instance Method Details
#add(*args) ⇒ Object Also known as: add_to_base
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/dao/errors.rb', line 50 def add(*args) = Dao.map_for(args.last.is_a?(Hash) ? args.pop : {}) sticky = [:sticky] clear = [:clear] args.flatten! = args.pop keys = args keys = [Global] if keys.empty? errors = Hash.new if Array(keys) == [Global] sticky = true unless .has_key?(:sticky) end sticky = true if(.respond_to?(:sticky?) and .sticky?) if if .respond_to?(:full_messages) .depth_first_each do |keys, msg| errors[keys] = Message.new(msg, :sticky => sticky) end else errors[keys] = Message.new(, :sticky => sticky) end else raise(ArgumentError, 'no message!') end = Message.new() unless .is_a?(Message) result = [] errors.each do |keys, | list = get(keys) unless get(keys) set(keys => []) list = get(keys) end list.clear if clear list.push() result = list end result end |
#add!(*args) ⇒ Object Also known as: add_to_base!
98 99 100 101 102 103 |
# File 'lib/dao/errors.rb', line 98 def add!(*args) = Dao.map_for(args.last.is_a?(Hash) ? args.pop : {}) [:sticky] = true args.push() add(*args) end |
#clear ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/dao/errors.rb', line 133 def clear keep = [] depth_first_each do |keys, | index = keys.pop args = [keys, ].flatten keep.push(args) if .sticky? end clear! ensure keep.each{|args| add!(*args)} end |
#clear! ⇒ Object
132 |
# File 'lib/dao/errors.rb', line 132 alias_method('clear!', 'clear') |
#clone ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/dao/errors.rb', line 106 def clone clone = Errors.new depth_first_each do |keys, | args = [*keys] args.push() clone.add(*args) end clone end |
#each_full_message ⇒ Object Also known as: each_full
189 190 191 |
# File 'lib/dao/errors.rb', line 189 def .each{|msg| yield msg} end |
#each_message ⇒ Object
181 182 183 184 185 186 187 |
# File 'lib/dao/errors.rb', line 181 def depth_first_each do |keys, | index = keys.pop = .to_s.strip yield(keys, ) end end |
#full_messages ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/dao/errors.rb', line 162 def = [] = [] depth_first_each do |keys, value| index = keys.pop key = keys.join('.') value = value.to_s next if value.strip.empty? if key == Global .push([key, value]) else .push([key, value]) end end + end |
#invalid?(*keys) ⇒ Boolean Also known as: on?
145 146 147 |
# File 'lib/dao/errors.rb', line 145 def invalid?(*keys) has?(keys) and !get(keys).nil? end |
#messages ⇒ Object
194 195 196 197 198 |
# File 'lib/dao/errors.rb', line 194 def = (self[Global]||[]).map{|| }. select{|| not .strip.empty?} end |
#on(*args, &block) ⇒ Object
150 151 152 |
# File 'lib/dao/errors.rb', line 150 def on(*args, &block) get(*args, &block) end |
#size ⇒ Object Also known as: count, length
154 155 156 157 158 |
# File 'lib/dao/errors.rb', line 154 def size size = 0 depth_first_each{ size += 1 } size end |
#to_html(*args) ⇒ Object
200 201 202 |
# File 'lib/dao/errors.rb', line 200 def to_html(*args) Errors.to_html(errors=self, *args) end |
#to_s(*args, &block) ⇒ Object
238 239 240 |
# File 'lib/dao/errors.rb', line 238 def to_s(*args, &block) to_html(*args, &block) end |
#update(other, options = {}) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/dao/errors.rb', line 117 def update(other, = {}) = Dao.map_for() prefix = Array([:prefix]).flatten.compact other.each do |key, val| key = key.to_s if key == 'base' or key == Global add!(val) else key = prefix + [key] unless prefix.empty? add(key, val) end end end |