scash 0.1.2 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/scash.rb +2 -1
- data/lib/scash/version.rb +1 -1
- data/test/helper.rb +1 -1
- data/test/test_scash.rb +62 -30
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d252fda83f48f053eb75f9513f7e7153eeeeb1c
|
4
|
+
data.tar.gz: 66fe967c829bf8eecf540b5c002991b65ff1a1c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ab08cb3cd24b8add4ec104953cfc4708a1e39acc2618c6c5d77d7c17dfe91133478a536e22c491fb2e15e474e6c8ac3b2fdbbe7784bdbaaab8ac2f83098500a2
|
7
|
+
data.tar.gz: 38851b5a215f84ccc3c3009099e7f379322da9132c4a5695a8375cc659077c903045bedbb9580f1504739c2576f68d77e6d3b2207933e529d5f3f6862c351b30
|
data/lib/scash.rb
CHANGED
@@ -12,7 +12,7 @@ class Scash
|
|
12
12
|
|
13
13
|
def initialize(variables = nil, klass = HashWithIndifferentAccess)
|
14
14
|
@klass = klass
|
15
|
-
@stack = [
|
15
|
+
@stack = [convert(variables), global_variables].compact
|
16
16
|
build!
|
17
17
|
end
|
18
18
|
|
@@ -64,6 +64,7 @@ class Scash
|
|
64
64
|
|
65
65
|
def build_hash(index = 0)
|
66
66
|
@stack[index..-1].inject(@klass.new) do |hash, variables|
|
67
|
+
variables = variables.reject{|key, value| value.nil? && @stack.last.key?(key) }
|
67
68
|
variables.merge hash
|
68
69
|
end
|
69
70
|
end
|
data/lib/scash/version.rb
CHANGED
data/test/helper.rb
CHANGED
data/test/test_scash.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
require_relative 'helper'
|
2
2
|
|
3
3
|
describe Scash do
|
4
4
|
it "is instantiable" do
|
@@ -122,52 +122,84 @@ describe Scash do
|
|
122
122
|
assert_equal "bar1", foo1.bar
|
123
123
|
assert_equal "bar2", foo2.bar
|
124
124
|
end
|
125
|
-
end
|
126
125
|
|
127
|
-
describe "global variables" do
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
126
|
+
describe "global variables" do
|
127
|
+
it "is able to define a global variable" do
|
128
|
+
scash = Scash.new
|
129
|
+
scash.with({:a => 1}) do
|
130
|
+
scash.define_global_variables :result => 1337
|
131
|
+
assert_equal 1, scash[:a]
|
132
|
+
assert_equal 1337, scash[:result]
|
133
|
+
|
134
|
+
scash.with({:b => 2}) do
|
135
|
+
assert_equal 1, scash[:a]
|
136
|
+
assert_equal 2, scash[:b]
|
137
|
+
assert_equal 1337, scash[:result]
|
138
|
+
end
|
134
139
|
|
135
|
-
scash.with({:b => 2}) do
|
136
140
|
assert_equal 1, scash[:a]
|
137
|
-
|
141
|
+
assert_nil scash[:b]
|
138
142
|
assert_equal 1337, scash[:result]
|
139
143
|
end
|
140
144
|
|
141
|
-
|
145
|
+
assert_nil scash[:a]
|
142
146
|
assert_nil scash[:b]
|
143
147
|
assert_equal 1337, scash[:result]
|
144
148
|
end
|
145
149
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
+
it "overwrite a nil value with same name" do
|
151
|
+
# initial values can never be overwritten with a global-variable, except nil values
|
152
|
+
scash = Scash.new(:initial_a => 1337, :initial_b => nil)
|
153
|
+
scash.with({:a => 1, :b => nil}) do
|
154
|
+
assert_equal [1337, nil, 1, nil], scash.values
|
155
|
+
assert_equal 1337, scash[:initial_a]
|
156
|
+
assert_equal nil, scash[:initial_b]
|
150
157
|
|
151
|
-
|
152
|
-
|
153
|
-
scash.with({:a => 1}) do
|
154
|
-
scash.define_global_variables :result => 1337
|
155
|
-
assert_equal 1337, scash[:result]
|
158
|
+
scash.define_global_variables :a => "global a", :b => "global b"
|
159
|
+
scash.define_global_variables :initial_a => "new a", :initial_b => "new b"
|
156
160
|
|
157
|
-
|
161
|
+
assert_equal [1, "global b", 1337, "new b"], scash.values
|
162
|
+
|
163
|
+
scash.with(:a => "a", :b => nil, :c => nil) do
|
164
|
+
assert_equal ["a", "global b", 1337, "new b", nil], scash.values # NOTE: only 1 nil value (b)
|
165
|
+
assert_equal "a", scash[:a]
|
166
|
+
assert_equal "global b", scash[:b]
|
167
|
+
assert_equal nil, scash[:c]
|
168
|
+
end
|
169
|
+
|
170
|
+
assert_equal 1, scash[:a]
|
171
|
+
assert_equal "global b", scash[:b]
|
172
|
+
assert_equal 1337, scash[:initial_a]
|
173
|
+
assert_equal "new b", scash[:initial_b]
|
174
|
+
end
|
175
|
+
assert_equal "global a", scash[:a]
|
176
|
+
assert_equal "global b", scash[:b]
|
177
|
+
|
178
|
+
assert_equal 1337, scash[:initial_a]
|
179
|
+
assert_equal "new b", scash[:initial_b]
|
180
|
+
end
|
181
|
+
|
182
|
+
it "overwrites a global variable with same name" do
|
183
|
+
scash = Scash.new
|
184
|
+
scash.with({:a => 1}) do
|
185
|
+
scash.define_global_variables :result => 1337
|
158
186
|
assert_equal 1337, scash[:result]
|
159
|
-
|
160
|
-
scash.
|
161
|
-
|
162
|
-
|
187
|
+
|
188
|
+
scash.with({:b => 2}) do
|
189
|
+
assert_equal 1337, scash[:result]
|
190
|
+
assert_equal 2, scash[:b]
|
191
|
+
scash.define_global_variables :result => "foo"
|
192
|
+
scash.define_global_variables :b => "bar"
|
193
|
+
assert_equal 2, scash[:b]
|
194
|
+
assert_equal "foo", scash[:result]
|
195
|
+
end
|
196
|
+
|
163
197
|
assert_equal "foo", scash[:result]
|
164
198
|
end
|
165
199
|
|
166
200
|
assert_equal "foo", scash[:result]
|
201
|
+
assert_equal "bar", scash[:b]
|
167
202
|
end
|
168
|
-
|
169
|
-
assert_equal "foo", scash[:result]
|
170
|
-
assert_equal "bar", scash[:b]
|
171
203
|
end
|
172
204
|
|
173
205
|
it "responds to merge" do
|
@@ -176,4 +208,4 @@ describe "global variables" do
|
|
176
208
|
assert_equal({:a => 1, :b => 2}.with_indifferent_access, scash.merge(:b => 2))
|
177
209
|
end
|
178
210
|
end
|
179
|
-
end
|
211
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephan Kaag
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-02-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|