Class: ScrivenerErrors
- Inherits:
-
Object
- Object
- ScrivenerErrors
- Defined in:
- lib/scrivener_errors.rb
Defined Under Namespace
Modules: Helpers
Constant Summary collapse
- MESSAGES =
{ :not_email => "is not an email", :not_decimal => "is not a decimal", :not_in_range => "has invalid length", :not_numeric => "is not a number", :not_present => "can't be blank", :not_url => "is not a url", :too_short => "is too short" }.freeze
Instance Attribute Summary collapse
-
#scrivener ⇒ Object
readonly
Returns the value of attribute scrivener.
Instance Method Summary collapse
- #ensure_validated ⇒ Object
- #error_string(att, error) ⇒ Object
-
#initialize(scrivener) ⇒ ScrivenerErrors
constructor
A new instance of ScrivenerErrors.
- #messages ⇒ Object
- #to_s ⇒ Object (also: #message)
Constructor Details
#initialize(scrivener) ⇒ ScrivenerErrors
Returns a new instance of ScrivenerErrors.
14 15 16 17 |
# File 'lib/scrivener_errors.rb', line 14 def initialize(scrivener) @scrivener = scrivener ensure_validated end |
Instance Attribute Details
#scrivener ⇒ Object (readonly)
Returns the value of attribute scrivener.
2 3 4 |
# File 'lib/scrivener_errors.rb', line 2 def scrivener @scrivener end |
Instance Method Details
#ensure_validated ⇒ Object
19 20 21 22 23 |
# File 'lib/scrivener_errors.rb', line 19 def ensure_validated if scrivener.errors == {} scrivener.valid? end end |
#error_string(att, error) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/scrivener_errors.rb', line 38 def error_string(att, error) att = att.to_s.tr('_', ' ') failure = MESSAGES.fetch(error, 'is invalid') [att, failure].join(' ') end |
#messages ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/scrivener_errors.rb', line 30 def scrivener.errors.inject([]) do |memo, error| att = error[0] memo.concat error[1].map {|e| error_string(att, e) } memo end end |
#to_s ⇒ Object Also known as: message
25 26 27 |
# File 'lib/scrivener_errors.rb', line 25 def to_s .join(', ').capitalize end |