Strong attributes for ruby ruby objects.
Yes, there are so many alternative solutions already! Why then? Cause some times people need distractions at the airports.
Add this line to your application's Gemfile:
gem 'koine-attributes'
And then execute:
$ bundle
Or install it yourself as:
$ gem install koine-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.birthday # => #<Date 2001-02-31>
person.attributes.to_h # => {
# name: 'John Doe',
# birthday: #<Date 2001-02-31>
# }
Options:
attributes do
attribute :name, Koine::Attributes::Adapters::Date.new.with_default_value('guest')
# or
attribute :name, :string, ->(adapter) { adapter.with_default_value('guest') }
end
- with_default_value(default, &block)
- with_nil_value(value = nil, &block)
Also, a constructor can be created by the API
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
person = Person.new(name: 'John Doe', birthday: '2001-01-31', foo: :bar)
You can disable 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)
class Product
include Koine::Attributes
attributes do
attribute :price, MyCustom::Money.new
attribute :available, :boolean, ->(attribues){ attributes.with_default_value(true) }
attribute :available, Koine::Attributes::Drivers::Boolean.new.with_default_value(true)
attribute :tags, array_of(:string)
attribute :config, hash_of(:symbol, :string)
# if you want to costumize the above
attribute :config, hash_of(:symbol, :string) do |adapter|
adapter.for_keys.with_nil_value
adapter.for_values.with_nil_value
end
end
end
product = Product.new
product.available # => true
product.available = 0
product.available # => false
product.price = { currency: 'USD', value: 100 }
product.tags = ['new']
product.config = { short_url: 'http://config.url' }
# or
product.price = "100 USD"
class MyCustom::Money
attr_accessor :attribute_name
def default_value
return 'some default_value'
end
def coerce(*values)
Money.new(values.first, values.last)
end
end
product = Product.new
product.available # => true
product.available = 0
product.available # => false
product.price = { currency: 'USD', value: 100 }
# or
product.price = "100 USD"
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)
- :any
- :array_of
- :boolean
- :date
- :float
- :hash_of
- :integer
- :string
- empty_to_nil
- trim_empty_spaces
- :symbol
- :time
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/mjacobus/koine-attributes. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.