scash 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +25 -0
- data/Rakefile +9 -0
- data/lib/scash/version.rb +3 -0
- data/lib/scash.rb +53 -0
- data/scash.gemspec +23 -0
- data/test/helper.rb +24 -0
- data/test/test_scash.rb +128 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5d5852ea574973ae0911cf7b51d436df065a2207
|
4
|
+
data.tar.gz: 7bc8af7f2db1631e4707f6cc26d04a0d1a327962
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 600ea20af229037c5ae282ba6ef85142958af0252455312123cdbc5fcb6bb53c93174ea8bbf78d3d60a177d533bc0f2ce509569ef1b091cc09b2334737204981
|
7
|
+
data.tar.gz: ae8ee2e7d3513b0898ba9046dcca3a3bdc2695282bb0f26bbaf7cf3f4a8f4a795e2c6060b074f7b572bc92f900be7cc033d840f9aa90c406f41d3e609a5a110e
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0-p247
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Stephan Kaag
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Scash
|
2
|
+
|
3
|
+
A scoped hash
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'scash'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install scash
|
18
|
+
|
19
|
+
## Contributing
|
20
|
+
|
21
|
+
1. Fork it
|
22
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
23
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
24
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
25
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/scash.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require "active_support/hash_with_indifferent_access"
|
2
|
+
require "active_support/core_ext/hash/indifferent_access"
|
3
|
+
require "active_support/core_ext/module/delegation"
|
4
|
+
require "scash/version"
|
5
|
+
|
6
|
+
class Scash
|
7
|
+
include Enumerable
|
8
|
+
delegate :each, :empty?, :eql?, :has_key?, :has_value?, :include?, :index,
|
9
|
+
:keys, :merge, :reject, :replace, :select, :size, :values, :to => :to_hash
|
10
|
+
|
11
|
+
attr_reader :stack
|
12
|
+
|
13
|
+
def initialize(variables = {})
|
14
|
+
raise NotImplementedError if variables.any?
|
15
|
+
@stack = []
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_hash
|
19
|
+
@stack.inject(HashWithIndifferentAccess.new) do |hash, variables|
|
20
|
+
variables.merge hash
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_inverse_hash
|
25
|
+
@stack.inject(HashWithIndifferentAccess.new) do |hash, variables|
|
26
|
+
hash.merge variables
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def scope(variables)
|
31
|
+
@stack.unshift variables.with_indifferent_access
|
32
|
+
yield
|
33
|
+
ensure
|
34
|
+
@stack.shift
|
35
|
+
end
|
36
|
+
alias :with :scope
|
37
|
+
|
38
|
+
def [](key, inverse = false)
|
39
|
+
if inverse
|
40
|
+
to_inverse_hash[key]
|
41
|
+
else
|
42
|
+
to_hash[key]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def define_global_variable(key, value)
|
47
|
+
define_global_variables key => value
|
48
|
+
end
|
49
|
+
|
50
|
+
def define_global_variables(variables)
|
51
|
+
@stack.push variables.with_indifferent_access
|
52
|
+
end
|
53
|
+
end
|
data/scash.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'scash/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "scash"
|
8
|
+
spec.version = Scash::VERSION
|
9
|
+
spec.authors = ["Stephan Kaag"]
|
10
|
+
spec.email = ["stephan@ka.ag"]
|
11
|
+
spec.description = spec.summary = "A scoped hash"
|
12
|
+
|
13
|
+
spec.files = `git ls-files`.split($/)
|
14
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
15
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
16
|
+
spec.require_paths = ["lib"]
|
17
|
+
|
18
|
+
spec.add_dependency "activesupport", ">= 3"
|
19
|
+
spec.add_development_dependency "minitest", ">= 5"
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "coveralls"
|
23
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
$TESTING = true
|
2
|
+
require 'coveralls'
|
3
|
+
|
4
|
+
Coveralls.wear! do
|
5
|
+
add_filter "/test/"
|
6
|
+
end
|
7
|
+
|
8
|
+
ENV['RACK_ENV'] = ENV['RAILS_ENV'] = 'test'
|
9
|
+
if ENV.has_key?("SIMPLECOV")
|
10
|
+
require 'simplecov'
|
11
|
+
SimpleCov.start do
|
12
|
+
add_filter "/test/"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
begin
|
17
|
+
require 'pry'
|
18
|
+
rescue LoadError
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'scash'
|
22
|
+
|
23
|
+
require 'minitest/autorun'
|
24
|
+
require 'minitest/pride'
|
data/test/test_scash.rb
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestScash < Minitest::Test
|
4
|
+
describe "basics" do
|
5
|
+
it "should be instantiable" do
|
6
|
+
assert scash = Scash.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should be scopable" do
|
10
|
+
scash = Scash.new
|
11
|
+
|
12
|
+
assert_equal %w(), scash.keys
|
13
|
+
scash.with({:a => 1}) do
|
14
|
+
assert_equal 1, scash[:a]
|
15
|
+
assert_equal 1, scash[:a, true]
|
16
|
+
assert_equal 1, scash['a', true]
|
17
|
+
assert_equal %w(a), scash.keys
|
18
|
+
|
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
|
+
|
25
|
+
scash.with({"b" => 3}) do
|
26
|
+
assert_equal 3, scash[:b]
|
27
|
+
assert_equal 3, scash[:b, true]
|
28
|
+
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
|
+
end
|
36
|
+
end
|
37
|
+
|
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
|
+
|
48
|
+
assert_equal 1, scash[:a]
|
49
|
+
assert_equal %w(a), scash.keys
|
50
|
+
end
|
51
|
+
assert_equal %w(), scash.keys
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should keep scope instact when an error occurs" do
|
55
|
+
scash = Scash.new
|
56
|
+
assert_raises(NoMethodError) do
|
57
|
+
scash.with(["a" => 1]) do
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
assert_equal({}, scash.to_hash)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should reuse instances" do
|
65
|
+
class Foo
|
66
|
+
attr_accessor :bar
|
67
|
+
end
|
68
|
+
|
69
|
+
foo1 = Foo.new
|
70
|
+
foo2 = Foo.new
|
71
|
+
foo1id = foo1.object_id
|
72
|
+
foo2id = foo2.object_id
|
73
|
+
|
74
|
+
scash = Scash.new
|
75
|
+
scash.with({"a" => foo1}) do
|
76
|
+
assert_equal foo1id, scash["a"].object_id
|
77
|
+
scash["a"].bar = "bar1"
|
78
|
+
scash.with({"b" => foo2}) do
|
79
|
+
assert_equal "bar1", scash["a"].bar
|
80
|
+
assert_equal foo1id, scash["a"].object_id
|
81
|
+
assert_equal foo2id, scash["b"].object_id
|
82
|
+
scash["b"].bar = "bar2"
|
83
|
+
end
|
84
|
+
assert_equal foo1id, scash["a"].object_id
|
85
|
+
assert_equal "bar1", scash["a"].bar
|
86
|
+
assert_equal "bar2", foo2.bar
|
87
|
+
end
|
88
|
+
|
89
|
+
assert_equal "bar1", foo1.bar
|
90
|
+
assert_equal "bar2", foo2.bar
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "global variables" do
|
95
|
+
it "should be able to define a global variable" do
|
96
|
+
scash = Scash.new
|
97
|
+
scash.with({:a => 1}) do
|
98
|
+
scash.define_global_variables :result => 1337
|
99
|
+
assert_equal 1, scash[:a]
|
100
|
+
assert_equal 1337, scash[:result]
|
101
|
+
|
102
|
+
scash.with({:b => 2}) do
|
103
|
+
assert_equal 1, scash[:a]
|
104
|
+
assert_equal 2, scash[:b]
|
105
|
+
assert_equal 1337, scash[:result]
|
106
|
+
end
|
107
|
+
|
108
|
+
assert_equal 1, scash[:a]
|
109
|
+
assert_nil scash[:b]
|
110
|
+
assert_equal 1337, scash[:result]
|
111
|
+
end
|
112
|
+
|
113
|
+
assert_nil scash[:a]
|
114
|
+
assert_nil scash[:b]
|
115
|
+
assert_equal 1337, scash[:result]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe "behave like a hash" do
|
120
|
+
it "should respond to merge" do
|
121
|
+
scash = Scash.new
|
122
|
+
scash.with({:a => 1}) do
|
123
|
+
assert_equal({:a => 1, :b => 2}.with_indifferent_access, scash.merge(:b => 2))
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: scash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stephan Kaag
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: coveralls
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: A scoped hash
|
84
|
+
email:
|
85
|
+
- stephan@ka.ag
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- .ruby-version
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- lib/scash.rb
|
97
|
+
- lib/scash/version.rb
|
98
|
+
- scash.gemspec
|
99
|
+
- test/helper.rb
|
100
|
+
- test/test_scash.rb
|
101
|
+
homepage:
|
102
|
+
licenses: []
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.0.3
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: A scoped hash
|
124
|
+
test_files:
|
125
|
+
- test/helper.rb
|
126
|
+
- test/test_scash.rb
|