Class: ScrivenerErrors

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#scrivenerObject (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_validatedObject



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

#messagesObject



30
31
32
33
34
35
36
# File 'lib/scrivener_errors.rb', line 30

def messages
  scrivener.errors.inject([]) do |memo, error|
    att = error[0]
    memo.concat error[1].map {|e| error_string(att, e) }
    memo
  end
end

#to_sObject Also known as: message



25
26
27
# File 'lib/scrivener_errors.rb', line 25

def to_s
  messages.join(', ').capitalize
end