immutable_struct 1.1.1 → 1.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 +7 -0
- data/README.mdown +8 -1
- data/lib/immutable_struct.rb +27 -2
- metadata +12 -21
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 93086edd21bcd5072e8cdb370c3bfed0f2d20531
|
4
|
+
data.tar.gz: e07772aa0a327755e5970a29ff04d26cbca1299c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d5506fccce9550424ce24e893dc135db396dd89756e506fd0ed6ebf6ae7f5a79cff19362754d0d78ce4eb63e58e26c7e3a520fe2b52d6fa6b9e361d3003768e2
|
7
|
+
data.tar.gz: 2872be0b42a810eeecdeb7ba9d5e8a5185ba312539d9548a81a01fe40c87ef4b3abf3b1ed337e8b13b5f7040e87de4cf69717122e5baf50ad48c886c000fe8c6
|
data/README.mdown
CHANGED
@@ -16,5 +16,12 @@ In addition to the above, `ImmutableStruct` enhances the constructor to accept a
|
|
16
16
|
ship = Spaceship.new(:max_speed => '∞', :name => 'The TARDIS')
|
17
17
|
|
18
18
|
puts ship.max_speed # => ∞
|
19
|
+
|
20
|
+
You can create a strict version of the struct, which will raise an error if you initialize it with a Hash that does not contain all the fields
|
21
|
+
of the struct
|
22
|
+
|
23
|
+
StrictSpaceship = Spaceship.strict
|
24
|
+
|
25
|
+
strict_ship = StrictSpaceship.new(:name => 'Enterprise') # raises ArgumentError
|
19
26
|
|
20
|
-
I'm surprised every time I look at the RDoc for `Struct` that it doesn't do this.
|
27
|
+
I'm surprised every time I look at the RDoc for `Struct` that it doesn't do this.
|
data/lib/immutable_struct.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
class ImmutableStruct
|
4
|
-
VERSION = '1.
|
4
|
+
VERSION = '1.2.0'
|
5
5
|
|
6
6
|
def self.new(*attrs, &block)
|
7
7
|
struct = Struct.new(*attrs, &block)
|
@@ -10,7 +10,7 @@ class ImmutableStruct
|
|
10
10
|
extend_dup!(struct)
|
11
11
|
struct
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
private
|
15
15
|
|
16
16
|
def self.make_immutable!(struct)
|
@@ -18,6 +18,12 @@ private
|
|
18
18
|
struct.members.each do |member|
|
19
19
|
struct.send(:undef_method, "#{member}=".to_sym)
|
20
20
|
end
|
21
|
+
struct.instance_variable_set('@strict_mode', false)
|
22
|
+
struct.define_singleton_method(:strict) do
|
23
|
+
cl = self.clone
|
24
|
+
cl.instance_variable_set('@strict_mode', true)
|
25
|
+
cl
|
26
|
+
end
|
21
27
|
end
|
22
28
|
|
23
29
|
def self.optionalize_constructor!(struct)
|
@@ -26,6 +32,14 @@ private
|
|
26
32
|
|
27
33
|
def initialize(*attrs)
|
28
34
|
if members.size > 1 && attrs && attrs.size == 1 && attrs.first.instance_of?(Hash)
|
35
|
+
strict = self.class.instance_variable_get('@strict_mode')
|
36
|
+
if strict
|
37
|
+
attr_keys = attrs.first.keys.map(&:to_sym)
|
38
|
+
diff = members - attr_keys
|
39
|
+
unless diff.empty?
|
40
|
+
raise ArgumentError, "Fields #{diff.join(',')} not provided"
|
41
|
+
end
|
42
|
+
end
|
29
43
|
struct_initialize(*members.map { |m| attrs.first[m.to_sym] })
|
30
44
|
else
|
31
45
|
struct_initialize(*attrs)
|
@@ -38,6 +52,17 @@ private
|
|
38
52
|
h
|
39
53
|
end
|
40
54
|
end
|
55
|
+
|
56
|
+
def encode_with(coder)
|
57
|
+
members.each do |m|
|
58
|
+
coder[m.to_s] = self[m]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def init_with(coder)
|
63
|
+
struct_initialize(*members.map { |m| coder.map[m.to_s] })
|
64
|
+
end
|
65
|
+
|
41
66
|
end
|
42
67
|
end
|
43
68
|
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: immutable_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.2.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Theo Hultberg
|
@@ -10,38 +9,34 @@ authors:
|
|
10
9
|
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
date:
|
12
|
+
date: 2016-03-18 00:00:00.000000000 Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
15
|
name: rspec
|
17
16
|
requirement: !ruby/object:Gem::Requirement
|
18
|
-
none: false
|
19
17
|
requirements:
|
20
|
-
- -
|
18
|
+
- - ">="
|
21
19
|
- !ruby/object:Gem::Version
|
22
20
|
version: '0'
|
23
21
|
type: :development
|
24
22
|
prerelease: false
|
25
23
|
version_requirements: !ruby/object:Gem::Requirement
|
26
|
-
none: false
|
27
24
|
requirements:
|
28
|
-
- -
|
25
|
+
- - ">="
|
29
26
|
- !ruby/object:Gem::Version
|
30
27
|
version: '0'
|
31
28
|
- !ruby/object:Gem::Dependency
|
32
29
|
name: yard
|
33
30
|
requirement: !ruby/object:Gem::Requirement
|
34
|
-
none: false
|
35
31
|
requirements:
|
36
|
-
- -
|
32
|
+
- - ">="
|
37
33
|
- !ruby/object:Gem::Version
|
38
34
|
version: '0'
|
39
35
|
type: :development
|
40
36
|
prerelease: false
|
41
37
|
version_requirements: !ruby/object:Gem::Requirement
|
42
|
-
none: false
|
43
38
|
requirements:
|
44
|
-
- -
|
39
|
+
- - ">="
|
45
40
|
- !ruby/object:Gem::Version
|
46
41
|
version: '0'
|
47
42
|
description: A version of Ruby's Struct class that creates classes whose instances
|
@@ -54,35 +49,31 @@ executables: []
|
|
54
49
|
extensions: []
|
55
50
|
extra_rdoc_files: []
|
56
51
|
files:
|
57
|
-
- lib/immutable_struct.rb
|
58
52
|
- LICENSE
|
59
53
|
- README.mdown
|
54
|
+
- lib/immutable_struct.rb
|
60
55
|
homepage: http://github.com/iconara/immutable_struct
|
61
56
|
licenses: []
|
57
|
+
metadata: {}
|
62
58
|
post_install_message:
|
63
59
|
rdoc_options: []
|
64
60
|
require_paths:
|
65
61
|
- lib
|
66
62
|
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
-
none: false
|
68
63
|
requirements:
|
69
|
-
- -
|
64
|
+
- - ">="
|
70
65
|
- !ruby/object:Gem::Version
|
71
66
|
version: '0'
|
72
|
-
segments:
|
73
|
-
- 0
|
74
|
-
hash: 186220760556065832
|
75
67
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
-
none: false
|
77
68
|
requirements:
|
78
|
-
- -
|
69
|
+
- - ">="
|
79
70
|
- !ruby/object:Gem::Version
|
80
71
|
version: 1.3.6
|
81
72
|
requirements: []
|
82
73
|
rubyforge_project: immutable_struct
|
83
|
-
rubygems_version:
|
74
|
+
rubygems_version: 2.4.8
|
84
75
|
signing_key:
|
85
|
-
specification_version:
|
76
|
+
specification_version: 4
|
86
77
|
summary: A version of Ruby's Struct class that creates classes whose instances are
|
87
78
|
immutable
|
88
79
|
test_files: []
|