Module: Koine

Defined in:
lib/koine/attributes.rb,
lib/koine/attributes/version.rb,
lib/koine/attributes/attributes.rb,
lib/koine/attributes/adapter/any.rb,
lib/koine/attributes/hash_helper.rb,
lib/koine/attributes/adapter/base.rb,
lib/koine/attributes/adapter/date.rb,
lib/koine/attributes/adapter/time.rb,
lib/koine/attributes/adapter/float.rb,
lib/koine/attributes/adapter/string.rb,
lib/koine/attributes/adapter/symbol.rb,
lib/koine/attributes/adapter/boolean.rb,
lib/koine/attributes/adapter/hash_of.rb,
lib/koine/attributes/adapter/integer.rb,
lib/koine/attributes/adapter/array_of.rb,
lib/koine/attributes/attributes_factory.rb

Overview

provides the following API

Examples:

using attributes

class Person
  include Koine::Attributes

  attributes do
    attribute :name, :string
    attribute :birthday, :date

    # or
    attribute :birthday, Koine::Attributes::Adapter::Date.new
  end
end

peson = Person.new
person.name = 'John Doe'
person.birtday = '2001-02-31' # Date Object can also be given

person.name # => 'John Doe'
person.birtday # => #<Date 2001-02-31>

custom attribute options


attributes do
  attribute :name, Koine::Attributes::Adapters::Date.new.with_default('guest')

  # or
  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')

# foo: attribute will raise error with strict mode
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

# foo will be ignored
person = Person.new(name: 'John Doe', birthday: '2001-01-31', foo: :bar)

Override constructor


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 # => :bar
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)

Defined Under Namespace

Modules: Attributes