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" }
Instance Attribute Summary collapse
-
#scrivener ⇒ Object
readonly
Returns the value of attribute scrivener.
Instance Method Summary collapse
- #[](att) ⇒ Object
- #ensure_validated ⇒ Object
- #error_string(att, error) ⇒ Object
-
#initialize(scrivener) ⇒ ScrivenerErrors
constructor
A new instance of ScrivenerErrors.
- #lookup(key) ⇒ Object
- #message ⇒ Object (also: #to_s)
- #messages ⇒ Object
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
#[](att) ⇒ Object
25 26 27 28 29 |
# File 'lib/scrivener_errors.rb', line 25 def [](att) if (errors = scrivener.errors[att.to_sym]) errors.map { |e| lookup(e) }.join(', ') end end |
#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
43 44 45 46 |
# File 'lib/scrivener_errors.rb', line 43 def error_string(att, error) att = att.to_s.tr('_', ' ') [att, lookup(error)].join(' ') end |
#lookup(key) ⇒ Object
48 49 50 |
# File 'lib/scrivener_errors.rb', line 48 def lookup(key) MESSAGES.fetch(key, 'is invalid') end |
#message ⇒ Object Also known as: to_s
31 32 33 |
# File 'lib/scrivener_errors.rb', line 31 def .join(', ').capitalize end |
#messages ⇒ Object
36 37 38 39 40 41 |
# File 'lib/scrivener_errors.rb', line 36 def scrivener.errors.each_with_object([]) do |error, collection| att = error[0] collection.concat error[1].map {|e| error_string(att, e) } end end |