Value Object pattern realized in scope of Ruby.
Value Object pattern realized in scope of Ruby.
attribute
and property
semantics;#freeze
invocation;Comparable
, #<=>
, #eql?
) (based on object’s type and object’s attributes and parameters);Enumerable
, #each
) (enumerates itself by default);soon
) #clone
and #dup
;
gem 'smart_value-object'
bundle install
# --- or ---
gem install smart_value-object
require 'smart_core/value-object'
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', 'Khabarovsk', location: '48.4814/135.0721', capital: false)
same_city = Address.new('Russia', 'Khabarovsk', 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: 'Khabarovsk', 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
GitHub Actions
;
bundle exec rspec
bundle exec rubocop
bundle exec rubocop -A
git checkout -b feature/my-new-feature
)git commit -am '[feature_context] Add some feature'
)git push origin feature/my-new-feature
)Released under MIT License.