motion-prime/models/errors.rb in motion-prime-0.2.0 vs motion-prime/models/errors.rb in motion-prime-0.2.1
- old
+ new
@@ -1,36 +1,37 @@
module MotionPrime
class Errors
attr_accessor :keys
- attr_accessor :errors
def initialize(model)
@keys = []
- @errors = {}
model.class.attributes.map(&:to_sym).each do |key|
initialize_for_key key
end
end
def initialize_for_key(key)
+ return if @keys.include?(key.to_sym)
@keys << key.to_sym unless @keys.include?(key.to_sym)
- @errors[key.to_sym] ||= []
+ unless instance_variable_get("@#{key}")
+ instance_variable_set("@#{key}", [])
+ end
+ self.class.send :attr_accessor, key.to_sym
end
def get(key)
initialize_for_key(key)
- @errors[key.to_sym]
+ send(:"#{key.to_sym}")
end
def set(key, errors)
initialize_for_key(key)
- @errors[key.to_sym] = Array.wrap(errors)
+ send :"#{key.to_sym}=", Array.wrap(errors)
end
def add(key, error)
- initialize_for_key(key)
- @errors[key.to_sym] << error
+ get(key) << error
end
def [](key)
get(key)
end
@@ -44,10 +45,10 @@
set(key, [])
end
end
def messages
- errors.values.compact.flatten
+ @keys.map{ |k| get(k)}.compact.flatten
end
def blank?
messages.blank?
end
\ No newline at end of file