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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 870ae047c83a20f0836d92bdec2aefc3c40fba7a
4
- data.tar.gz: 1ce68c69beaa55da5a6c25328b8eaf260636700b
3
+ metadata.gz: 16a5ec50f494a359f18bd1ad634e80f0ed384578
4
+ data.tar.gz: 7844e3d7c3503fe89d2dbf75604587743257331a
5
5
  SHA512:
6
- metadata.gz: 5b56fcf53e6642e6e78c402b22f4f3df0af875ecb1a14bc83ff18ee596daf869ac810acf02dce50fe94bf9a4dd0a1c825dc4463e7fd62b890fe7f25da1cafee2
7
- data.tar.gz: ba5ac81b034a84980d9fc36972cd77c3d69be72ce2c8f6aad9e1f9381643cd8c71f908ba92e8b3ddd68e94b426087c5d7958ad11ebad9196f11094d2f0ddccf4
6
+ metadata.gz: da960e0e9ee69928180d9f3e4b8b5991644b9568c63ebd7aedea2db7c64cd1327c3e8002bea85edf0bdde8c1a905d7f245472c64a66420898f08e676abf015b3
7
+ data.tar.gz: 84c8d89840a38ba3c8924c88405d5781f53d6dbb6471e4dfe5fb72967afc868e0366897d41044f4e948b9d948eabecdf525eed79003a318178a50515dcf9120d
@@ -1,3 +1,3 @@
1
1
  class ClosedStruct
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
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
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: closed_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paweł Obrok