Class: Dao::Errors
- Inherits:
-
Map
- Object
- Map
- Dao::Errors
- Defined in:
- lib/dao/errors.rb
Defined Under Namespace
Classes: Message
Constant Summary collapse
- Global =
you can tweak these if you want
'*'
- Separator =
Separator = ‘⇒’ unless defined?(Separator)
"\342\207\222"
Class Method Summary collapse
- .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)
instance methods.
- #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
html generation methods.
- #to_s(*args, &block) ⇒ Object
- #update(other, options = {}) ⇒ Object
Class Method Details
.default_errors_to_html(*args) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/dao/errors.rb', line 216 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
46 47 48 |
# File 'lib/dao/errors.rb', line 46 def for(*args, &block) new(*args, &block) end |
.global_key ⇒ Object
42 43 44 |
# File 'lib/dao/errors.rb', line 42 def global_key [Global] end |
.to_html(*args, &block) ⇒ Object
208 209 210 211 212 213 214 |
# File 'lib/dao/errors.rb', line 208 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
instance methods
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 96 97 98 |
# File 'lib/dao/errors.rb', line 53 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!
101 102 103 104 105 106 |
# File 'lib/dao/errors.rb', line 101 def add!(*args) = Dao.map_for(args.last.is_a?(Hash) ? args.pop : {}) [:sticky] = true args.push() add(*args) end |
#clear ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/dao/errors.rb', line 135 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
134 |
# File 'lib/dao/errors.rb', line 134 alias_method('clear!', 'clear') |
#clone ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/dao/errors.rb', line 109 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
191 192 193 |
# File 'lib/dao/errors.rb', line 191 def .each{|msg| yield msg} end |
#each_message ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/dao/errors.rb', line 183 def depth_first_each do |keys, | index = keys.pop = .to_s.strip yield(keys, ) end end |
#full_messages ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/dao/errors.rb', line 164 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?
147 148 149 |
# File 'lib/dao/errors.rb', line 147 def invalid?(*keys) has?(keys) and !get(keys).nil? end |
#messages ⇒ Object
196 197 198 199 200 |
# File 'lib/dao/errors.rb', line 196 def = (self[Global]||[]).map{|| }. select{|| not .strip.empty?} end |
#on(*args, &block) ⇒ Object
152 153 154 |
# File 'lib/dao/errors.rb', line 152 def on(*args, &block) get(*args, &block) end |
#size ⇒ Object Also known as: count, length
156 157 158 159 160 |
# File 'lib/dao/errors.rb', line 156 def size size = 0 depth_first_each{ size += 1 } size end |
#to_html(*args) ⇒ Object
html generation methods
204 205 206 |
# File 'lib/dao/errors.rb', line 204 def to_html(*args) Errors.to_html(errors=self, *args) end |
#to_s(*args, &block) ⇒ Object
242 243 244 |
# File 'lib/dao/errors.rb', line 242 def to_s(*args, &block) to_html(*args, &block) end |
#update(other, options = {}) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/dao/errors.rb', line 119 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 |