Skip to content

Cado-Labs/smart_value-object

Repository files navigation

SmartCore::ValueObject · Supported by Cado Labs · Gem Version

Value Object pattern realized in scope of Ruby.


Supported by Cado Labs


Major featurs

  • 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

gem 'smart_value-object'
bundle install
# --- or ---
gem install smart_value-object
require 'smart_core/value-object'

Synopsis

class Address < SmartCore::ValueObject
  attribute :country, 'value.string' # an alias of SmartCore::Types::Value::String (see smart_initializer gem)
  attribute :city,    'value.string'
  property :location, 'value.string'
  property :capital,  SmartCore::Types::Value::Boolean
end

khabarovsk = Address.new('Russia', 'Khabaovsk', location: '48.4814/135.0721', capital: false)
same_city = Address.new('Russia', 'Khabaovsk', location: '48.4814/135.0721', capital: false)
another_city = Address.new('Russia', 'Moscow', location: '59.9311/30.3609', capital: false)
khabarovsk.frozen? # => true
khabarovsk.country # => 'Russia'
khabarovsk.city # => 'Khabarovsk'
khabarovsk.location # => '48.4814/135.0721'
khabarovsk.capital # => false
khabarovsk.to_h # or #as_hash or #to_hash
# => returns:
{ city: 'Russia', country: 'Khabaovsk', location: '48.4814/135.0721', capital: false }
# comparability:
khabarovsk == same_city # => true
khabarovsk == another_city # => false
# default Enumerable behavior:
khabarovsk.to_a # => [khabarovsk]
khabarovsk.each { |entity| puts entity } # => outputs itself

Roadmap

  • Migrate to GitHub Actions;

Build

  • run tests:
bundle exec rspec
  • run code style checks:
bundle exec rubocop
  • run code style checks with auto-correction:
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