Class: Navigable::Input

Inherits:
ImmutableObject show all
Defined in:
lib/navigable/input.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ImmutableObject

attribute, #attributes

Constructor Details

#initialize(attributes = {}) ⇒ Input

Returns a new instance of Input.



32
33
34
35
# File 'lib/navigable/input.rb', line 32

def initialize(attributes = {})
  @errors = Errors.new
  super(**attributes.to_hash.transform_keys(&:to_sym))
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



30
31
32
# File 'lib/navigable/input.rb', line 30

def errors
  @errors
end

Class Method Details

.validates_max_length_of(field, max_length) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/navigable/input.rb', line 17

def validates_max_length_of(field, max_length)
  validations << ->(subject) do
    if (subject.public_send(field) && subject.public_send(field).length > max_length)
      subject.errors.add(field, "longer than #{max_length}")
    end
  end
end

.validates_presence_of(field) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/navigable/input.rb', line 9

def validates_presence_of(field)
  validations << ->(subject) do
    if subject.public_send(field).nil?
      subject.errors.add(field, "not present")
    end
  end
end

.validationsObject



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

def validations
  @validations ||= []
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/navigable/input.rb', line 37

def valid?
  self.class.validations.each { |validation| validation.call(self) }
  @errors.empty?
end