Examples:
class Person
include Koine::Attributes
attributes do
attribute :name, :string
attribute :birthday, :date
attribute :birthday, Koine::Attributes::Adapter::Date.new
end
end
peson = Person.new
person.name = 'John Doe'
person.birtday = '2001-02-31'
person.name person.birtday
attributes do
attribute :name, Koine::Attributes::Adapters::Date.new.with_default('guest')
attribute :name, :string, ->(adapter) { adapter.with_default('guest') }
end
Constructor for attributes
class Person
include Koine::Attributes
attributes initializer: true do
attribute :name, :string
attribute :birthday, :date
end
end
person = Person.new(name: 'John Doe', birthday: '2001-01-31')
person = Person.new(name: 'John Doe', birthday: '2001-01-31', foo: :bar)
Constructor for attributes withouth strict mode
class Person
include Koine::Attributes
attributes initializer: { strict: false } do
attribute :name, :string
attribute :birthday, :date
end
end
person = Person.new(name: 'John Doe', birthday: '2001-01-31', foo: :bar)
class Person
include Koine::Attributes
attr_reader :foo
attributes initializer: true do
attribute :name, :string
attribute :birthday, :date
end
def initialize(attributes = {})
@foo = attributes.delete(:foo)
self.attributes.set_values(attributes)
end
end
person = Person.new(name: 'John Doe', birthday: '2001-01-31', foo: :bar)
person.foo
class Location
include Koine::Attributes
attributes initializer: { freeze: true } do
attribute :lat, :float
attribute :lon, :float
end
end
location = Location.new(lat: 1, lon: 2)
new_location = location.with_lon(3)