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.



5
6
7
8
9
10
11
# File 'motion-prime/models/errors.rb', line 5

def initialize(model)
  @_unique_keys = []
  @model = model
  model.class.attributes.map(&:to_sym).each do |key|
    initialize_for_key key
  end
end

Instance Attribute Details

#_unique_keysObject

Returns the value of attribute _unique_keys.



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

def _unique_keys
  @_unique_keys
end

Instance Method Details

#[](key) ⇒ Object



41
42
43
# File 'motion-prime/models/errors.rb', line 41

def [](key)
  get(key)
end

#[]=(key, errors) ⇒ Object



45
46
47
# File 'motion-prime/models/errors.rb', line 45

def []=(key, errors)
  set(key, errors)
end

#add(key, error) ⇒ Object



36
37
38
39
# File 'motion-prime/models/errors.rb', line 36

def add(key, error)
  initialize_for_key(key)
  send(unique_key(key)) << error
end

#blank?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'motion-prime/models/errors.rb', line 63

def blank?
  messages.blank?
end

#get(key) ⇒ Object



26
27
28
29
# File 'motion-prime/models/errors.rb', line 26

def get(key)
  initialize_for_key(key)
  send(unique_key(key))
end

#initialize_for_key(key) ⇒ Object



17
18
19
20
21
22
23
24
# File 'motion-prime/models/errors.rb', line 17

def initialize_for_key(key)
  unique_key = unique_key(key)

  return if @_unique_keys.include?(unique_key)
  @_unique_keys << unique_key
  instance_variable_set("@#{unique_key}", [])
  self.class.send :attr_accessor, unique_key
end

#messagesObject



59
60
61
# File 'motion-prime/models/errors.rb', line 59

def messages
  @_unique_keys.map{ |uniq_k| send(uniq_k) }.compact.flatten
end

#present?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'motion-prime/models/errors.rb', line 67

def present?
  !blank?
end

#resetObject



53
54
55
56
57
# File 'motion-prime/models/errors.rb', line 53

def reset
  @_unique_keys.each do |unique_key|
    send :"#{unique_key}=", []
  end
end

#reset_for(key) ⇒ Object



49
50
51
# File 'motion-prime/models/errors.rb', line 49

def reset_for(key)
  send :"#{unique_key(key)}=", []
end

#set(key, errors) ⇒ Object



31
32
33
34
# File 'motion-prime/models/errors.rb', line 31

def set(key, errors)
  initialize_for_key(key)
  send :"#{unique_key(key)}=", Array.wrap(errors)
end

#to_sObject



71
72
73
# File 'motion-prime/models/errors.rb', line 71

def to_s
  messages.join(';')
end

#unique_key(key) ⇒ Object



13
14
15
# File 'motion-prime/models/errors.rb', line 13

def unique_key(key)
  [key, @model.object_id].join('_').to_sym
end