README.md in contracted_value-0.1.0 vs README.md in contracted_value-0.1.1

- old
+ new

@@ -1,25 +1,29 @@ # ContractedValue Library for creating contracted immutable(by default) value objects -[![Gem Version](http://img.shields.io/gem/v/contracted_value.svg?style=flat-square)](http://badge.fury.io/rb/contracted_value) -[![License](https://img.shields.io/github/license/PikachuEXE/contracted_value.svg?style=flat-square)](http://badge.fury.io/rb/contracted_value) - -[![Build Status](http://img.shields.io/travis/PikachuEXE/contracted_value.svg?style=flat-square)](https://travis-ci.org/PikachuEXE/contracted_value) - -[![Code Climate](https://img.shields.io/codeclimate/maintainability/PikachuEXE/contracted_value.svg?style=flat-square)](https://codeclimate.com/github/PikachuEXE/contracted_value) -[![Coverage Status](https://img.shields.io/codecov/c/github/PikachuEXE/contracted_value.svg?style=flat-square)](https://codecov.io/gh/PikachuEXE/contracted_value) - - This gem allows creation of value objects which are - contracted (enforced by [`contracts.ruby`](https://github.com/egonSchiele/contracts.ruby)) - immutable (enforced by [`ice_nine`](https://github.com/dkubb/ice_nine)) See details explanation in below sections +## Status + +[![GitHub Build Status](https://img.shields.io/github/workflow/status/PikachuEXE/contracted_value/Tests?style=flat-square)](https://github.com/PikachuEXE/contracted_value/actions?query=workflow%3ATests) + +[![Gem Version](http://img.shields.io/gem/v/contracted_value.svg?style=flat-square)](http://badge.fury.io/rb/contracted_value) +[![License](https://img.shields.io/github/license/PikachuEXE/contracted_value.svg?style=flat-square)](http://badge.fury.io/rb/contracted_value) + +[![Code Climate](https://img.shields.io/codeclimate/maintainability/PikachuEXE/contracted_value.svg?style=flat-square)](https://codeclimate.com/github/PikachuEXE/contracted_value) +[![Coverage Status](http://img.shields.io/coveralls/PikachuEXE/contracted_value.svg?style=flat-square)](https://coveralls.io/r/PikachuEXE/contracted_value) + +> The above badges are generated by https://shields.io/ + + ## Installation Add this line to your application's Gemfile: ```ruby @@ -144,21 +148,51 @@ ) location_range = ::Geometry::LocationRange::Entry.new(location) ``` +#### Passing objects of different `ContractedValue::Value` subclasses to `.new` +Possible due to the implementation calling `#to_h` for `ContractedValue::Value` objects +But in case the attribute names are different, or adding new attributes/updating existing attributes is needed +You will need to call `#to_h` to get a `Hash` and do whatever modification needed before passing into `.new` +```ruby +class Pokemon < ::ContractedValue::Value + attribute(:name) + attribute(:type) +end + +class Pikachu < ::Pokemon + attribute(:name, default_value: "Pikachu") + attribute(:type, default_value: "Thunder") +end + +# Ya I love using pokemon as examples, problem? +pikachu = Pikachu.new(name: "PikaPika") +pikachu.name #=> "PikaPika" +pikachu.type #=> "Thunder" + +pokemon1 = Pokemon.new(pikachu) +pokemon1.name #=> "PikaPika" +pokemon1.type #=> "Thunder" + +pokemon2 = Pokemon.new(pikachu.to_h.merge(name: "Piak")) +pokemon2.name #=> "Piak" +pokemon2.type #=> "Thunder" +``` + + ### Input Validation Input values are validated on object creation (instead of on attribute value access) with 2 validations: - Value contract - Value presence #### Value contract -An attribute can be declared without any contract, and any input value would be pass the validation -But you can pass a contract via `contract` option (must be a [`contracts.ruby`](https://github.com/egonSchiele/contracts.ruby) contract) -Passing input value violating an attribute's contract would cause an error +An attribute can be declared without any contract, and any input value would be pass the validation +But you can pass a contract via `contract` option (must be a [`contracts.ruby`](https://github.com/egonSchiele/contracts.ruby) contract) +Passing input value violating an attribute's contract would cause an error ```ruby class YetAnotherRationalNumber < ::ContractedValue::Value include ::Contracts::Core include ::Contracts::Builtin @@ -179,14 +213,14 @@ ) # => Error ``` #### Value presence -An attribute declared should be provided a value on object creation, even the input value is `nil` -Otherwise an error is raised -You can pass default value via option `default_value` -The default value will need to confront to the contract passed in `contract` option too +An attribute declared should be provided a value on object creation, even the input value is `nil` +Otherwise an error is raised +You can pass default value via option `default_value` +The default value will need to confront to the contract passed in `contract` option too ```ruby module ::WhatIsThis class Entry < ::ContractedValue::Value @@ -287,9 +321,11 @@ attribute(:type, default_value: "Thunder") end # Ya I love using pokemon as examples, problem? pikachu = Pikachu.new(name: "PikaPika") +pikachu.name #=> "PikaPika" +pikachu.type #=> "Thunder" ``` #### All existing attributes can be redeclared Within the same class you cannot redefine an attribute But in subclasses you can