Are you happy with your logging solution? Would you help us out by taking a 30-second survey? Click here

immutable-struct

Create struct-like classes that don't have setters, but have an awesome constructor.

Subscribe to updates I use immutable-struct


Statistics on immutable-struct

Number of watchers on Github 102
Number of open issues 3
Average time to close an issue 3 months
Main language Ruby
Average time to merge a PR 3 days
Open pull requests 11+
Closed pull requests 3+
Last commit over 1 year ago
Repo Created almost 5 years ago
Repo Last Updated over 1 year ago
Size 396 KB
Homepage https://stitchfix...
Organization / Authorstitchfix
Latest Releasev2.2.3
Contributors6
Page Updated
Do you use immutable-struct? Leave a review!
View open issues (3)
View immutable-struct activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating immutable-struct for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

= ImmutableStruct

{Build Status}[https://travis-ci.org/stitchfix/immutable-struct]

Creates struct-like classes (that can build value objects) that do not have setters and also have better constructors than Ruby's built-in +Struct+.

This 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.

== Install

Add to your +Gemfile+:

gem 'immutable-struct'

Then install:

bundle install

If not using bundler, just use RubyGems:

gem install immutable-struct

== To use

Person = ImmutableStruct.new(:name, :age, :job, :active?, [:addresses]) do
  def minor?
    age < 18
  end
end

p = Person.new(name: "Dave",   # name will be 'Dave'
               age: 40,        # age will be 40
                               # job is omitted, so will be nil
               active: true)   # active and active? will be true
                               # addresses is omitted, but since we've selected
                               # Array coercion, it'll be []
p.name      # => "Dave"
p.age       # => 40
p.active?   # => true
p.minor?    # => false
p.addresses # => []

p2 = Person.new(name: "Dave", age: 40, active: true)

p == p2     # => true
p.eql?(p2)  # => true

SimilarPerson = ImmutableStruct.new(:name, :age, :job, :active?, [:addresses])

sp = SimilarPerson.new(name: "Dave", age: 40, active: true)

p == sp     # => false         # Different class leads to inequality

new_person = p.merge(name: "Other Dave", age: 41) # returns a new object with merged attributes
new_person.name    # => "Other Dave"
new_person.age     # => 41
new_person.active? # => true

You can treat the interior of the block as a normal class definition with the exception of setting constants. Use +const_set+ to scope constants as-expected.

Point = ImmutableStruct.new(:x, :y) do
  const_set(:ZERO, 0)
  ONE_HUNDRED = 100
end
Point::ZERO # => 0
::ONE_HUNDRED # => 100
::ZERO # => NameError: uninitialized constant ZERO    

== Links

  • rdoc[http://stitchfix.github.io/immutable-struct]
  • source[http://github.com/stitchfix/immutable-struct]
  • blog[http://technology.stitchfix.com/blog/2013/12/20/presenters-delegation-vs-structs/]
immutable-struct open issues Ask a question     (View All Issues)
  • about 4 years JRuby support status
immutable-struct open pull requests (View All Pulls)
  • Bundle Update on 2016-01-28
  • to_h will only call methods with zero args
  • Fixes #4
  • Makes attributes and instance immutable
  • Bundle update 2016-12
  • Bundle Update 2017-05
  • Bundle Update 2017-04
  • Bundle Update 2017-03
  • Bundle Update 2017-02
  • Bundle update for 2017-Oct-05
  • Bundle Update 2017-08
immutable-struct questions on Stackoverflow (View All Questions)
  • Shortest way to write immutable struct in C#
  • How Swift captures mutable and immutable struct
  • What are the benefits of an immutable struct over a mutable one?
  • Forced immutable struct field in Rust
  • How to declare an immutable struct with public fields?
  • XAML binding to an immutable struct
  • When should I implement GetHashCode on an immutable struct?
  • Should this immutable struct be a mutable class?
  • Serializing an immutable struct in a SecurityTransparent assembly
  • Immutable class vs Immutable struct
  • Immutable struct with collection
immutable-struct list of languages used
immutable-struct latest release notes

RC release to incorporate #33, which allows .to_json calls on ImmutableStruct objects to be made recursively and include all Zero-argument methods as well.

  • [FIX] Handle array attributes in hash/equality methods. Fixed with PR #28
  • Added hash method to enable immutable-struct instances to be Set-friendly. Hash code values are constructed from the state (all attribute values) and the class
  • Modified to_h method to only call methods with zero args. Addresses issue logged as #to_h serializes all instance methods from @nandosola's PR. Thank you, @nandosola!
Other projects in Ruby