Module: PhcStringFormat::Validations::ClassMethods
- Defined in:
- lib/phc_string_format/validations.rb
Overview
class methods
Instance Method Summary collapse
- #do_validate(that) ⇒ Object
- #validate(name, **options) ⇒ Object
- #validates(name, **options) ⇒ Object
Instance Method Details
#do_validate(that) ⇒ Object
28 29 30 31 |
# File 'lib/phc_string_format/validations.rb', line 28 def do_validate(that) @validators.each { |validator| validator.call(that) } that end |
#validate(name, **options) ⇒ Object
23 24 25 26 |
# File 'lib/phc_string_format/validations.rb', line 23 def validate(name, **) @validators ||= [] @validators << ->(object) { raise ArgumentError, [:message] unless object.send(name) } end |
#validates(name, **options) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/phc_string_format/validations.rb', line 13 def validates(name, **) @validators ||= [] @validators << lambda { |object| value = object.instance_variable_get(name) return if [:allow_nil] && !value regex = .dig(:format, :with) raise ArgumentError, [:message] unless !regex || value =~ regex } end |