Class: MotionPrime::Errors
- Inherits:
-
Object
- Object
- MotionPrime::Errors
- Defined in:
- motion-prime/models/errors.rb
Instance Attribute Summary collapse
-
#changes ⇒ Object
readonly
Returns the value of attribute changes.
-
#info ⇒ Object
Returns the value of attribute info.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, errors) ⇒ Object
- #add(key, error, options = {}) ⇒ Object
- #blank? ⇒ Boolean
- #get(key) ⇒ Object
-
#initialize(model) ⇒ Errors
constructor
A new instance of Errors.
- #messages ⇒ Object
- #present? ⇒ Boolean
- #reset ⇒ Object
- #reset_for(key, options = {}) ⇒ Object
- #set(key, errors, options = {}) ⇒ Object
- #to_hash ⇒ Object
- #to_s ⇒ Object
- #track_changed(options = {}) ⇒ Object
Constructor Details
#initialize(model) ⇒ Errors
Returns a new instance of Errors.
6 7 8 9 10 11 12 13 |
# File 'motion-prime/models/errors.rb', line 6 def initialize(model) @info = MotionSupport::HashWithIndifferentAccess.new @changes = MotionSupport::HashWithIndifferentAccess.new @model = model model.class.attributes.map(&:to_sym).each do |key| initialize_for_key key end end |
Instance Attribute Details
#changes ⇒ Object (readonly)
Returns the value of attribute changes.
4 5 6 |
# File 'motion-prime/models/errors.rb', line 4 def changes @changes end |
#info ⇒ Object
Returns the value of attribute info.
3 4 5 |
# File 'motion-prime/models/errors.rb', line 3 def info @info end |
Instance Method Details
#[](key) ⇒ Object
39 40 41 |
# File 'motion-prime/models/errors.rb', line 39 def [](key) get(key) end |
#[]=(key, errors) ⇒ Object
43 44 45 |
# File 'motion-prime/models/errors.rb', line 43 def []=(key, errors) set(key, errors) end |
#add(key, error, options = {}) ⇒ Object
32 33 34 35 36 37 |
# File 'motion-prime/models/errors.rb', line 32 def add(key, error, = {}) initialize_for_key(key) track_changed do to_hash[key] << error end end |
#blank? ⇒ Boolean
65 66 67 |
# File 'motion-prime/models/errors.rb', line 65 def blank? .none? end |
#get(key) ⇒ Object
19 20 21 22 |
# File 'motion-prime/models/errors.rb', line 19 def get(key) initialize_for_key(key) to_hash[key] end |
#messages ⇒ Object
61 62 63 |
# File 'motion-prime/models/errors.rb', line 61 def to_hash.values.flatten end |
#present? ⇒ Boolean
69 70 71 |
# File 'motion-prime/models/errors.rb', line 69 def present? !blank? end |
#reset ⇒ Object
53 54 55 56 57 58 59 |
# File 'motion-prime/models/errors.rb', line 53 def reset track_changed do to_hash.keys.each do |key| reset_for(key, silent: true) end end end |
#reset_for(key, options = {}) ⇒ Object
47 48 49 50 51 |
# File 'motion-prime/models/errors.rb', line 47 def reset_for(key, = {}) track_changed do to_hash[key] = [] end end |
#set(key, errors, options = {}) ⇒ Object
24 25 26 27 28 29 30 |
# File 'motion-prime/models/errors.rb', line 24 def set(key, errors, = {}) initialize_for_key(key) track_changed do to_hash[key] = Array.wrap(errors) end end |
#to_hash ⇒ Object
15 16 17 |
# File 'motion-prime/models/errors.rb', line 15 def to_hash @info end |
#to_s ⇒ Object
73 74 75 |
# File 'motion-prime/models/errors.rb', line 73 def to_s .join(';') end |
#track_changed(options = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'motion-prime/models/errors.rb', line 77 def track_changed( = {}) return yield if [:silent] @changes = MotionSupport::HashWithIndifferentAccess.new saved_info = to_hash.clone willChangeValueForKey(:info) yield to_hash.each do |key, value| @changes[key] = [value, saved_info[key]] unless value == saved_info[key] end didChangeValueForKey(:info) end |