scash 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4d4027b8c16c80e55adc1684995a68ae3845e37b
4
- data.tar.gz: dba5b4f126fe90764752c77fe622460c8c06d93e
3
+ metadata.gz: f18dc252c49e5b61bf36756f3094ea165a24143a
4
+ data.tar.gz: 92cb2148378adfb9a5eb3346c474f8c74feb0f4d
5
5
  SHA512:
6
- metadata.gz: 53430248c797466b5e5de7d09a44c84c7b92ce231560b9d332658e423cf30d8d1424dac3f18c30b3ace8459fbbef48721b0171aef488ba7e4335fca721458d8a
7
- data.tar.gz: 8d47354d191d0ef4d67d7f403b64e41a2041d21c019d05a54af95dd3da1033d02e50c1281be16fa303234b84620962da573f6a0bd50579e0f28f60286f3bbe3a
6
+ metadata.gz: c39f698028d8efc8151e5d885c09a007d1680bcb9bdd40b8f604ec427e9b713643a2568d79727dd86be8fcd20c1f4bfbe2b97c004ca76a44f4e9dca56c47c071
7
+ data.tar.gz: 61f207aa4f27ea24a197211ed38ec26759338ba0604cd2f70e582ee01db129017a55b5b42081fe9adf22f5ce4b2aefb16bee808cbd065a23529c4bc2eb056833
data/lib/scash.rb CHANGED
@@ -12,31 +12,26 @@ class Scash
12
12
 
13
13
  def initialize(variables = nil, klass = HashWithIndifferentAccess)
14
14
  @klass = klass
15
- @stack = variables ? [convert(variables)] : []
16
- @hashes = variables ? [build_hash] : []
17
- @inverse_hashes = variables ? [build_inverse_hash] : []
15
+ @stack = [global_variables, convert(variables)].compact
16
+ build!
18
17
  end
19
18
 
20
19
  def to_hash
21
- any? ? @hashes.first.merge(global_variables) : global_variables
20
+ @hashes.first
22
21
  end
23
22
 
24
23
  def to_inverse_hash
25
- any? ? @inverse_hashes.first.merge(global_variables) : global_variables
24
+ @inverse_hashes.first
26
25
  end
27
26
 
28
27
  def scope(variables)
29
28
  @stack.unshift convert(variables)
30
29
  added = true
31
- @hashes.unshift build_hash
32
- @inverse_hashes.unshift build_inverse_hash
30
+ build!
33
31
  yield
34
32
  ensure
35
- @stack.shift
36
- if added
37
- @hashes.shift
38
- @inverse_hashes.shift
39
- end
33
+ @stack.shift if added
34
+ build!
40
35
  end
41
36
  alias :with :scope
42
37
 
@@ -54,6 +49,7 @@ class Scash
54
49
 
55
50
  def define_global_variables(variables)
56
51
  global_variables.merge! convert(variables)
52
+ build!
57
53
  end
58
54
 
59
55
  private
@@ -83,8 +79,20 @@ class Scash
83
79
  end
84
80
 
85
81
  def convert(variables)
82
+ return if variables.nil?
83
+
86
84
  raise(ArgumentError, "Variables should respond to `keys`") unless variables.respond_to?("keys")
87
- @klass.new(variables)
85
+ variables.is_a?(@klass) ? variables : @klass.new(variables)
86
+ end
87
+
88
+ def build!
89
+ @hashes = stack.size.times.map do |index|
90
+ build_hash(index)
91
+ end
92
+
93
+ @inverse_hashes = stack.size.times.map do |index|
94
+ build_inverse_hash(index)
95
+ end
88
96
  end
89
97
 
90
98
  end
data/lib/scash/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Scash
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/test/test_scash.rb CHANGED
@@ -1,184 +1,179 @@
1
1
  require 'helper'
2
2
 
3
- class TestScash < Minitest::Test
4
- describe "basics" do
5
- it "should be instantiable" do
6
- assert scash = Scash.new
7
- end
3
+ describe Scash do
4
+ it "is instantiable" do
5
+ assert Scash.new
6
+ end
8
7
 
9
- it "should be scopable" do
10
- scash = Scash.new
8
+ it "is scopable" do
9
+ scash = Scash.new
11
10
 
12
- assert_equal %w(), scash.keys
13
- scash.with({:a => 1}) do
14
- assert_equal 1, scash[:a]
11
+ assert_equal %w(), scash.keys
12
+ scash.with({:a => 1}) do
13
+ assert_equal 1, scash[:a]
14
+ assert_equal 1, scash[:a, true]
15
+ assert_equal 1, scash['a', true]
16
+ assert_equal %w(a), scash.keys
17
+
18
+ scash.with({"a" => 2}) do
19
+ assert_equal 2, scash[:a]
15
20
  assert_equal 1, scash[:a, true]
16
21
  assert_equal 1, scash['a', true]
17
22
  assert_equal %w(a), scash.keys
18
23
 
19
- scash.with({"a" => 2}) do
20
- assert_equal 2, scash[:a]
21
- assert_equal 1, scash[:a, true]
22
- assert_equal 1, scash['a', true]
23
- assert_equal %w(a), scash.keys
24
+ scash.with({"b" => 3}) do
25
+ assert_equal 3, scash[:b]
26
+ assert_equal 3, scash[:b, true]
27
+ assert_equal 3, scash[:b, true]
24
28
 
25
- scash.with({"b" => 3}) do
26
- assert_equal 3, scash[:b]
29
+ scash.with({"b" => 4}) do
30
+ assert_equal 4, scash[:b]
27
31
  assert_equal 3, scash[:b, true]
28
32
  assert_equal 3, scash[:b, true]
29
-
30
- scash.with({"b" => 4}) do
31
- assert_equal 4, scash[:b]
32
- assert_equal 3, scash[:b, true]
33
- assert_equal 3, scash[:b, true]
34
- end
35
33
  end
36
34
  end
35
+ end
37
36
 
38
- scash.with({"b" => 3}) do
39
- assert_equal 1, scash[:a]
40
- assert_equal %w(a b), scash.keys
41
- assert_equal 3, scash["b"]
42
- assert_equal 1, scash[:a, true]
43
- assert_equal 1, scash['a', true]
44
- assert_equal 3, scash[:b, true]
45
- assert_equal 3, scash['b', true]
46
- end
47
-
37
+ scash.with({"b" => 3}) do
48
38
  assert_equal 1, scash[:a]
49
- assert_equal %w(a), scash.keys
39
+ assert_equal %w(a b), scash.keys
40
+ assert_equal 3, scash["b"]
41
+ assert_equal 1, scash[:a, true]
42
+ assert_equal 1, scash['a', true]
43
+ assert_equal 3, scash[:b, true]
44
+ assert_equal 3, scash['b', true]
50
45
  end
51
- assert_equal %w(), scash.keys
46
+
47
+ assert_equal 1, scash[:a]
48
+ assert_equal %w(a), scash.keys
52
49
  end
50
+ assert_equal %w(), scash.keys
51
+ end
53
52
 
54
- it "should keep scope instact when an error occurs" do
55
- scash = Scash.new
56
- assert_raises(ArgumentError) do
57
- scash.with(["a" => 1]) do
58
- end
53
+ it "keeps scope instact when an error occurs" do
54
+ scash = Scash.new
55
+ assert_raises(ArgumentError) do
56
+ scash.with(["a" => 1]) do
59
57
  end
60
-
61
- assert_equal({}, scash.to_hash)
62
58
  end
63
59
 
64
- it "should accept variables in initializer" do
65
- scash = Scash.new({:a => 1})
66
- assert_equal 1, scash[:a]
67
- assert_equal({"a" => 1}, scash.to_hash)
68
- scash.with({:b => 2}) do
69
- assert_equal 1, scash[:a]
70
- assert_equal 2, scash[:b]
71
- scash.define_global_variables :c => 3
72
- end
60
+ assert_equal({}, scash.to_hash)
61
+ end
62
+
63
+ it "accepts variables in initializer" do
64
+ scash = Scash.new({:a => 1})
65
+ assert_equal 1, scash[:a]
66
+ assert_equal({"a" => 1}, scash.to_hash)
67
+ scash.with({:b => 2}) do
73
68
  assert_equal 1, scash[:a]
74
- assert_equal 3, scash[:c]
69
+ assert_equal 2, scash[:b]
70
+ scash.define_global_variables :c => 3
75
71
  end
72
+ assert_equal 1, scash[:a]
73
+ assert_equal 3, scash[:c]
74
+ end
76
75
 
77
- it "should typecast correctly" do
78
- scash = Scash.new({:a => 1})
79
- assert_equal({"a" => 1}, scash.to_hash)
80
- assert_equal HashWithIndifferentAccess, scash.to_hash.class
76
+ it "typecasts correctly" do
77
+ scash = Scash.new({:a => 1})
78
+ assert_equal({"a" => 1}, scash.to_hash)
79
+ assert_equal HashWithIndifferentAccess, scash.to_hash.class
81
80
 
82
- scash = Scash.new({:a => 1})
83
- assert_equal({"a" => 1}, scash.to_inverse_hash)
84
- assert_equal HashWithIndifferentAccess, scash.to_inverse_hash.class
81
+ scash = Scash.new({:a => 1})
82
+ assert_equal({"a" => 1}, scash.to_inverse_hash)
83
+ assert_equal HashWithIndifferentAccess, scash.to_inverse_hash.class
85
84
 
86
- class SubHash < HashWithIndifferentAccess
87
- end
85
+ class SubHash < HashWithIndifferentAccess
86
+ end
88
87
 
89
- scash = Scash.new({:a => 1}, SubHash)
90
- assert_equal({"a" => 1}, scash.to_hash)
91
- assert_equal SubHash, scash.to_hash.class
88
+ scash = Scash.new({:a => 1}, SubHash)
89
+ assert_equal({"a" => 1}, scash.to_hash)
90
+ assert_equal SubHash, scash.to_hash.class
92
91
 
93
- scash = Scash.new({:a => 1}, SubHash)
94
- assert_equal({"a" => 1}, scash.to_inverse_hash)
95
- assert_equal SubHash, scash.to_inverse_hash.class
96
- end
92
+ scash = Scash.new({:a => 1}, SubHash)
93
+ assert_equal({"a" => 1}, scash.to_inverse_hash)
94
+ assert_equal SubHash, scash.to_inverse_hash.class
95
+ end
97
96
 
98
- it "should reuse instances" do
99
- class Foo
100
- attr_accessor :bar
101
- end
97
+ it "reuses instances" do
98
+ class Foo
99
+ attr_accessor :bar
100
+ end
102
101
 
103
- foo1 = Foo.new
104
- foo2 = Foo.new
105
- foo1id = foo1.object_id
106
- foo2id = foo2.object_id
102
+ foo1 = Foo.new
103
+ foo2 = Foo.new
104
+ foo1id = foo1.object_id
105
+ foo2id = foo2.object_id
107
106
 
108
- scash = Scash.new
109
- scash.with({"a" => foo1}) do
110
- assert_equal foo1id, scash["a"].object_id
111
- scash["a"].bar = "bar1"
112
- scash.with({"b" => foo2}) do
113
- assert_equal "bar1", scash["a"].bar
114
- assert_equal foo1id, scash["a"].object_id
115
- assert_equal foo2id, scash["b"].object_id
116
- scash["b"].bar = "bar2"
117
- end
118
- assert_equal foo1id, scash["a"].object_id
107
+ scash = Scash.new
108
+ scash.with({"a" => foo1}) do
109
+ assert_equal foo1id, scash["a"].object_id
110
+ scash["a"].bar = "bar1"
111
+ scash.with({"b" => foo2}) do
119
112
  assert_equal "bar1", scash["a"].bar
120
- assert_equal "bar2", foo2.bar
113
+ assert_equal foo1id, scash["a"].object_id
114
+ assert_equal foo2id, scash["b"].object_id
115
+ scash["b"].bar = "bar2"
121
116
  end
122
-
123
- assert_equal "bar1", foo1.bar
117
+ assert_equal foo1id, scash["a"].object_id
118
+ assert_equal "bar1", scash["a"].bar
124
119
  assert_equal "bar2", foo2.bar
125
120
  end
126
- end
127
121
 
128
- describe "global variables" do
129
- it "should be able to define a global variable" do
130
- scash = Scash.new
131
- scash.with({:a => 1}) do
132
- scash.define_global_variables :result => 1337
133
- assert_equal 1, scash[:a]
134
- assert_equal 1337, scash[:result]
122
+ assert_equal "bar1", foo1.bar
123
+ assert_equal "bar2", foo2.bar
124
+ end
125
+ end
135
126
 
136
- scash.with({:b => 2}) do
137
- assert_equal 1, scash[:a]
138
- assert_equal 2, scash[:b]
139
- assert_equal 1337, scash[:result]
140
- end
127
+ describe "global variables" do
128
+ it "is able to define a global variable" do
129
+ scash = Scash.new
130
+ scash.with({:a => 1}) do
131
+ scash.define_global_variables :result => 1337
132
+ assert_equal 1, scash[:a]
133
+ assert_equal 1337, scash[:result]
141
134
 
135
+ scash.with({:b => 2}) do
142
136
  assert_equal 1, scash[:a]
143
- assert_nil scash[:b]
137
+ assert_equal 2, scash[:b]
144
138
  assert_equal 1337, scash[:result]
145
139
  end
146
140
 
147
- assert_nil scash[:a]
141
+ assert_equal 1, scash[:a]
148
142
  assert_nil scash[:b]
149
143
  assert_equal 1337, scash[:result]
150
144
  end
151
145
 
152
- it "should overwrite a global variable with same name" do
153
- scash = Scash.new
154
- scash.with({:a => 1}) do
155
- scash.define_global_variables :result => 1337
156
- assert_equal 1337, scash[:result]
146
+ assert_nil scash[:a]
147
+ assert_nil scash[:b]
148
+ assert_equal 1337, scash[:result]
149
+ end
157
150
 
158
- scash.with({:b => 2}) do
159
- assert_equal 1337, scash[:result]
160
- assert_equal 2, scash[:b]
161
- scash.define_global_variables :result => "foo"
162
- scash.define_global_variables :b => "bar"
163
- assert_equal "bar", scash[:b]
164
- assert_equal "foo", scash[:result]
165
- end
151
+ it "overwrites a global variable with same name" do
152
+ scash = Scash.new
153
+ scash.with({:a => 1}) do
154
+ scash.define_global_variables :result => 1337
155
+ assert_equal 1337, scash[:result]
166
156
 
157
+ scash.with({:b => 2}) do
158
+ assert_equal 1337, scash[:result]
159
+ assert_equal 2, scash[:b]
160
+ scash.define_global_variables :result => "foo"
161
+ scash.define_global_variables :b => "bar"
162
+ assert_equal 2, scash[:b]
167
163
  assert_equal "foo", scash[:result]
168
164
  end
169
165
 
170
166
  assert_equal "foo", scash[:result]
171
- assert_equal "bar", scash[:b]
172
167
  end
168
+
169
+ assert_equal "foo", scash[:result]
170
+ assert_equal "bar", scash[:b]
173
171
  end
174
172
 
175
- describe "behave like a hash" do
176
- it "should respond to merge" do
177
- scash = Scash.new
178
- scash.with({:a => 1}) do
179
- assert_equal({:a => 1, :b => 2}.with_indifferent_access, scash.merge(:b => 2))
180
- end
173
+ it "responds to merge" do
174
+ scash = Scash.new
175
+ scash.with({:a => 1}) do
176
+ assert_equal({:a => 1, :b => 2}.with_indifferent_access, scash.merge(:b => 2))
181
177
  end
182
178
  end
183
-
184
- end
179
+ end
metadata CHANGED
@@ -1,83 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephan Kaag
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-13 00:00:00.000000000 Z
11
+ date: 2014-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '5'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '5'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1.3'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.3'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: coveralls
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  description: A scoped hash
@@ -87,8 +87,7 @@ executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
- - .gitignore
91
- - .ruby-version
90
+ - ".gitignore"
92
91
  - Gemfile
93
92
  - LICENSE.txt
94
93
  - README.md
@@ -107,17 +106,17 @@ require_paths:
107
106
  - lib
108
107
  required_ruby_version: !ruby/object:Gem::Requirement
109
108
  requirements:
110
- - - '>='
109
+ - - ">="
111
110
  - !ruby/object:Gem::Version
112
111
  version: '0'
113
112
  required_rubygems_version: !ruby/object:Gem::Requirement
114
113
  requirements:
115
- - - '>='
114
+ - - ">="
116
115
  - !ruby/object:Gem::Version
117
116
  version: '0'
118
117
  requirements: []
119
118
  rubyforge_project:
120
- rubygems_version: 2.0.3
119
+ rubygems_version: 2.2.0
121
120
  signing_key:
122
121
  specification_version: 4
123
122
  summary: A scoped hash
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 2.0.0-p247