immutable-struct 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +8 -5
- data/lib/immutable-struct.rb +13 -3
- data/spec/immutable_struct_spec.rb +9 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e4b4a2c3e90fd194dd9f1085251225de29879d3
|
4
|
+
data.tar.gz: 3e28dc475148a8fe9c6513c057ca405e303fc052
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: debc18bb3ae8aefdf002b22725e2fb67e6b9fdcb32a550831e25c336eeeaaa5d6d6fc539d3f0fa7e7e3b6972116516128f20dcbbee439ade9d320a3f439bd208
|
7
|
+
data.tar.gz: 52c21f88124f4de2edaf137fad39100b8f85e91cc89878b3570998b40f53df8f108261807c5df3432a8b754614bbb1c56ba9a7d699a18cde6d4733c767fb641d
|
data/README.rdoc
CHANGED
@@ -23,7 +23,7 @@ If not using bundler, just use RubyGems:
|
|
23
23
|
|
24
24
|
== To use
|
25
25
|
|
26
|
-
Person =
|
26
|
+
Person = ImmutableStruct.new(:name, :age, :job, :active?, [:addresses]) do
|
27
27
|
def minor?
|
28
28
|
age < 18
|
29
29
|
end
|
@@ -33,10 +33,13 @@ If not using bundler, just use RubyGems:
|
|
33
33
|
age: 40, # age will be 40
|
34
34
|
# job is omitted, so will be nil
|
35
35
|
active: true) # active and active? will be true
|
36
|
-
|
37
|
-
|
38
|
-
p.
|
39
|
-
p.
|
36
|
+
# addresses is omitted, but since we've selected
|
37
|
+
# Array coercion, it'll be []
|
38
|
+
p.name # => "Dave"
|
39
|
+
p.age # => 40
|
40
|
+
p.active? # => true
|
41
|
+
p.minor? # => false
|
42
|
+
p.addresses # => []
|
40
43
|
|
41
44
|
You can also treat the interior as a normal class definition.
|
42
45
|
|
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.1.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.
|
@@ -14,6 +14,9 @@ class ImmutableStruct
|
|
14
14
|
# an attribute without a question mark that passes through the raw
|
15
15
|
# value and an attribute *with* the question mark that coerces that
|
16
16
|
# value to a boolean. You would initialize it with the non-question-mark value
|
17
|
+
# An attribute that is an array of one symbol will create an attribute named for
|
18
|
+
# that symbol, but that doesn't return nil, instead returning the +to_a+ of the
|
19
|
+
# value passed to the construtor.
|
17
20
|
# block:: if present, evaluates in the context of the new class, so +def+, +def.self+, +include+
|
18
21
|
# and +extend+ should all work as in a normal class definition.
|
19
22
|
#
|
@@ -42,6 +45,8 @@ class ImmutableStruct
|
|
42
45
|
define_method(attribute) do
|
43
46
|
!!instance_variable_get("@#{raw_name}")
|
44
47
|
end
|
48
|
+
elsif attribute.kind_of?(Array) and attribute.size == 1
|
49
|
+
attr_reader attribute[0]
|
45
50
|
else
|
46
51
|
attr_reader attribute
|
47
52
|
end
|
@@ -50,8 +55,13 @@ class ImmutableStruct
|
|
50
55
|
define_method(:initialize) do |*args|
|
51
56
|
attrs = args[0] || {}
|
52
57
|
attributes.each do |attribute|
|
53
|
-
|
54
|
-
|
58
|
+
if attribute.kind_of?(Array) and attribute.size == 1
|
59
|
+
ivar_name = attribute[0].to_s
|
60
|
+
instance_variable_set("@#{ivar_name}", (attrs[ivar_name.to_s] || attrs[ivar_name.to_sym]).to_a)
|
61
|
+
else
|
62
|
+
ivar_name = attribute.to_s.gsub(/\?$/,'')
|
63
|
+
instance_variable_set("@#{ivar_name}",attrs[ivar_name.to_s] || attrs[ivar_name.to_sym])
|
64
|
+
end
|
55
65
|
end
|
56
66
|
end
|
57
67
|
end
|
@@ -53,6 +53,15 @@ describe ImmutableStruct do
|
|
53
53
|
|
54
54
|
end
|
55
55
|
|
56
|
+
context "allows for values that should be coerced to collections" do
|
57
|
+
it "can define an array value that should never be nil" do
|
58
|
+
klass = ImmutableStruct.new([:foo], :bar)
|
59
|
+
instance = klass.new
|
60
|
+
instance.foo.should == []
|
61
|
+
instance.bar.should == nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
56
65
|
it "allows defining instance methods" do
|
57
66
|
klass = ImmutableStruct.new(:foo, :bar) do
|
58
67
|
def derived; self.foo + ":" + self.bar; end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: immutable-struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stitch Fix Engineering
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-02-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
92
|
version: '0'
|
93
93
|
requirements: []
|
94
94
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.2
|
95
|
+
rubygems_version: 2.4.2
|
96
96
|
signing_key:
|
97
97
|
specification_version: 4
|
98
98
|
summary: Easily create value objects without the pain of Ruby's Struct (or its setters)
|