Class: Navigable::Input
- Inherits:
-
ImmutableObject
- Object
- ImmutableObject
- Navigable::Input
- Defined in:
- lib/navigable/input.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Class Method Summary collapse
- .validates_max_length_of(field, max_length) ⇒ Object
- .validates_presence_of(field) ⇒ Object
- .validations ⇒ Object
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Input
constructor
A new instance of Input.
- #valid? ⇒ Boolean
Methods inherited from ImmutableObject
Constructor Details
Instance Attribute Details
#errors ⇒ Object (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 |
.validations ⇒ Object
25 26 27 |
# File 'lib/navigable/input.rb', line 25 def validations @validations ||= [] end |
Instance Method Details
#valid? ⇒ 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 |