Class: Dao::Errors

Inherits:
Map
  • Object
show all
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

Instance Method Summary collapse

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
  options = Dao.map_for(args.last.is_a?(Hash) ? args.pop : {})
  errors = [error, *args].flatten.compact

  at_least_one_error = false
  css_class = options[:class] || 'dao errors'

  to_html =
    table_(:class => css_class){
      caption_{ "We're so sorry, but can you please fix the following errors?" }
      errors.each do |e|
        e.full_messages.each do |key, message|
          at_least_one_error = true
          tr_{
            td_(:class => :key){ key }
            td_(:class => :separator){ Separator }
            td_(:class => :message){ 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_keyObject



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)
  options = Dao.map_for(args.last.is_a?(Hash) ? args.pop : {})
  sticky = options[:sticky]
  clear = options[:clear]

  args.flatten!
  message = args.pop
  keys = args
  keys = [Global] if keys.empty?
  errors = Hash.new

  if Array(keys) == [Global]
    sticky = true unless options.has_key?(:sticky)
  end

  sticky = true if(message.respond_to?(:sticky?) and message.sticky?)

  if message
    if message.respond_to?(:full_messages)
      message.depth_first_each do |keys, msg|
        errors[keys] = Message.new(msg, :sticky => sticky)
      end
    else
      errors[keys] = Message.new(message, :sticky => sticky)
    end
  else
    raise(ArgumentError, 'no message!')
  end

  message = Message.new(message) unless message.is_a?(Message)

  result = []

  errors.each do |keys, message|
    list = get(keys)
    unless get(keys)
      set(keys => [])
      list = get(keys)
    end
    list.clear if clear
    list.push(message)
    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)
  options = Dao.map_for(args.last.is_a?(Hash) ? args.pop : {})
  options[:sticky] = true
  args.push(options)
  add(*args)
end

#clearObject



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, message|
    index = keys.pop
    args = [keys, message].flatten
    keep.push(args) if message.sticky?
  end
  clear!
ensure
  keep.each{|args| add!(*args)}
end

#clear!Object



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

alias_method('clear!', 'clear')

#cloneObject



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, message|
    args = [*keys]
    args.push(message)
    clone.add(*args)
  end
  clone
end

#each_full_messageObject Also known as: each_full



191
192
193
# File 'lib/dao/errors.rb', line 191

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

#each_messageObject



183
184
185
186
187
188
189
# File 'lib/dao/errors.rb', line 183

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

#full_messagesObject



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 full_messages
  global_messages = []
  full_messages = []

  depth_first_each do |keys, value|
    index = keys.pop
    key = keys.join('.')
    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

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

Returns:

  • (Boolean)


147
148
149
# File 'lib/dao/errors.rb', line 147

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

#messagesObject



196
197
198
199
200
# File 'lib/dao/errors.rb', line 196

def messages
  messages =
    (self[Global]||[]).map{|message| message}.
    select{|message| not message.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

#sizeObject 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, options = {})
  options = Dao.map_for(options)
  prefix = Array(options[: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