{"id":17775503,"url":"https://github.com/pikachuexe/contracted_value","last_synced_at":"2025-08-31T13:30:34.592Z","repository":{"id":42535909,"uuid":"189381210","full_name":"PikachuEXE/contracted_value","owner":"PikachuEXE","description":"Library for creating contracted immutable(by default) value objects","archived":false,"fork":false,"pushed_at":"2024-12-26T00:45:21.000Z","size":56,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-28T12:02:14.890Z","etag":null,"topics":["contracts","gem","ruby","value-object"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PikachuEXE.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["PikachuEXE"]}},"created_at":"2019-05-30T09:03:29.000Z","updated_at":"2025-07-22T03:08:38.000Z","dependencies_parsed_at":"2023-02-17T23:10:14.274Z","dependency_job_id":"4b7fe784-d11a-4c0a-a033-ce9fc8a5cb61","html_url":"https://github.com/PikachuEXE/contracted_value","commit_stats":{"total_commits":47,"total_committers":2,"mean_commits":23.5,"dds":0.04255319148936165,"last_synced_commit":"6823172a43553785a789270d40f24af26b507811"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/PikachuEXE/contracted_value","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PikachuEXE%2Fcontracted_value","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PikachuEXE%2Fcontracted_value/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PikachuEXE%2Fcontracted_value/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PikachuEXE%2Fcontracted_value/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PikachuEXE","download_url":"https://codeload.github.com/PikachuEXE/contracted_value/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PikachuEXE%2Fcontracted_value/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272988240,"owners_count":25026955,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-31T02:00:09.071Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["contracts","gem","ruby","value-object"],"created_at":"2024-10-26T21:57:42.313Z","updated_at":"2025-08-31T13:30:34.374Z","avatar_url":"https://github.com/PikachuEXE.png","language":"Ruby","funding_links":["https://github.com/sponsors/PikachuEXE"],"categories":[],"sub_categories":[],"readme":"# ContractedValue\n\nLibrary for creating contracted immutable(by default) value objects\n\nThis gem allows creation of value objects which are\n- contracted (enforced by [`contracts.ruby`](https://github.com/egonSchiele/contracts.ruby))\n- immutable (enforced by [`ice_nine`](https://github.com/dkubb/ice_nine))\n\nSee details explanation in below sections\n\n\n## Status\n\n[![GitHub Build Status](https://img.shields.io/github/actions/workflow/status/PikachuEXE/contracted_value/tests.yaml?branch=master\u0026style=flat-square)](https://github.com/PikachuEXE/contracted_value/actions/workflows/tests.yaml)\n\n[![Gem Version](http://img.shields.io/gem/v/contracted_value.svg?style=flat-square)](http://badge.fury.io/rb/contracted_value)\n[![License](https://img.shields.io/github/license/PikachuEXE/contracted_value.svg?style=flat-square)](http://badge.fury.io/rb/contracted_value)\n\n[![Code Climate](https://img.shields.io/codeclimate/maintainability/PikachuEXE/contracted_value.svg?style=flat-square)](https://codeclimate.com/github/PikachuEXE/contracted_value)\n[![Coverage Status](http://img.shields.io/coveralls/PikachuEXE/contracted_value.svg?style=flat-square)](https://coveralls.io/r/PikachuEXE/contracted_value)\n\n\u003e The above badges are generated by https://shields.io/\n\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\n# `require` can be set to `true` safely without too much side effect\n# (except having additional modules \u0026 classes defined which could be wasting memory).\n# But there is no point requiring it unless in test\n# Also maybe add it inside a \"group\"\ngem \"contracted_value\", require: false\n```\n\nAnd then execute:\n\n```bash\n$ bundle\n```\n\nOr install it yourself as:\n\n```bash\n$ gem install contracted_value\n```\n\n\n## Usage\n\nThe examples below might contain some of my habbits,  \nlike including [`contracts.ruby`](https://github.com/egonSchiele/contracts.ruby) modules in class  \nYou **don't** have to do it  \n\n\n### Attribute Declaration\n\nYou can declare with or without contract/default value\nBut an attribute **cannot** be declared twice\n\n```ruby\nmodule ::Geometry\nend\n\nmodule ::Geometry::LocationRange\n  class Entry \u003c ::ContractedValue::Value\n    include ::Contracts::Core\n    include ::Contracts::Builtin\n\n    attribute(\n      :latitude,\n      contract: Numeric,\n    )\n    attribute(\n      :longitude,\n      contract: Numeric,\n    )\n\n    attribute(\n      :radius_in_meter,\n      contract: And[Numeric, Send[:positive?]],\n    )\n\n    attribute(\n      :latitude,\n    ) # =\u003e error, declared already\n  end\nend\n\nlocation_range = ::Geometry::LocationRange::Entry.new(\n  latitude:         22.2,\n  longitude:        114.4,\n  radius_in_meter:  1234,\n)\n```\n\n\n### Attribute Assignment\n\nOnly `Hash` and `ContractedValue::Value` can be passed to `.new`\n\n```ruby\nmodule ::Geometry\nend\n\nmodule ::Geometry::Location\n  class Entry \u003c ::ContractedValue::Value\n    include ::Contracts::Core\n    include ::Contracts::Builtin\n\n    attribute(\n      :latitude,\n      contract: Numeric,\n    )\n    attribute(\n      :longitude,\n      contract: Numeric,\n    )\n  end\nend\n\nmodule ::Geometry::LocationRange\n  class Entry \u003c ::ContractedValue::Value\n    include ::Contracts::Core\n    include ::Contracts::Builtin\n\n    attribute(\n      :latitude,\n      contract: Numeric,\n    )\n    attribute(\n      :longitude,\n      contract: Numeric,\n    )\n\n    attribute(\n      :radius_in_meter,\n      contract: Maybe[And[Numeric, Send[:positive?]]],\n      default_value: nil,\n    )\n  end\nend\n\nlocation = ::Geometry::Location::Entry.new(\n  latitude:   22.2,\n  longitude:  114.4,\n)\nlocation_range = ::Geometry::LocationRange::Entry.new(location)\n\n```\n\n#### Passing objects of different `ContractedValue::Value` subclasses to `.new`\nPossible due to the implementation calling `#to_h` for `ContractedValue::Value` objects  \nBut in case the attribute names are different, or adding new attributes/updating existing attributes is needed  \nYou will need to call `#to_h` to get a `Hash` and do whatever modification needed before passing into `.new`  \n\n```ruby\nclass Pokemon \u003c ::ContractedValue::Value\n  attribute(:name)\n  attribute(:type)\nend\n\nclass Pikachu \u003c ::Pokemon\n  attribute(:name, default_value: \"Pikachu\")\n  attribute(:type, default_value: \"Thunder\")\nend\n\n# Ya I love using pokemon as examples, problem?\npikachu = Pikachu.new(name: \"PikaPika\")\npikachu.name #=\u003e \"PikaPika\"\npikachu.type #=\u003e \"Thunder\"\n\npokemon1 = Pokemon.new(pikachu)\npokemon1.name #=\u003e \"PikaPika\"\npokemon1.type #=\u003e \"Thunder\"\n\npokemon2 = Pokemon.new(pikachu.to_h.merge(name: \"Piak\"))\npokemon2.name #=\u003e \"Piak\"\npokemon2.type #=\u003e \"Thunder\"\n```\n\n\n### Input Validation\n\nInput values are validated on object creation (instead of on attribute value access) with 2 validations:\n- Value contract\n- Value presence\n\n#### Value contract\nAn attribute can be declared without any contract, and any input value would be pass the validation  \nBut you can pass a contract via `contract` option (must be a [`contracts.ruby`](https://github.com/egonSchiele/contracts.ruby) contract)  \nPassing input value violating an attribute's contract would cause an error  \n\n```ruby\nclass YetAnotherRationalNumber \u003c ::ContractedValue::Value\n  include ::Contracts::Core\n  include ::Contracts::Builtin\n\n  attribute(\n    :numerator,\n    contract: ::Integer,\n  )\n  attribute(\n    :denominator,\n    contract: And[::Integer, Not[Send[:zero?]]],\n  )\nend\n\nYetAnotherRationalNumber.new(\n  numerator: 1, \n  denominator: 0, \n) # =\u003e Error\n\n```\n\n#### Value presence\nAn attribute declared should be provided a value on object creation, even the input value is `nil`  \nOtherwise an error is raised  \nYou can pass default value via option `default_value`  \nThe default value will need to confront to the contract passed in `contract` option too  \n\n\n```ruby\nmodule ::WhatIsThis\n  class Entry \u003c ::ContractedValue::Value\n    include ::Contracts::Core\n    include ::Contracts::Builtin\n\n    attribute(\n      :something_required,\n    )\n    attribute(\n      :something_optional,\n      default_value: nil,\n    )\n    attribute(\n      :something_with_error,\n      contract: NatPos,\n      default_value: 0,\n    ) # =\u003e error\n  end\nend\n\nWhatIsThis::Entry.new(\n  something_required: 123,\n).something_optional # =\u003e nil\n```\n\n\n### Object Freezing\nAll input values are frozen using [`ice_nine`](https://github.com/dkubb/ice_nine) by default  \nBut some objects won't work properly when deeply frozen (rails obviously)  \nSo you can specify how input value should be frozen (or not frozen) with option `refrigeration_mode`  \nPossible values are:\n- `:deep` (default)\n- `:shallow`\n- `:none`\n\nHowever the value object itself is always frozen  \nAny lazy method caching with use of instance var would cause `FrozenError`  \n(Many Rails classes use lazy caching heavily so most rails object can't be frozen to work properly)  \n\n```ruby\nclass SomeDataEntry \u003c ::ContractedValue::Value\n  include ::Contracts::Core\n  include ::Contracts::Builtin\n\n  attribute(\n    :cold_hash,\n    contract: ::Hash,\n  )\n  attribute(\n    :cool_hash,\n    contract: ::Hash,\n    refrigeration_mode: :shallow,\n  )\n  attribute(\n    :warm_hash,\n    contract: ::Hash,\n    refrigeration_mode: :none,\n  )\n  \n  def cached_hash\n    @cached_hash ||= {}\n  end\nend\n\nentry = SomeDataEntry.new(\n  cold_hash: {a: {b: 0}},\n  cool_hash: {a: {b: 0}},\n  warm_hash: {a: {b: 0}},\n)\n\nentry.cold_hash[:a].delete(:b) # =\u003e `FrozenError`\n\nentry.cool_hash[:a].delete(:b) # =\u003e fine\nentry.cool_hash.delete(:a) # =\u003e `FrozenError`\n\nentry.warm_hash.delete(:a) # =\u003e fine\n\nentry.cached_hash # =\u003e `FrozenError`\n\n```\n\nBeware that the value passed to `default_value` option when declaring an attribute is always deeply frozen  \nThis is to avoid any in-place change which changes the default value of any value object class attribute  \n\n\n### Value Object Class Inheritance\nYou can create a value object class inheriting an existing value class instead of `::ContractedValue::Value`  \n\n#### All existing attributes can be used  \nNo need to explain right?\n```ruby\nclass Pokemon \u003c ::ContractedValue::Value\n  attribute(:name)\nend\n\nclass Pikachu \u003c ::Pokemon\n  attribute(:type, default_value: \"Thunder\")\nend\n\n# Ya I love using pokemon as examples, problem?\npikachu = Pikachu.new(name: \"PikaPika\")\npikachu.name #=\u003e \"PikaPika\"\npikachu.type #=\u003e \"Thunder\"\n```\n\n#### All existing attributes can be redeclared  \nWithin the same class you cannot redefine an attribute\nBut in subclasses you can\n```ruby\nclass Pokemon \u003c ::ContractedValue::Value\n  attribute(:name)\nend\n\nclass Pikachu \u003c ::Pokemon\n  include ::Contracts::Core\n  include ::Contracts::Builtin\n\n  attribute(\n    :name,\n    contract: And[::String, Not[Send[:empty?]]],\n    default_value: String.new(\"Pikachu\"),\n    refrigeration_mode: :none,\n  )\nend\n\n# Ya I love using pokemon as examples, problem?\nPikachu.new.name # =\u003e \"Pikachu\"\nPikachu.new.name.frozen? # =\u003e true, as mentioned above default value are always deeply frozen\nPikachu.new(name: \"Pikaaaachuuu\").name.frozen? # =\u003e false\n```\n\n\n## Related gems\nHere is a list of gems which I found and I have tried some of them.  \nBut eventually I am unsatisfied so I build this gem.  \n\n- [values](https://github.com/tcrayford/values)\n- [active_attr](https://github.com/cgriego/active_attr)\n- [dry-struct](https://github.com/dry-rb/dry-struct)\n\n### [values](https://github.com/tcrayford/values)\nI used to use this a bit  \nBut I keep having to write the attribute names in `Values.new`,  \nthen the same attribute names again with `attr_reader` + contract (since I want to use contract)  \nAlso the input validation happens on attribute value access instead of on object creation  \n\n### [active_attr](https://github.com/cgriego/active_attr)\nGot similar issue as `values`  \n\n### [dry-struct](https://github.com/dry-rb/dry-struct)\nSeems more suitable for form objects instead of just value objects (for me)  \n\n\n## Contributing\n\n1. Fork it ( https://github.com/PikachuEXE/contracted_value/fork )\n2. Create your branch (Preferred to be prefixed with `feature`/`fix`/other sensible prefixes)\n3. Commit your changes (No version related changes will be accepted)\n4. Push to the branch on your forked repo\n5. Create a new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpikachuexe%2Fcontracted_value","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpikachuexe%2Fcontracted_value","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpikachuexe%2Fcontracted_value/lists"}