immutable-struct 2.1.2 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +2 -0
- data/CONTRIBUTING.md +1 -1
- data/README.rdoc +6 -0
- data/lib/immutable-struct.rb +12 -2
- data/spec/immutable_struct_spec.rb +18 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e33fc2fff0d454a888f54e3897851bd398b177a
|
4
|
+
data.tar.gz: ee9e7a6649f6165a9996b387b23aa16a6d76232a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 078b7691d7d08ae2bf66671243dba88f9db0f8c36ae97578cf7cd30a28857ccc937ddafbd2cc8754f418c30027291f7a2be34dd4e8481bf53a19f3d364c7d880
|
7
|
+
data.tar.gz: 2729cf68d9fcd5ab0597e5596b504cf23069bed3b98b4db2927a1b64b4b306570c78d1bce726a31e03bab2540338c442a80a73d3384afe5969af77cc813e45f5
|
data/.travis.yml
CHANGED
data/CONTRIBUTING.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# Contributing
|
2
|
-
Thanks for using and improving *ImmutableStruct*! If you'd like to help out, check out [the project's issues list]
|
2
|
+
Thanks for using and improving *ImmutableStruct*! If you'd like to help out, check out [the project's issues list](https://github.com/stitchfix/immutable-struct/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
3
|
|
4
4
|
1. [Fork][fork] the repo.
|
5
5
|
1. [Create a topic branch.][branch]
|
data/README.rdoc
CHANGED
@@ -52,8 +52,14 @@ If not using bundler, just use RubyGems:
|
|
52
52
|
|
53
53
|
p == sp # => false # Different class leads to inequality
|
54
54
|
|
55
|
+
new_person = p.merge(name: "Other Dave", age: 41) # returns a new object with merged attributes
|
56
|
+
new_person.name # => "Other Dave"
|
57
|
+
new_person.age # => 41
|
58
|
+
new_person.active? # => true
|
59
|
+
|
55
60
|
You can also treat the interior as a normal class definition.
|
56
61
|
|
62
|
+
|
57
63
|
== Links
|
58
64
|
|
59
65
|
* rdoc[http://stitchfix.github.io/immutable-struct]
|
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.
|
9
|
+
VERSION='2.2.0' #: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.
|
@@ -36,6 +36,11 @@ class ImmutableStruct
|
|
36
36
|
# p.minor # => "yup"
|
37
37
|
# p.minor? # => true
|
38
38
|
#
|
39
|
+
# new_person = p.merge(name: "Other Dave", age: 41) # returns a new object with merged attributes
|
40
|
+
# new_person.name # => "Other Dave"
|
41
|
+
# new_person.age # => 41
|
42
|
+
# new_person.active? # => true
|
43
|
+
#
|
39
44
|
def self.new(*attributes,&block)
|
40
45
|
klass = Class.new do
|
41
46
|
attributes.each do |attribute|
|
@@ -70,6 +75,11 @@ class ImmutableStruct
|
|
70
75
|
attributes.all? { |attribute| self.send(attribute) == other.send(attribute) }
|
71
76
|
end
|
72
77
|
|
78
|
+
define_method(:merge) do |new_attrs|
|
79
|
+
attrs = to_h
|
80
|
+
klass.new(attrs.merge(new_attrs))
|
81
|
+
end
|
82
|
+
|
73
83
|
alias_method :eql?, :==
|
74
84
|
end
|
75
85
|
klass.class_exec(&block) unless block.nil?
|
@@ -77,7 +87,7 @@ class ImmutableStruct
|
|
77
87
|
klass.class_exec(imethods) do |imethods|
|
78
88
|
define_method(:to_h) do
|
79
89
|
imethods.inject({}) do |hash, method|
|
80
|
-
next hash if [:==, :eql
|
90
|
+
next hash if [:==, :eql?, :merge].include?(method)
|
81
91
|
hash.merge(method.to_sym => self.send(method))
|
82
92
|
end
|
83
93
|
end
|
@@ -114,6 +114,24 @@ describe ImmutableStruct do
|
|
114
114
|
end
|
115
115
|
end
|
116
116
|
|
117
|
+
describe "merge" do
|
118
|
+
it "returns a new object as a result of merging attributes" do
|
119
|
+
klass = ImmutableStruct.new(:food, :snacks, :butter)
|
120
|
+
instance = klass.new(food: 'hot dogs', butter: true)
|
121
|
+
new_instance = instance.merge(snacks: 'candy hot dogs', butter: false)
|
122
|
+
|
123
|
+
instance.food.should == 'hot dogs'
|
124
|
+
instance.butter.should == true
|
125
|
+
instance.snacks.should == nil
|
126
|
+
|
127
|
+
new_instance.food.should == 'hot dogs'
|
128
|
+
new_instance.snacks.should == 'candy hot dogs'
|
129
|
+
new_instance.butter.should == false
|
130
|
+
|
131
|
+
new_instance.object_id.should_not == instance.object_id
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
117
135
|
describe "equality" do
|
118
136
|
|
119
137
|
before do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: immutable-struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stitch Fix Engineering
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2016-01-07 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|