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 +4 -4
- data/.ruby-version +1 -1
- data/.travis.yml +1 -0
- data/CODE_OF_CONDUCT.md +6 -0
- data/CONTRIBUTING.md +18 -0
- data/README.rdoc +11 -0
- data/immutable-struct.gemspec +3 -3
- data/lib/immutable-struct.rb +12 -2
- data/spec/immutable_struct_spec.rb +54 -1
- metadata +9 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c5b40b44baa7449ead521ed6adcc3d29f2d631c
|
4
|
+
data.tar.gz: afbd6ff4c6959e8cc1cde9432e53e606484ff4cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 95da502d7c1f22a08bf24ec16a4c543350ef51d2124e88d865df9c7c61ec746fee65bd068dde00ba02a5822f2aa928b0c50e7d90f51229bd669323d5cba74518
|
7
|
+
data.tar.gz: 26aa7ba089b984a87362b6b1404763f6a453dc07f6ffcb61ce7cff40e689d62a6cfdb60691b77b525e052fa834b54799008a37b979634faeefc88f6f03b37c17
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.2.2
|
data/.travis.yml
CHANGED
data/CODE_OF_CONDUCT.md
ADDED
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
|
data/immutable-struct.gemspec
CHANGED
@@ -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
|
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
|
|
data/lib/immutable-struct.rb
CHANGED
@@ -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.
|
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({})
|
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
|
-
|
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.
|
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-
|
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.
|
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)
|