closed_struct 0.0.2 → 0.1.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/lib/closed_struct/version.rb +1 -1
- data/lib/closed_struct.rb +5 -2
- data/spec/closed_struct_spec.rb +23 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 16a5ec50f494a359f18bd1ad634e80f0ed384578
|
4
|
+
data.tar.gz: 7844e3d7c3503fe89d2dbf75604587743257331a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da960e0e9ee69928180d9f3e4b8b5991644b9568c63ebd7aedea2db7c64cd1327c3e8002bea85edf0bdde8c1a905d7f245472c64a66420898f08e676abf015b3
|
7
|
+
data.tar.gz: 84c8d89840a38ba3c8924c88405d5781f53d6dbb6471e4dfe5fb72967afc868e0366897d41044f4e948b9d948eabecdf525eed79003a318178a50515dcf9120d
|
data/lib/closed_struct.rb
CHANGED
@@ -1,13 +1,16 @@
|
|
1
1
|
require "closed_struct/version"
|
2
2
|
|
3
3
|
class ClosedStruct
|
4
|
-
def initialize(contents)
|
5
|
-
@contents = contents
|
4
|
+
def initialize(contents, &block)
|
5
|
+
@contents = contents.dup
|
6
6
|
|
7
7
|
singleton_class = (class << self; self; end)
|
8
8
|
@contents.each do |key, value|
|
9
|
+
raise ArgumentError.new("Cannot define #{key} as it already exists") if respond_to?(key)
|
9
10
|
singleton_class.send(:define_method, key) { value }
|
10
11
|
end
|
12
|
+
|
13
|
+
singleton_class.class_eval(&block) if block_given?
|
11
14
|
end
|
12
15
|
|
13
16
|
def to_h
|
data/spec/closed_struct_spec.rb
CHANGED
@@ -28,4 +28,27 @@ describe ClosedStruct do
|
|
28
28
|
expect(first.eql?(second)).to be_false
|
29
29
|
expect(first.hash).not_to eq(second.hash)
|
30
30
|
end
|
31
|
+
|
32
|
+
it "allows for definitions of additional methods" do
|
33
|
+
object = ClosedStruct.new(:a => :b) do
|
34
|
+
def something
|
35
|
+
:value
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
expect(object.something).to eq(:value)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "is indifferent to the input getting mutated" do
|
43
|
+
input = {:a => :b}
|
44
|
+
object = ClosedStruct.new(input)
|
45
|
+
|
46
|
+
expect { input[:a] = [:d] }.not_to change { object.to_h }
|
47
|
+
expect { input[:a] = [:c] }.not_to change { object.hash }
|
48
|
+
end
|
49
|
+
|
50
|
+
it "does not allow duplicate definitions" do
|
51
|
+
expect { ClosedStruct.new(:a => :b, "a" => :b) }.
|
52
|
+
to raise_error(ArgumentError)
|
53
|
+
end
|
31
54
|
end
|