Class: MotionPrime::Errors

Inherits:
Object
  • Object
show all
Defined in:
motion-prime/models/errors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#changesObject (readonly)

Returns the value of attribute changes.



4
5
6
# File 'motion-prime/models/errors.rb', line 4

def changes
  @changes
end

#infoObject

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, options = {})
  initialize_for_key(key)
  track_changed do
    to_hash[key] << error
  end
end

#blank?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'motion-prime/models/errors.rb', line 65

def blank?
  messages.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

#messagesObject



61
62
63
# File 'motion-prime/models/errors.rb', line 61

def messages
  to_hash.values.flatten
end

#present?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'motion-prime/models/errors.rb', line 69

def present?
  !blank?
end

#resetObject



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, options = {})
  track_changed options 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, options = {})
  initialize_for_key(key)

  track_changed options do
    to_hash[key] = Array.wrap(errors)
  end
end

#to_hashObject



15
16
17
# File 'motion-prime/models/errors.rb', line 15

def to_hash
  @info
end

#to_sObject



73
74
75
# File 'motion-prime/models/errors.rb', line 73

def to_s
  messages.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(options = {})
  return yield if options[: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