Created
December 24, 2018 10:33
-
-
Save zgfif/6f6f2724d5dd5a50e2a9d34b09736ffc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_model' | |
class Person | |
include ActiveModel::Validations | |
attr_accessor :name, :age | |
def initialize(name: nil, age: nil) | |
@name = name | |
@age = age | |
end | |
validates :name, :age , presence: true | |
validates :age, numericality: { greater_than: 0 } | |
end | |
puts 'Empty Person' | |
person = Person.new | |
p person.valid? | |
p person.errors.messages | |
puts ' with name' | |
person = Person.new name: 'Bruce Willis' | |
p person.name | |
p person.valid? | |
p person.errors.messages | |
puts 'With age equal 0' | |
person = Person.new name: 'Bruce Willis', age: 0 | |
p person.age | |
p person.valid? | |
p person.errors.messages | |
puts 'With age greater than 0' | |
person = Person.new name: 'Bruce Willis', age: 56 | |
p person.name | |
p person.age | |
p person.valid? | |
p person.errors.messages |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment