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 +4 -4
- data/lib/scash.rb +21 -13
- data/lib/scash/version.rb +1 -1
- data/test/test_scash.rb +122 -127
- metadata +16 -17
- data/.ruby-version +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f18dc252c49e5b61bf36756f3094ea165a24143a
|
4
|
+
data.tar.gz: 92cb2148378adfb9a5eb3346c474f8c74feb0f4d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 =
|
16
|
-
|
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
|
-
|
20
|
+
@hashes.first
|
22
21
|
end
|
23
22
|
|
24
23
|
def to_inverse_hash
|
25
|
-
|
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
|
-
|
32
|
-
@inverse_hashes.unshift build_inverse_hash
|
30
|
+
build!
|
33
31
|
yield
|
34
32
|
ensure
|
35
|
-
@stack.shift
|
36
|
-
|
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
data/test/test_scash.rb
CHANGED
@@ -1,184 +1,179 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
3
|
+
describe Scash do
|
4
|
+
it "is instantiable" do
|
5
|
+
assert Scash.new
|
6
|
+
end
|
8
7
|
|
9
|
-
|
10
|
-
|
8
|
+
it "is scopable" do
|
9
|
+
scash = Scash.new
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
|
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({"
|
20
|
-
assert_equal
|
21
|
-
assert_equal
|
22
|
-
assert_equal
|
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" =>
|
26
|
-
assert_equal
|
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
|
-
|
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
|
-
|
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
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
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
|
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
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
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
|
-
|
83
|
-
|
84
|
-
|
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
|
-
|
87
|
-
|
85
|
+
class SubHash < HashWithIndifferentAccess
|
86
|
+
end
|
88
87
|
|
89
|
-
|
90
|
-
|
91
|
-
|
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
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
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
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
97
|
+
it "reuses instances" do
|
98
|
+
class Foo
|
99
|
+
attr_accessor :bar
|
100
|
+
end
|
102
101
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
102
|
+
foo1 = Foo.new
|
103
|
+
foo2 = Foo.new
|
104
|
+
foo1id = foo1.object_id
|
105
|
+
foo2id = foo2.object_id
|
107
106
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
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 "
|
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",
|
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
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
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
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
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
|
-
|
137
|
+
assert_equal 2, scash[:b]
|
144
138
|
assert_equal 1337, scash[:result]
|
145
139
|
end
|
146
140
|
|
147
|
-
|
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
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
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
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
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
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
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.
|
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:
|
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
|
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
|