motion-prime/models/errors.rb in motion-prime-0.1.7 vs motion-prime/models/errors.rb in motion-prime-0.2.0

- old
+ new

@@ -1,3 +1,64 @@ module MotionPrime - class StoreError < StandardError; end + 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) + @keys << key.to_sym unless @keys.include?(key.to_sym) + @errors[key.to_sym] ||= [] + end + + def get(key) + initialize_for_key(key) + @errors[key.to_sym] + end + + def set(key, errors) + initialize_for_key(key) + @errors[key.to_sym] = Array.wrap(errors) + end + + def add(key, error) + initialize_for_key(key) + @errors[key.to_sym] << error + end + + def [](key) + get(key) + end + + def []=(key, errors) + set(key, errors) + end + + def reset + @keys.each do |key| + set(key, []) + end + end + + def messages + errors.values.compact.flatten + end + + def blank? + messages.blank? + end + + def present? + !blank? + end + + def to_s + messages.join(';') + end + end end \ No newline at end of file