immutable-struct 2.1.1 → 2.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: af44014a283429f556ddd83301f23cf85b8ce988
4
- data.tar.gz: f749729fb930b33ed6a6a48014e982a0253f9958
3
+ metadata.gz: 5c5b40b44baa7449ead521ed6adcc3d29f2d631c
4
+ data.tar.gz: afbd6ff4c6959e8cc1cde9432e53e606484ff4cb
5
5
  SHA512:
6
- metadata.gz: a23377a3878c39a98af6bb91a5f46964344119732b6c7ba8df3eab2c20490a0ee3ed49caf2dda55194f073b48b8fd3dbf54823c6e085b01085bace42ced5ec5a
7
- data.tar.gz: 130d852c29953497d09d9fe5a6248bdd11d56cdafe13a266417ad8e76d435b775a39e466bfcc2880f6a7952aeccf592c39eb5a1b1e60aab235955efb24c0b7a0
6
+ metadata.gz: 95da502d7c1f22a08bf24ec16a4c543350ef51d2124e88d865df9c7c61ec746fee65bd068dde00ba02a5822f2aa928b0c50e7d90f51229bd669323d5cba74518
7
+ data.tar.gz: 26aa7ba089b984a87362b6b1404763f6a453dc07f6ffcb61ce7cff40e689d62a6cfdb60691b77b525e052fa834b54799008a37b979634faeefc88f6f03b37c17
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.1.0
1
+ 2.2.2
data/.travis.yml CHANGED
@@ -3,5 +3,6 @@ rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
5
  - 2.1.0
6
+ - 2.2.2
6
7
  - rbx-2
7
8
  - ruby-head
@@ -0,0 +1,6 @@
1
+ # Code of Conduct
2
+
3
+ We are committed to keeping our community open and inclusive.
4
+
5
+ **Our Code of Conduct can be found here**:
6
+ http://opensource.stitchfix.com/code-of-conduct.html
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,18 @@
1
+ # Contributing
2
+ Thanks for using and improving *ImmutableStruct*! If you'd like to help out, check out [the project's issues list][issues] for ideas on what could be improved. If there's an idea you'd like to propose, or a design change, feel free to file a new issue or send a pull request:
3
+
4
+ 1. [Fork][fork] the repo.
5
+ 1. [Create a topic branch.][branch]
6
+ 1. Write tests.
7
+ 1. Implement your feature or fix bug.
8
+ 1. Add, commit, and push your changes.
9
+ 1. [Submit a pull request.][pr]
10
+
11
+ [fork]: https://help.github.com/articles/fork-a-repo/
12
+ [branch]: https://help.github.com/articles/creating-and-deleting-branches-within-your-repository/
13
+ [pr]: https://help.github.com/articles/using-pull-requests/
14
+
15
+ ## General Guidelines
16
+
17
+ * When in doubt, test it. If you can't test it, re-think what you are doing.
18
+ * Code formatting and internal application architecture should be consistent.
data/README.rdoc CHANGED
@@ -41,6 +41,17 @@ If not using bundler, just use RubyGems:
41
41
  p.minor? # => false
42
42
  p.addresses # => []
43
43
 
44
+ p2 = Person.new(name: "Dave", age: 40, active: true)
45
+
46
+ p == p2 # => true
47
+ p.eql?(p2) # => true
48
+
49
+ SimilarPerson = ImmutableStruct.new(:name, :age, :job, :active?, [:addresses])
50
+
51
+ sp = SimilarPerson.new(name: "Dave", age: 40, active: true)
52
+
53
+ p == sp # => false # Different class leads to inequality
54
+
44
55
  You can also treat the interior as a normal class definition.
45
56
 
46
57
  == Links
@@ -6,10 +6,10 @@ require 'immutable-struct'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "immutable-struct"
8
8
  spec.version = ImmutableStruct::VERSION
9
- spec.authors = ["Stitch Fix Engineering"]
10
- spec.email = ["opensource@stitchfix.com"]
9
+ spec.authors = ["Stitch Fix Engineering","Dave Copeland","Simeon Willbanks"]
10
+ spec.email = ["opensource@stitchfix.com","davetron5000@gmail.com","simeon@simeons.net"]
11
11
  spec.description = %q{Easily create value objects without the pain of Ruby's Struct (or its setters)}
12
- spec.summary = %q{Easily create value objects without the pain of Ruby's Struct (or its setters)}
12
+ spec.summary = %q{Easily create value objects without the pain of Ruby's Struct (or its setters)}
13
13
  spec.homepage = "https://github.com/stitchfix/immutable-struct"
14
14
  spec.license = "MIT"
15
15
 
@@ -6,7 +6,7 @@
6
6
  # will be evaluated as if it were inside a class definition, allowing you
7
7
  # to add methods, include or extend modules, or do whatever else you want.
8
8
  class ImmutableStruct
9
- VERSION='2.1.1' #:nodoc:
9
+ VERSION='2.1.2' #:nodoc:
10
10
  # Create a new class with the given read-only attributes.
11
11
  #
12
12
  # attributes:: list of symbols or strings that can be used to create attributes.
@@ -64,12 +64,22 @@ class ImmutableStruct
64
64
  end
65
65
  end
66
66
  end
67
+
68
+ define_method(:==) do |other|
69
+ return false unless other.is_a?(klass)
70
+ attributes.all? { |attribute| self.send(attribute) == other.send(attribute) }
71
+ end
72
+
73
+ alias_method :eql?, :==
67
74
  end
68
75
  klass.class_exec(&block) unless block.nil?
69
76
  imethods = klass.instance_methods(include_super=false)
70
77
  klass.class_exec(imethods) do |imethods|
71
78
  define_method(:to_h) do
72
- imethods.inject({}){ |hash, method| hash.merge(method.to_sym => self.send(method)) }
79
+ imethods.inject({}) do |hash, method|
80
+ next hash if [:==, :eql?].include?(method)
81
+ hash.merge(method.to_sym => self.send(method))
82
+ end
73
83
  end
74
84
  end
75
85
  klass
@@ -73,7 +73,7 @@ describe ImmutableStruct do
73
73
  it "allows defining class methods" do
74
74
  klass = ImmutableStruct.new(:foo, :bar) do
75
75
  def self.from_array(array)
76
- self.new(foo: array[0], bar: array[1])
76
+ new(foo: array[0], bar: array[1])
77
77
  end
78
78
  end
79
79
  instance = klass.from_array(["hello","world"])
@@ -113,4 +113,57 @@ describe ImmutableStruct do
113
113
  instance.to_h.should == {flappy: 'bird', lawsuit: 'pending'}
114
114
  end
115
115
  end
116
+
117
+ describe "equality" do
118
+
119
+ before do
120
+ klass_1 = ImmutableStruct.new(:foo, :bar)
121
+ klass_2 = ImmutableStruct.new(:foo, :bar)
122
+ @k1_a = klass_1.new(foo: 'foo', bar: 'bar')
123
+ @k1_b = klass_1.new(foo: 'xxx', bar: 'yyy')
124
+ @k1_c = klass_1.new(foo: 'foo', bar: 'bar')
125
+ @k2_a = klass_2.new(foo: 'foo', bar: 'bar')
126
+ end
127
+
128
+ describe "==" do
129
+
130
+ it "should be equal to itself" do
131
+ (@k1_a == @k1_a).should be true
132
+ end
133
+
134
+ it "should be equal to same class with identical attribute values" do
135
+ (@k1_a == @k1_c).should be true
136
+ end
137
+
138
+ it 'should not be equal to same class with different attribute values' do
139
+ (@k1_a == @k1_b).should be false
140
+ end
141
+
142
+ it 'should not be equal to different class with identical attribute values' do
143
+ (@k1_a == @k3_a).should be false
144
+ end
145
+
146
+ end
147
+
148
+ describe "eql?" do
149
+
150
+ it "should be equal to itself" do
151
+ @k1_a.eql?(@k1_a).should be true
152
+ end
153
+
154
+ it "should be equal to same class with identical attribute values" do
155
+ @k1_a.eql?(@k1_c).should be true
156
+ end
157
+
158
+ it 'should not be equal to same class with different attribute values' do
159
+ @k1_a.eql?(@k1_b).should be false
160
+ end
161
+
162
+ it 'should not be equal to different class with identical attribute values' do
163
+ @k1_a.eql?(@k3_a).should be false
164
+ end
165
+
166
+ end
167
+
168
+ end
116
169
  end
metadata CHANGED
@@ -1,14 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immutable-struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stitch Fix Engineering
8
+ - Dave Copeland
9
+ - Simeon Willbanks
8
10
  autorequire:
9
11
  bindir: bin
10
12
  cert_chain: []
11
- date: 2015-05-21 00:00:00.000000000 Z
13
+ date: 2015-10-21 00:00:00.000000000 Z
12
14
  dependencies:
13
15
  - !ruby/object:Gem::Dependency
14
16
  name: bundler
@@ -56,6 +58,8 @@ description: Easily create value objects without the pain of Ruby's Struct (or i
56
58
  setters)
57
59
  email:
58
60
  - opensource@stitchfix.com
61
+ - davetron5000@gmail.com
62
+ - simeon@simeons.net
59
63
  executables: []
60
64
  extensions: []
61
65
  extra_rdoc_files: []
@@ -64,6 +68,8 @@ files:
64
68
  - ".ruby-gemset"
65
69
  - ".ruby-version"
66
70
  - ".travis.yml"
71
+ - CODE_OF_CONDUCT.md
72
+ - CONTRIBUTING.md
67
73
  - Gemfile
68
74
  - LICENSE.txt
69
75
  - README.rdoc
@@ -92,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
98
  version: '0'
93
99
  requirements: []
94
100
  rubyforge_project:
95
- rubygems_version: 2.4.1
101
+ rubygems_version: 2.4.8
96
102
  signing_key:
97
103
  specification_version: 4
98
104
  summary: Easily create value objects without the pain of Ruby's Struct (or its setters)