Skip to content

Instantly share code, notes, and snippets.

@zgfif
Created December 24, 2018 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zgfif/6f6f2724d5dd5a50e2a9d34b09736ffc to your computer and use it in GitHub Desktop.
Save zgfif/6f6f2724d5dd5a50e2a9d34b09736ffc to your computer and use it in GitHub Desktop.
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