项目作者: mjacobus

项目描述 :
Stronger attributes for ruby
高级语言: Ruby
项目地址: git://github.com/mjacobus/koine-attributes.git
创建时间: 2017-04-24T18:21:28Z
项目社区:https://github.com/mjacobus/koine-attributes

开源协议:MIT License

下载


Koine::Attributes

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.

Build Status
Coverage Status
Scrutinizer Code Quality
Code Climate
Issue Count

Gem Version

Installation

Add this line to your application’s Gemfile:

  1. gem 'koine-attributes'

And then execute:

  1. $ bundle

Or install it yourself as:

  1. $ gem install koine-attributes

Usage

  1. class Person
  2. include Koine::Attributes
  3. attributes do
  4. attribute :name, :string
  5. attribute :birthday, :date
  6. # or
  7. attribute :birthday, Koine::Attributes::Adapter::Date.new
  8. end
  9. end
  10. peson = Person.new
  11. person.name = 'John Doe'
  12. person.birtday = '2001-02-31' # Date Object can also be given
  13. person.name # => 'John Doe'
  14. person.birthday # => #<Date 2001-02-31>
  15. person.attributes.to_h # => {
  16. # name: 'John Doe',
  17. # birthday: #<Date 2001-02-31>
  18. # }

Options:

  1. attributes do
  2. attribute :name, Koine::Attributes::Adapters::Date.new.with_default_value('guest')
  3. # or
  4. attribute :name, :string, ->(adapter) { adapter.with_default_value('guest') }
  5. end

Adapter’s methods

  • with_default_value(default, &block)
  • with_nil_value(value = nil, &block)

Also, a constructor can be created by the API

  1. class Person
  2. include Koine::Attributes
  3. attributes initializer: true do
  4. attribute :name, :string
  5. attribute :birthday, :date
  6. end
  7. end
  8. person = Person.new(name: 'John Doe', birthday: '2001-01-31')
  9. # foo: attribute will raise error
  10. person = Person.new(name: 'John Doe', birthday: '2001-01-31', foo: :bar)

You can disable strict mode

  1. class Person
  2. include Koine::Attributes
  3. attributes initializer: { strict: false } do
  4. attribute :name, :string
  5. attribute :birthday, :date
  6. end
  7. end
  8. # foo will be ignored
  9. person = Person.new(name: 'John Doe', birthday: '2001-01-31', foo: :bar)
  1. class Product
  2. include Koine::Attributes
  3. attributes do
  4. attribute :price, MyCustom::Money.new
  5. attribute :available, :boolean, ->(attribues){ attributes.with_default_value(true) }
  6. attribute :available, Koine::Attributes::Drivers::Boolean.new.with_default_value(true)
  7. attribute :tags, array_of(:string)
  8. attribute :config, hash_of(:symbol, :string)
  9. # if you want to costumize the above
  10. attribute :config, hash_of(:symbol, :string) do |adapter|
  11. adapter.for_keys.with_nil_value
  12. adapter.for_values.with_nil_value
  13. end
  14. end
  15. end
  16. product = Product.new
  17. product.available # => true
  18. product.available = 0
  19. product.available # => false
  20. product.price = { currency: 'USD', value: 100 }
  21. product.tags = ['new']
  22. product.config = { short_url: 'http://config.url' }
  23. # or
  24. product.price = "100 USD"
  1. class MyCustom::Money
  2. attr_accessor :attribute_name
  3. def default_value
  4. return 'some default_value'
  5. end
  6. def coerce(*values)
  7. Money.new(values.first, values.last)
  8. end
  9. end
  10. product = Product.new
  11. product.available # => true
  12. product.available = 0
  13. product.available # => false
  14. product.price = { currency: 'USD', value: 100 }
  15. # or
  16. product.price = "100 USD"

Value objects

  1. class Location
  2. include Koine::Attributes
  3. attributes initializer: { freeze: true } do
  4. attribute :lat, :float
  5. attribute :lon, :float
  6. end
  7. end
  8. location = Location.new(lat: 1, lon: 2)
  9. new_location = location.with_lon(3)

Standard types

  1. - :any
  2. - :array_of
  3. - :boolean
  4. - :date
  5. - :float
  6. - :hash_of
  7. - :integer
  8. - :string
  9. - empty_to_nil
  10. - trim_empty_spaces
  11. - :symbol
  12. - :time

Alternative solutions

Development

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.

Contributing

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.

License

The gem is available as open source under the terms of the MIT License.