Class: Dao::Errors

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

Defined Under Namespace

Classes: Message

Constant Summary collapse

Global =
'*'
Separator =
''

Class Method Summary collapse

Instance Method Summary collapse

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
  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



36
37
38
# File 'lib/dao/errors.rb', line 36

def for(*args, &block)
  new(*args, &block)
end

.global_keyObject



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)
  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!



98
99
100
101
102
103
# File 'lib/dao/errors.rb', line 98

def add!(*args)
  options = Dao.map_for(args.last.is_a?(Hash) ? args.pop : {})
  options[:sticky] = true
  args.push(options)
  add(*args)
end

#clearObject



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, 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



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

alias_method('clear!', 'clear')

#cloneObject



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

#each_full_messageObject Also known as: each_full



189
190
191
# File 'lib/dao/errors.rb', line 189

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

#each_messageObject



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

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

#full_messagesObject



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 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)


145
146
147
# File 'lib/dao/errors.rb', line 145

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

#messagesObject



194
195
196
197
198
# File 'lib/dao/errors.rb', line 194

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

#sizeObject 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, 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