-
Notifications
You must be signed in to change notification settings - Fork 31
Home
Josh Brody edited this page Jun 19, 2018
·
3 revisions
Welcome to the Attributary wiki!
gem install attributary
class Character
include Attributary::DSL
attribute :name, :string, :default => "Thomas"
attribute :age, :integer, :validate => proc { |value| value >= 18 }
end
Use it
character = Character.new
character.name = "Ruby"
character.age = 15
character.attributary_valid? # false
character.attributary_errors {:age => {:name => :age, :klass => ValidationError, :message => "Validation failed."}}
character.age = 18
character.attributary_valid? # true
You can also initialize if you include Attributary::Initializer
.
character = Character.new(:name => "Ruby", :age => 18)
Need to initialize other stuff? Cool. Just call attributary_attributes
class Character
include Attributary::DSL
include Attributary::Initializer
attribute :age, :integer
def initialize(name, options = {})
@name = name
attributary_attributes(options)
end
end
character = Character.new("John", :age => 18)