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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f18dc252c49e5b61bf36756f3094ea165a24143a
4
- data.tar.gz: 92cb2148378adfb9a5eb3346c474f8c74feb0f4d
3
+ metadata.gz: 7d252fda83f48f053eb75f9513f7e7153eeeeb1c
4
+ data.tar.gz: 66fe967c829bf8eecf540b5c002991b65ff1a1c2
5
5
  SHA512:
6
- metadata.gz: c39f698028d8efc8151e5d885c09a007d1680bcb9bdd40b8f604ec427e9b713643a2568d79727dd86be8fcd20c1f4bfbe2b97c004ca76a44f4e9dca56c47c071
7
- data.tar.gz: 61f207aa4f27ea24a197211ed38ec26759338ba0604cd2f70e582ee01db129017a55b5b42081fe9adf22f5ce4b2aefb16bee808cbd065a23529c4bc2eb056833
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 = [global_variables, convert(variables)].compact
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
@@ -1,3 +1,3 @@
1
1
  class Scash
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.1"
3
3
  end
data/test/helper.rb CHANGED
@@ -18,7 +18,7 @@ begin
18
18
  rescue LoadError
19
19
  end
20
20
 
21
- require 'scash'
21
+ require_relative "../lib/scash"
22
22
 
23
23
  require 'minitest/autorun'
24
24
  require 'minitest/pride'
data/test/test_scash.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'helper'
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
- 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]
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
- assert_equal 2, scash[:b]
141
+ assert_nil scash[:b]
138
142
  assert_equal 1337, scash[:result]
139
143
  end
140
144
 
141
- assert_equal 1, scash[:a]
145
+ assert_nil scash[:a]
142
146
  assert_nil scash[:b]
143
147
  assert_equal 1337, scash[:result]
144
148
  end
145
149
 
146
- assert_nil scash[:a]
147
- assert_nil scash[:b]
148
- assert_equal 1337, scash[:result]
149
- end
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
- 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]
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
- scash.with({:b => 2}) do
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
- 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]
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.2
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-01-27 00:00:00.000000000 Z
11
+ date: 2014-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport