{"id":19446053,"url":"https://github.com/stitchfix/immutable-struct","last_synced_at":"2025-05-15T05:06:11.631Z","repository":{"id":21384022,"uuid":"24701642","full_name":"stitchfix/immutable-struct","owner":"stitchfix","description":"Create struct-like classes that don't have setters, but have an awesome constructor.","archived":false,"fork":false,"pushed_at":"2025-04-01T16:58:49.000Z","size":464,"stargazers_count":172,"open_issues_count":3,"forks_count":7,"subscribers_count":115,"default_branch":"main","last_synced_at":"2025-04-15T00:49:46.762Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://stitchfix.github.io/immutable-struct/","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/stitchfix.png","metadata":{"files":{"readme":"README.rdoc","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","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,"zenodo":null}},"created_at":"2014-10-02T01:12:39.000Z","updated_at":"2025-04-01T16:58:52.000Z","dependencies_parsed_at":"2023-02-14T11:46:01.610Z","dependency_job_id":"8590ae61-4d2a-405f-a3bc-a42597516355","html_url":"https://github.com/stitchfix/immutable-struct","commit_stats":{"total_commits":128,"total_committers":25,"mean_commits":5.12,"dds":0.6796875,"last_synced_commit":"9fab6c83f96efe6d3839226b8e52bbf871d28183"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stitchfix%2Fimmutable-struct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stitchfix%2Fimmutable-struct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stitchfix%2Fimmutable-struct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stitchfix%2Fimmutable-struct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stitchfix","download_url":"https://codeload.github.com/stitchfix/immutable-struct/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254276447,"owners_count":22043867,"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":[],"created_at":"2024-11-10T16:12:42.593Z","updated_at":"2025-05-15T05:06:06.620Z","avatar_url":"https://github.com/stitchfix.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"= ImmutableStruct\n\n{\u003cimg src=\"https://travis-ci.org/stitchfix/immutable-struct.svg?branch=master\" alt=\"Build Status\" /\u003e}[https://travis-ci.org/stitchfix/immutable-struct]\n\nCreates struct-like classes (that can build value objects) that do not have setters and also have better constructors than Ruby's built-in +Struct+.\n\nThis is highly useful for creating presenters, non-database-related models, or other quick and dirty classes in your application.  Instead of using a +Hash+ or +OpenStruct+, you can create a bit more clarity around your types by using +ImmutableStruct+, which is almost as convienient.\n\n== Install\n\nAdd to your +Gemfile+:\n\n    gem 'immutable-struct'\n\nThen install:\n\n    bundle install\n\nIf not using bundler, just use RubyGems:\n\n    gem install immutable-struct\n\n\n== To use\n\n    Person = ImmutableStruct.new(:name, :age, :job, :active?, [:addresses]) do\n      def minor?\n        age \u003c 18\n      end\n    end\n\n    p = Person.new(name: \"Dave\",   # name will be 'Dave'\n                   age: 40,        # age will be 40\n                                   # job is omitted, so will be nil\n                   active: true)   # active and active? will be true\n                                   # addresses is omitted, but since we've selected\n                                   # Array coercion, it'll be []\n    p.name      # =\u003e \"Dave\"\n    p.age       # =\u003e 40\n    p.active?   # =\u003e true\n    p.minor?    # =\u003e false\n    p.addresses # =\u003e []\n\n    p2 = Person.new(name: \"Dave\", age: 40, active: true)\n\n    p == p2     # =\u003e true\n    p.eql?(p2)  # =\u003e true\n\n    SimilarPerson = ImmutableStruct.new(:name, :age, :job, :active?, [:addresses])\n\n    sp = SimilarPerson.new(name: \"Dave\", age: 40, active: true)\n\n    p == sp     # =\u003e false         # Different class leads to inequality\n\n    new_person = p.merge(name: \"Other Dave\", age: 41) # returns a new object with merged attributes\n    new_person.name    # =\u003e \"Other Dave\"\n    new_person.age     # =\u003e 41\n    new_person.active? # =\u003e true\n\nYou can coerce values into struct types by using the +from+ method.\nThis is similar to Ruby's conversion functions, e.g. Integer(\"1\").\n\n    dave = Person.from(p)\n    dave.equal?(p) # =\u003e true (object equality)\n\n    daveish = Person.from(dave.to_h)\n    daveish.equal?(dave) # =\u003e false\n    daveish == dave      # =\u003e true\n\nYou can treat the interior of the block as a normal class definition with the exception of setting constants.\nUse +const_set+ to scope constants as-expected.\n\n    Point = ImmutableStruct.new(:x, :y) do\n      const_set(:ZERO, 0)\n      ONE_HUNDRED = 100\n    end\n    Point::ZERO # =\u003e 0\n    ::ONE_HUNDRED # =\u003e 100\n    ::ZERO # =\u003e NameError: uninitialized constant ZERO\n\n\n== Links\n\n* rdoc[http://stitchfix.github.io/immutable-struct]\n* source[http://github.com/stitchfix/immutable-struct]\n* blog[http://technology.stitchfix.com/blog/2013/12/20/presenters-delegation-vs-structs/]\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstitchfix%2Fimmutable-struct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstitchfix%2Fimmutable-struct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstitchfix%2Fimmutable-struct/lists"}