{"id":18837067,"url":"https://github.com/bkuhlmann/wholeable","last_synced_at":"2025-04-14T06:21:57.778Z","repository":{"id":257809615,"uuid":"867739659","full_name":"bkuhlmann/wholeable","owner":"bkuhlmann","description":"Provides whole value object behavior.","archived":false,"fork":false,"pushed_at":"2025-03-25T02:13:24.000Z","size":59,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-12T06:47:03.822Z","etag":null,"topics":["equality","value","value-object","whole"],"latest_commit_sha":null,"homepage":"https://alchemists.io/projects/wholeable","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bkuhlmann.png","metadata":{"files":{"readme":"README.adoc","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.adoc","code_of_conduct":null,"threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["bkuhlmann"]}},"created_at":"2024-10-04T16:16:41.000Z","updated_at":"2025-03-25T02:13:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"6cd63a21-fe5e-4c69-b9ba-bc645e02d89d","html_url":"https://github.com/bkuhlmann/wholeable","commit_stats":null,"previous_names":["bkuhlmann/wholeable"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bkuhlmann%2Fwholeable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bkuhlmann%2Fwholeable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bkuhlmann%2Fwholeable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bkuhlmann%2Fwholeable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bkuhlmann","download_url":"https://codeload.github.com/bkuhlmann/wholeable/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248830903,"owners_count":21168368,"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","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":["equality","value","value-object","whole"],"created_at":"2024-11-08T02:33:42.575Z","updated_at":"2025-04-14T06:21:57.759Z","avatar_url":"https://github.com/bkuhlmann.png","language":"Ruby","funding_links":["https://github.com/sponsors/bkuhlmann"],"categories":[],"sub_categories":[],"readme":":toc: macro\n:toclevels: 5\n:figure-caption!:\n\n:data_link: link:https://alchemists.io/articles/ruby_data[Data]\n:pattern_matching_link: link:https://alchemists.io/articles/ruby_pattern_matching[pattern matching]\n:ruby_link: link:https://www.ruby-lang.org[Ruby]\n:data_link: link:https://alchemists.io/articles/ruby_data[Data]\n:structs_link: link:https://alchemists.io/articles/ruby_structs[Structs]\n\n= Wholeable\n\nWholeable allows you to turn your object into a _whole value object_ by ensuring object equality is determined by the values of the object instead of by identity. Whole value objects -- or value objects in general -- have the following traits as noted via link:https://en.wikipedia.org/wiki/Value_object[Wikipedia]:\n\n* Equality is determined by the values that make up an object and not by link:https://en.wikipedia.org/wiki/Identity_(object-oriented_programming)[identity] (i.e. memory address) which is the default behavior for all {ruby_link} objects except for {data_link} and {structs_link}.\n* Identity remains unique since two objects can have the same values but different identity. This means `BasicObject#equal?` is never overwritten -- which is strongly discouraged -- as per link:https://rubyapi.org/o/basicobject#method-i-3D-3D[BasicObject] documentation.\n* Value objects should be immutable (i.e. frozen) by default. This implementation enforces a strict adherence to immutability in order to ensure value objects remain equal and discourage mutation.\n\ntoc::[]\n\n== Features\n\n* Ensures equality (i.e. `#==` and `#eql?`) is determined by attribute values and not object identity (i.e. `#equal?`).\n* Allows you to compare two objects of same or different types and see their differences.\n* Provides {pattern_matching_link}.\n* Provides inheritance so you can subclass and add attributes or provide additional behavior.\n* Automatically defines public attribute readers (i.e. `.attr_reader`) if _immutable_ (default) or public attribute readers and writers (i.e. `.attr_accessor`) if _mutable_.\n* Ensures object inspection (i.e. `#inspect`) shows all registered attributes.\n* Ensures object is frozen upon initialization by default.\n\n== Requirements\n\n. {ruby_link}.\n\n== Setup\n\nTo install _with_ security, run:\n\n[source,bash]\n----\n# 💡 Skip this line if you already have the public certificate installed.\ngem cert --add \u003c(curl --compressed --location https://alchemists.io/gems.pem)\ngem install wholeable --trust-policy HighSecurity\n----\n\nTo install _without_ security, run:\n\n[source,bash]\n----\ngem install wholeable\n----\n\nYou can also add the gem directly to your project:\n\n[source,bash]\n----\nbundle add wholeable\n----\n\nOnce the gem is installed, you only need to require it:\n\n[source,ruby]\n----\nrequire \"wholeable\"\n----\n\n== Usage\n\nTo use, include Wholeable along with a list of attributes that make up your whole value object:\n\n[source,ruby]\n----\nclass Person\n  include Wholeable[:name, :email]\n\n  def initialize name:, email:\n    @name = name\n    @email = email\n  end\nend\n\njill = Person[name: \"Jill Smith\", email: \"jill@example.com\"]\njill_two = Person[name: \"Jill Smith\", email: \"jill@example.com\"]\njack = Person[name: \"Jack Smith\", email: \"jack@example.com\"]\n\nPerson.members         # [:name, :email]\njill.members           # [:name, :email]\n\njill.name              # \"Jill Smith\"\njill.email             # \"jill@example.com\"\n\njill.frozen?           # true\njill_two.frozen?       # true\njack.frozen?           # true\n\njill.inspect           # \"#\u003cPerson @name=\\\"Jill Smith\\\", @email=\\\"jill@example.com\\\"\u003e\"\njill_two.inspect       # \"#\u003cPerson @name=\\\"Jill Smith\\\", @email=\\\"jill@example.com\\\"\u003e\"\njack.inspect           # \"#\u003cPerson @name=\\\"Jack Smith\\\", @email=\\\"jack@example.com\\\"\u003e\"\n\njill == jill           # true\njill == jill_two       # true\njill == jack           # false\n\njill.diff(jill)        # {}\njill.diff(jack)        # {\n                       #   name: [\"Jill Smith\", \"Jack Smith\"],\n                       #   email: [\"jill@example.com\", \"jack@example.com\"]\n                       # }\njill.diff(Object.new)  # {:name=\u003e[\"Jill Smith\", nil], :email=\u003e[\"jill@example.com\", nil]}\n\njill.eql? jill         # true\njill.eql? jill_two     # true\njill.eql? jack         # false\n\njill.equal? jill       # true\njill.equal? jill_two   # false\njill.equal? jack       # false\n\njill.hash              # 3650965837788801745\njill_two.hash          # 3650965837788801745\njack.hash              # 4460658980509842640\n\njill.to_a              # [\"Jill Smith\", \"jill@example.com\"]\njack.to_a              # [\"Jack Smith\", \"jack@example.com\"]\n\njill.to_h              # {:name=\u003e\"Jill Smith\", :email=\u003e\"jill@example.com\"}\njack.to_h              # {:name=\u003e\"Jack Smith\", :email=\u003e\"jack@example.com\"}\n\njill.to_s              # \"#\u003cPerson @name=\\\"Jill Smith\\\", @email=\\\"jill@example.com\\\"\u003e\"\njill_two.to_s          # \"#\u003cPerson @name=\\\"Jill Smith\\\", @email=\\\"jill@example.com\\\"\u003e\"\njack.to_s              # \"#\u003cPerson @name=\\\"Jack Smith\\\", @email=\\\"jack@example.com\\\"\u003e\"\n\njill.with name: \"Sue\"  # #\u003cPerson @name=\"Sue\", @email=\"jill@example.com\"\u003e\njill.with bad: \"!\"     # unknown keyword: :bad (ArgumentError)\n----\n\nAs you can see, object equality is determined by the object's values and _not_ by the object's identity. When you include `Wholeable` along with a list of keys, the following happens:\n\n. The corresponding _public_ `attr_reader` (or `attr_accessor` if mutable) for each key is created which saves you time and reduces double entry when implementing your whole value object.\n. The `#to_a`, `#to_h`, and `#to_s` methods are added for convenience and to be compatible with {data_link} and {structs_link}.\n. The `#deconstruct` and `#deconstruct_keys` aliases are created so you can leverage {pattern_matching_link}.\n. The `#==`, `#eql?`, `#hash`, `#inspect`, and `#with` methods are added to provide whole value behavior.\n. The object is immediately frozen after initialization to ensure your instance is _immutable_ by default.\n\n=== Initialization\n\nAs shown above, you can create an instance of your whole value object by using `.[]`. Example:\n\n[source,ruby]\n----\nPerson[name: \"Jill Smith\", email: \"jill@example.com\"]\n----\n\nAlternatively, you can create new instances using `.new`. Example:\n\n[source,ruby]\n----\nPerson.new name: \"Jill Smith\", email: \"jill@example.com\"\n----\n\nBoth methods work but use `.[]` when supplying arguments and `.new` when you don't have any arguments.\n\n=== Mutability\n\nAll whole value objects are frozen by default. You can change behavior by specifying whether instances should be mutable by passing `kind: :mutable` as a keyword argument. Example:\n\n[source,ruby]\n----\nclass Person\n  include Wholeable[:name, :email, kind: :mutable]\n\n  def initialize name: \"Jill\", email: \"jill@example.com\"\n    @name = name\n    @email = email\n  end\nend\n\njill = Person.new\njill.frozen? # false\n----\n\nWhen your object is mutable, you'll also have access to setter methods in addition to the normal getter methods. Example:\n\n[source,ruby]\n----\njill.name # \"Jill\"\njill.name = \"Jayne\"\njill.name # \"Jayne\"\n----\n\nYou can also make your object immutable by using `kind: :immutable` but this is default behavior and redundant. Any invalid kind (example: `kind: :bogus`) will be ignored and default to being immutable.\n\n=== Inheritance\n\nUnlike {data_link} or {structs_link}, you can subclass a whole value object. Example:\n\n[source,ruby]\n----\nclass Person\n  include Wholeable[:name]\n\n  def initialize name:\n    @name = name\n  end\nend\n\nclass Contact \u003c Person\n  include Wholeable[:email]\n\n  def initialize(email:, **)\n    super(**)\n    @email = email\n  end\nend\n\ncontact = Contact[name: \"Jill Smith\", email: \"jill@example.com\"]\n\ncontact.to_h     # {name: \"Jill Smith\", email: \"jill@example.com\"}\ncontact.frozen?  # true\n----\n\nNotice `Contact` inherits from `Person` while only defining the attributes that make it unique. You don't need to redefine the same attributes found in the superclass as that would be redundant and defeat the purpose of subclassing in the first place.\n\nWhen subclassing, each subclass has access to the same attributes defined by the superclass no matter how deep your ancestry is. This does mean you must pass the remaining attributes to the superclass via the double splat.\n\nMutability is honored but is specific to each object in the ancestry. In other words, if the entire ancestry is immutable then no object can mutate an attribute defined in the ancestry. The same applies if the entire ancestry is mutable except, now, any child can mutate any attribute previously defined by the ancestry. Any attribute that is mutated is only mutated specific to the subclass as is standard inheritance behavior.\n\nIf your ancestry is a mixed (immutable and mutable) then behavior is specific to each child in the ancestry. This means a mutable child won't make the entire ancestry mutable, only the child will be mutable. Best practice is to architect your ancestry so immutability or mutability is the same across all objects. To illustrate, here's an example with an immutable parent and mutable child:\n\n[source,ruby]\n----\nclass Parent\n  include Wholeable[:one]\n\n  def initialize one: 1\n    @one = one\n  end\nend\n\nclass Child \u003c Parent\n  include Wholeable[:two, kind: :mutable]\n\n  def initialize(two: 2, **)\n    super(**)\n    @two = two\n  end\nend\n\nchild = Child.new\n\nchild.one = 100  # NoMethodError\nchild.two = 200  # 200\nchild.frozen?    # false\n----\n\nNotice, when attempting to mutate the `one` attribute, you get a `NoMethodError`. This is because `#one=` is defined by the _immutable_ parent while `#two=` is defined on the _mutable_ child.\n\nIf you the flip mutability of your ancestry, you can make your parent mutable while the child immutable for different behavior. Example:\n\n[source,ruby]\n----\nclass Parent\n  include Wholeable[:one, kind: :mutable]\n\n  def initialize one: 1\n    @one = one\n  end\nend\n\nclass Child \u003c Parent\n  include Wholeable[:two]\n\n  def initialize(two: 2, **)\n    super(**)\n    @two = two\n  end\nend\n\nchild = Child.new\n\nchild.one = 100  # FrozenError\nchild.two = 200  # NoMethodError\nchild.frozen?    # true\n----\n\nIn this case, you get a `FrozenError` for `#one=` because the parent is _mutable_ and defined the `#one=` method but the child is _immutable_ which caused the associated attribute to be frozen. On the other hand, the `#two=` method is never defined by the subclass due to being immutable and so you you get a: `NoMethodError`.\n\n_Again, if using inheritance, ensure immutability or mutability remains consistent throughout the entire ancestry._\n\n== Caveats\n\nWhole values can be broken via the following situations:\n\n* *Post Attributes*: Adding additional attributes after what is defined when including `Wholeable` will break your whole value object. To prevent this, let Wholeable manage this for you (easiest). Otherwise (harder), you can manually override `#==`, `#eql?`, `#hash`, `#inspect`, `#to_a`, and `#to_h` behavior at which point you don't need Wholeable anymore.\n* *Deep Freezing*: The automatic freezing of your instances is shallow and will not deep freeze nested attributes. This behavior mimics the behavior of {data_link} objects.\n\n== Performance\n\nThe performance of this gem is good but definitely slower than native support for {data_link} and {structs_link} because they are written in C. To illustrate, here's a micro benchmark for comparison:\n\n----\nINITIALIZATION\n\nruby 3.3.5 (2024-09-03 revision ef084cc8f4) +YJIT [arm64-darwin23.6.0]\nWarming up --------------------------------------\n                Data   470.027k i/100ms\n              Struct   422.010k i/100ms\n               Whole   805.945k i/100ms\nCalculating -------------------------------------\n                Data      4.750M (± 1.1%) i/s  (210.53 ns/i) -     23.971M in   5.047225s\n              Struct      4.579M (± 1.1%) i/s  (218.38 ns/i) -     23.211M in   5.069228s\n               Whole      9.408M (± 1.2%) i/s  (106.29 ns/i) -     47.551M in   5.055033s\n\nComparison:\n               Whole:  9407938.7 i/s - 1.60x  slower\n                Data:  4750013.8 i/s - 3.17x  slower\n              Struct:  4579253.1 i/s - 3.28x  slower\n\nBEHAVIOR\n\nruby 3.3.5 (2024-09-03 revision ef084cc8f4) +YJIT [arm64-darwin23.6.0]\nWarming up --------------------------------------\n                Data   129.006k i/100ms\n              Struct   129.832k i/100ms\n           Wholeable    78.861k i/100ms\nCalculating -------------------------------------\n                Data      1.336M (± 3.6%) i/s  (748.33 ns/i) -      6.708M in   5.027517s\n              Struct      1.341M (± 1.7%) i/s  (745.89 ns/i) -      6.751M in   5.037050s\n           Wholeable    816.232k (± 1.9%) i/s    (1.23 μs/i) -      4.101M in   5.025751s\n\nComparison:\n              Struct:  1340687.5 i/s\n                Data:  1336304.1 i/s - same-ish: difference falls within error\n           Wholeable:   816232.0 i/s - 1.64x  slower\n----\n\nWhile the above isn't bad, you can definitely see this gem is slower than Ruby's own native objects when interacting with it despite being faster upon initialization.\n\nDefault to using {data_link} or {structs_link} but, if you find yourself needing a whole value object with more behavior than what a `Data` or `Struct` can provide, then this gem is a good solution.\n\n== Development\n\nTo contribute, run:\n\n[source,bash]\n----\ngit clone https://github.com/bkuhlmann/wholeable\ncd wholeable\nbin/setup\n----\n\nYou can also use the IRB console for direct access to all objects:\n\n[source,bash]\n----\nbin/console\n----\n\n== Tests\n\nTo test, run:\n\n[source,bash]\n----\nbin/rake\n----\n\n== link:https://alchemists.io/policies/license[License]\n\n== link:https://alchemists.io/policies/security[Security]\n\n== link:https://alchemists.io/policies/code_of_conduct[Code of Conduct]\n\n== link:https://alchemists.io/policies/contributions[Contributions]\n\n== link:https://alchemists.io/policies/developer_certificate_of_origin[Developer Certificate of Origin]\n\n== link:https://alchemists.io/projects/wholeable/versions[Versions]\n\n== link:https://alchemists.io/community[Community]\n\n== Credits\n\n* Built with link:https://alchemists.io/projects/gemsmith[Gemsmith].\n* Engineered by link:https://alchemists.io/team/brooke_kuhlmann[Brooke Kuhlmann].\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbkuhlmann%2Fwholeable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbkuhlmann%2Fwholeable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbkuhlmann%2Fwholeable/lists"}