项目作者: smart-rb

项目描述 :
Value Object pattern realized in scope of Ruby.
高级语言: Ruby
项目地址: git://github.com/smart-rb/smart_value-object.git
创建时间: 2020-07-01T11:13:58Z
项目社区:https://github.com/smart-rb/smart_value-object

开源协议:MIT License

下载


" class="reference-link">SmartCore::ValueObject · Supported by Cado Labs · Gem Version

Value Object pattern realized in scope of Ruby.




Supported by Cado Labs


Major features

  • attribute and property semantics;
  • primitive immutability based on #freeze invocation;
  • read-only instance attributes and properties;
  • support for hash representation (other formats coming soon);
  • support for:
    • semantic comparability (Comparable, #<=>, #eql?) (based on object’s type and object’s attributes and parameters);
    • semantic enumerability (Enumerable, #each) (enumerates itself by default);
    • (soon) #clone and #dup;

Installation

  1. gem 'smart_value-object'
  1. bundle install
  2. # --- or ---
  3. gem install smart_value-object
  1. require 'smart_core/value-object'

Synopsis

  1. class Address < SmartCore::ValueObject
  2. attribute :country, 'value.string' # an alias of SmartCore::Types::Value::String (see smart_initializer gem)
  3. attribute :city, 'value.string'
  4. property :location, 'value.string'
  5. property :capital, SmartCore::Types::Value::Boolean
  6. end
  7. khabarovsk = Address.new('Russia', 'Khabarovsk', location: '48.4814/135.0721', capital: false)
  8. same_city = Address.new('Russia', 'Khabarovsk', location: '48.4814/135.0721', capital: false)
  9. another_city = Address.new('Russia', 'Moscow', location: '59.9311/30.3609', capital: false)
  1. khabarovsk.frozen? # => true
  1. khabarovsk.country # => 'Russia'
  2. khabarovsk.city # => 'Khabarovsk'
  3. khabarovsk.location # => '48.4814/135.0721'
  4. khabarovsk.capital # => false
  1. khabarovsk.to_h # or #as_hash or #to_hash
  2. # => returns:
  3. { city: 'Russia', country: 'Khabarovsk', location: '48.4814/135.0721', capital: false }
  1. # comparability:
  2. khabarovsk == same_city # => true
  3. khabarovsk == another_city # => false
  1. # default Enumerable behavior:
  2. khabarovsk.to_a # => [khabarovsk]
  3. khabarovsk.each { |entity| puts entity } # => outputs itself

Roadmap

  • Migrate to GitHub Actions;

Build

  • run tests:
  1. bundle exec rspec
  • run code style checks:
  1. bundle exec rubocop
  • run code style checks with auto-correction:
  1. bundle exec rubocop -A

Contributing

  • Fork it ( https://github.com/smart-rb/smart_value-object )
  • Create your feature branch (git checkout -b feature/my-new-feature)
  • Commit your changes (git commit -am '[feature_context] Add some feature')
  • Push to the branch (git push origin feature/my-new-feature)
  • Create new Pull Request

License

Released under MIT License.

Supporting


Supported by Cado Labs

Authors

Rustam Ibragimov