2009-09-14 15:54:43 -04:00
|
|
|
require 'active_model'
|
2009-07-19 10:24:19 -04:00
|
|
|
|
|
|
|
class Person
|
2009-07-21 01:56:27 -04:00
|
|
|
include ActiveModel::Conversion
|
2009-07-19 10:24:19 -04:00
|
|
|
include ActiveModel::Validations
|
2009-07-21 01:56:27 -04:00
|
|
|
|
2013-06-24 01:49:51 -04:00
|
|
|
validates :name, presence: true
|
2009-07-21 01:56:27 -04:00
|
|
|
|
2009-07-19 10:24:19 -04:00
|
|
|
attr_accessor :name
|
2009-07-21 01:56:27 -04:00
|
|
|
|
2009-07-19 10:24:19 -04:00
|
|
|
def initialize(attributes = {})
|
|
|
|
@name = attributes[:name]
|
|
|
|
end
|
2009-07-21 01:56:27 -04:00
|
|
|
|
2009-07-19 10:24:19 -04:00
|
|
|
def persist
|
|
|
|
@persisted = true
|
|
|
|
end
|
2009-07-21 01:56:27 -04:00
|
|
|
|
2010-02-21 05:09:21 -05:00
|
|
|
def persisted?
|
2009-07-19 10:24:19 -04:00
|
|
|
@persisted
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
person1 = Person.new
|
2012-11-18 02:04:36 -05:00
|
|
|
p person1.valid? # => false
|
|
|
|
p person1.errors.messages # => {:name=>["can't be blank"]}
|
2009-07-19 10:24:19 -04:00
|
|
|
|
2013-07-04 12:25:50 -04:00
|
|
|
person2 = Person.new(name: 'matz')
|
2012-11-18 02:04:36 -05:00
|
|
|
p person2.valid? # => true
|