scash 0.2.4 → 0.3.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: 0bffa41de0ca678f84bac31ff66ebb2f48530aa0
4
- data.tar.gz: 0efbb1eb55d831f2a7c0b3cc360c4c5829b96028
3
+ metadata.gz: 1544314116a37cfa4425e4e03d8b5ef88d8ca5a9
4
+ data.tar.gz: 10457d9d63dce1ebd2fdd3fe924ee76c5bdcdade
5
5
  SHA512:
6
- metadata.gz: 0ad99565ef44a2dd563f5ffc913bea426d47f7e7745fe429062dbb6e7ed58c35b87936435800b1bac13ddc3a4819f52373b57d428a569860243422b846fcf771
7
- data.tar.gz: 58df8c537d1285e48d262a99c161b8f59c94adf784a82927a732fd3a5995c8f209509dc40d0dcedda5652304f013dd734a21869203338f9c20596cadcaf4ffbc
6
+ metadata.gz: 7089aad512341a09766a2874c5f9a800a466b634e326eb4c369bb35bcacfd68c963882c5916aeb394664d2d69d236312c7c410ec9b0be5461dca5b9e86188161
7
+ data.tar.gz: 22b5976ab4f5356bc133303b6a26ff45362dc2bcade5f17f7e361a1d637b9b2c6e32e755ecf7af849f1ac161e86242d90c540d3b150ecd4ee0724c5ee0530a8e
@@ -10,36 +10,41 @@ class Scash
10
10
 
11
11
  attr_reader :stack
12
12
 
13
- def initialize(variables = nil, klass = HashWithIndifferentAccess)
13
+ def initialize(variables = {}, klass = HashWithIndifferentAccess)
14
14
  @klass = klass
15
- @stack = [convert(variables), global_variables].compact
16
- build!
15
+ @hash = convert(variables)
16
+ @inverse_hash = convert(variables)
17
+ @global_variables = {}
17
18
  end
18
19
 
19
20
  def to_hash
20
- @hashes.first
21
+ @hash
21
22
  end
22
23
 
23
24
  def to_inverse_hash
24
- @inverse_hashes.first
25
+ @inverse_hash
25
26
  end
26
27
 
27
28
  def scope(variables)
28
- @stack.unshift convert(variables)
29
- added = true
30
- build!
29
+ previous_hash = @hash.select{|key|variables.key?(key.to_s) || variables.key?(key.to_sym)}
30
+ previous_inverse_hash = @inverse_hash.select{|key|variables.key?(key.to_s) || variables.key?(key.to_sym)}
31
+ @hash.merge! variables
32
+ @inverse_hash.merge!(variables.reject{|key| @inverse_hash.key?(key)})
31
33
  yield
32
34
  ensure
33
- @stack.shift if added
34
- build!
35
+ variables.keys.each{|key| @hash.delete(key)}
36
+ variables.keys.each{|key| @inverse_hash.delete(key)}
37
+ @hash.merge!(@global_variables)
38
+ @inverse_hash.merge!(previous_inverse_hash)
39
+ @hash.merge!(previous_hash)
35
40
  end
36
41
  alias :with :scope
37
42
 
38
43
  def [](key, inverse = false)
39
44
  if inverse
40
- to_inverse_hash[key]
45
+ @inverse_hash[key]
41
46
  else
42
- to_hash[key]
47
+ @hash[key]
43
48
  end
44
49
  end
45
50
 
@@ -48,9 +53,8 @@ class Scash
48
53
  end
49
54
 
50
55
  def define_global_variables(variables)
51
- @stack.each{|hash|variables.keys.each{|key| hash.delete(key)}}
52
- global_variables.merge! convert(variables)
53
- build!
56
+ @hash.merge! variables
57
+ @global_variables.merge! convert(variables)
54
58
  end
55
59
 
56
60
  private
@@ -59,41 +63,7 @@ class Scash
59
63
  @global_variables ||= @klass.new
60
64
  end
61
65
 
62
- def any?
63
- @stack.any?
64
- end
65
-
66
- def build_hash(stack_index = 0)
67
- @stack[stack_index..-1].each_with_index.inject(@klass.new) do |hash, (variables, index)|
68
- variables.merge hash
69
- end
70
- end
71
-
72
- def build_inverse_hash(index = 0)
73
- @stack[index..-1].inject(@klass.new) do |hash, variables|
74
- hash.merge variables
75
- end
76
- end
77
-
78
- def delete_key(key)
79
- @stack.each{|hash|hash.delete(key)}
80
- end
81
-
82
66
  def convert(variables)
83
- return if variables.nil?
84
-
85
- raise(ArgumentError, "Variables should respond to `keys`") unless variables.respond_to?("keys")
86
67
  variables.is_a?(@klass) ? variables : @klass.new(variables)
87
68
  end
88
-
89
- def build!
90
- @hashes = stack.size.times.map do |index|
91
- build_hash(index)
92
- end
93
-
94
- @inverse_hashes = stack.size.times.map do |index|
95
- build_inverse_hash(index)
96
- end
97
- end
98
-
99
69
  end
@@ -1,3 +1,3 @@
1
1
  class Scash
2
- VERSION = "0.2.4"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -6,20 +6,20 @@ describe Scash do
6
6
  end
7
7
 
8
8
  it "is scopable" do
9
- scash = Scash.new
9
+ scash = Scash.new(:i => true)
10
10
 
11
- assert_equal %w(), scash.keys
11
+ assert_equal %w(i), scash.keys
12
12
  scash.with({:a => 1}) do
13
13
  assert_equal 1, scash[:a]
14
14
  assert_equal 1, scash[:a, true]
15
15
  assert_equal 1, scash['a', true]
16
- assert_equal %w(a), scash.keys
16
+ assert_equal %w(a i), scash.keys.sort
17
17
 
18
18
  scash.with({"a" => 2}) do
19
19
  assert_equal 2, scash[:a]
20
20
  assert_equal 1, scash[:a, true]
21
21
  assert_equal 1, scash['a', true]
22
- assert_equal %w(a), scash.keys
22
+ assert_equal %w(a i), scash.keys.sort
23
23
 
24
24
  scash.with({"b" => 3}) do
25
25
  assert_equal 3, scash[:b]
@@ -32,11 +32,14 @@ describe Scash do
32
32
  assert_equal 3, scash[:b, true]
33
33
  end
34
34
  end
35
+
36
+ assert_nil scash[:b]
37
+ assert_nil scash[:b, true]
35
38
  end
36
39
 
37
40
  scash.with({"b" => 3}) do
38
41
  assert_equal 1, scash[:a]
39
- assert_equal %w(a b), scash.keys
42
+ assert_equal %w(a b i), scash.keys.sort
40
43
  assert_equal 3, scash["b"]
41
44
  assert_equal 1, scash[:a, true]
42
45
  assert_equal 1, scash['a', true]
@@ -45,14 +48,22 @@ describe Scash do
45
48
  end
46
49
 
47
50
  assert_equal 1, scash[:a]
48
- assert_equal %w(a), scash.keys
51
+ assert_equal %w(a i), scash.keys.sort
52
+ end
53
+ assert_equal %w(i), scash.keys
54
+
55
+ scash.with("a" => nil) do
56
+ assert_equal %w(i a), scash.keys
57
+ scash.with(:a => 1) do
58
+ assert_equal %w(i a), scash.keys
59
+ end
60
+ assert_equal %w(i a), scash.keys
49
61
  end
50
- assert_equal %w(), scash.keys
51
62
  end
52
63
 
53
64
  it "keeps scope instact when an error occurs" do
54
65
  scash = Scash.new
55
- assert_raises(ArgumentError) do
66
+ assert_raises(NoMethodError) do
56
67
  scash.with(["a" => 1]) do
57
68
  end
58
69
  end
@@ -162,6 +173,7 @@ describe Scash do
162
173
  assert_equal "foo", scash[:result]
163
174
  end
164
175
 
176
+ assert_equal "bar", scash[:b]
165
177
  assert_equal "foo", scash[:result]
166
178
  end
167
179
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephan Kaag