contextuality 1.0.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.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/Gemfile +22 -0
- data/Guardfile +24 -0
- data/LICENSE.txt +22 -0
- data/README.md +112 -0
- data/Rakefile +1 -0
- data/contextuality.gemspec +19 -0
- data/lib/contextuality.rb +37 -0
- data/lib/contextuality/context.rb +34 -0
- data/lib/contextuality/version.rb +3 -0
- data/spec/contextuality/context_spec.rb +100 -0
- data/spec/contextuality_spec.rb +80 -0
- data/spec/spec_helper.rb +13 -0
- metadata +63 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.3@contextuality --create
|
data/Gemfile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
def darwin_only(require_as)
|
2
|
+
RUBY_PLATFORM.include?('darwin') && require_as
|
3
|
+
end
|
4
|
+
|
5
|
+
def linux_only(require_as)
|
6
|
+
RUBY_PLATFORM.include?('linux') && require_as
|
7
|
+
end
|
8
|
+
|
9
|
+
source 'https://rubygems.org'
|
10
|
+
|
11
|
+
# Specify your gem's dependencies in cms_engine.gemspec
|
12
|
+
gemspec
|
13
|
+
|
14
|
+
gem 'rspec'
|
15
|
+
gem 'guard'
|
16
|
+
gem 'guard-rspec'
|
17
|
+
|
18
|
+
group :test do
|
19
|
+
gem 'rb-fsevent', require: darwin_only('rb-inotify')
|
20
|
+
gem 'rb-inotify', require: linux_only('rb-inotify')
|
21
|
+
gem 'libnotify'
|
22
|
+
end
|
data/Guardfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'rspec' do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
|
9
|
+
# Rails example
|
10
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
11
|
+
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
12
|
+
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
|
13
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
14
|
+
watch('config/routes.rb') { "spec/routing" }
|
15
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
16
|
+
|
17
|
+
# Capybara features specs
|
18
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
|
19
|
+
|
20
|
+
# Turnip features and steps
|
21
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
22
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
|
23
|
+
end
|
24
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 pyromaniac
|
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,112 @@
|
|
1
|
+
# Contextuality
|
2
|
+
|
3
|
+
Contextuality allows you to make global variables for individual contexts.
|
4
|
+
It is threadsafe and can forward contexts inside `Thread.new {}` blocks.
|
5
|
+
Context is just variables name-value hash.
|
6
|
+
You can access any context variable inside context.
|
7
|
+
|
8
|
+
It is better than global variables or singleton because variables could be set
|
9
|
+
only higher in call stack and you know it. No more unexpected values.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'contextuality'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install contextuality
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### Simple usage
|
28
|
+
|
29
|
+
```
|
30
|
+
class Foo
|
31
|
+
include Contextuality
|
32
|
+
|
33
|
+
def self.foo
|
34
|
+
contextuality.foo
|
35
|
+
end
|
36
|
+
|
37
|
+
def foo
|
38
|
+
contextuality.foo
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
contextualize(foo: 'Hello') do
|
43
|
+
Foo.foo #=> "Hello"
|
44
|
+
end #=> "Hello"
|
45
|
+
|
46
|
+
contextualize(foo: 'Goodbye') do
|
47
|
+
Foo.new.foo #=> "Goodbye"
|
48
|
+
end #=> "Goodbye"
|
49
|
+
|
50
|
+
# If context variable is not set - it returns nil
|
51
|
+
contextualize(bar: 'Hello') do
|
52
|
+
Foo.new.foo #=> nil
|
53
|
+
end #=> nil
|
54
|
+
|
55
|
+
# Contexts can be stacked along the callstack
|
56
|
+
contextualize(foo: 'Hello') do
|
57
|
+
contextualize(bar: 'Goodbye') do
|
58
|
+
"#{Foo.new.foo} #{Foo.contextuality.bar}" #=> "Hello Goodbye"
|
59
|
+
end #=> "Hello Goodbye"
|
60
|
+
end #=> "Hello Goodbye"
|
61
|
+
|
62
|
+
# Also context variables can be redefined deeper
|
63
|
+
contextualize(foo: 'Hello') do
|
64
|
+
contextualize(foo: 'Goodbye') do
|
65
|
+
Foo.new.foo #=> "Goodbye"
|
66
|
+
end #=> "Goodbye"
|
67
|
+
Foo.new.foo #=> "Hello"
|
68
|
+
end #=> "Hello"
|
69
|
+
```
|
70
|
+
|
71
|
+
You can include `Contextuality` in Object and get access to context everywhere,
|
72
|
+
or just include in classes on-demand.
|
73
|
+
|
74
|
+
### More complex example
|
75
|
+
|
76
|
+
```
|
77
|
+
class Article < ActiveRecord::Base
|
78
|
+
include Contextuality
|
79
|
+
|
80
|
+
def self.for_current_host
|
81
|
+
if contextuality.host
|
82
|
+
where(host: contextuality.host)
|
83
|
+
else
|
84
|
+
for_default_host # or just .all
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
class ArticlesController < ActionController::Base
|
90
|
+
around_filter :setup_host
|
91
|
+
|
92
|
+
def index
|
93
|
+
@articles = Article.for_current_host
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
def setup_host
|
99
|
+
contextualize(host: host_for(request.host)) do
|
100
|
+
yield
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
106
|
+
## Contributing
|
107
|
+
|
108
|
+
1. Fork it
|
109
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
110
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
111
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
112
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'contextuality/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "contextuality"
|
8
|
+
gem.version = Contextuality::VERSION
|
9
|
+
gem.authors = ["pyromaniac"]
|
10
|
+
gem.email = ["kinwizard@gmail.com"]
|
11
|
+
gem.description = %q{Contextual global variables}
|
12
|
+
gem.summary = %q{Contextual global variables}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "contextuality/version"
|
2
|
+
require "contextuality/context"
|
3
|
+
|
4
|
+
module Contextuality
|
5
|
+
def self.included klass
|
6
|
+
klass.class_eval do
|
7
|
+
extend ContextualityMethods
|
8
|
+
include ContextualityMethods
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module ContextualityMethods
|
13
|
+
def contextuality
|
14
|
+
::Thread.current.contextuality
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module ObjectMethods
|
19
|
+
def contextualize variables = {}, &block
|
20
|
+
::Thread.current.contextuality.push variables
|
21
|
+
result = block.call
|
22
|
+
::Thread.current.contextuality.pop
|
23
|
+
result
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module ThreadMethods
|
28
|
+
def contextuality
|
29
|
+
self[:contextuality] ||= (Thread.main != self) ?
|
30
|
+
Thread.main.contextuality.dup :
|
31
|
+
Contextuality::Context.new
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Object.send :include, Contextuality::ObjectMethods
|
37
|
+
Thread.send :include, Contextuality::ThreadMethods
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Contextuality
|
2
|
+
class Context
|
3
|
+
def initialize
|
4
|
+
@scopes = []
|
5
|
+
end
|
6
|
+
|
7
|
+
def push variables
|
8
|
+
@scopes.unshift Hash[variables.map { |(name, variable)| [name.to_s, variable] }]
|
9
|
+
end
|
10
|
+
|
11
|
+
def pop
|
12
|
+
@scopes.shift
|
13
|
+
end
|
14
|
+
|
15
|
+
def [] name
|
16
|
+
name = name.to_s
|
17
|
+
scope = @scopes.detect { |scope| scope.key? name }
|
18
|
+
scope[name] if scope
|
19
|
+
end
|
20
|
+
|
21
|
+
def key? name
|
22
|
+
name = name.to_s
|
23
|
+
@scopes.any? { |scope| scope.key? name }
|
24
|
+
end
|
25
|
+
|
26
|
+
def empty?
|
27
|
+
!@scopes.any? { |scope| !scope.empty? }
|
28
|
+
end
|
29
|
+
|
30
|
+
def method_missing method, *args, &block
|
31
|
+
self[method]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Contextuality::Context do
|
4
|
+
subject { described_class.new }
|
5
|
+
|
6
|
+
describe '#initialize' do
|
7
|
+
it { should be_empty }
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#push' do
|
11
|
+
before { subject.push(:hello => 'world') }
|
12
|
+
|
13
|
+
it { should_not be_empty }
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#pop' do
|
17
|
+
context do
|
18
|
+
before do
|
19
|
+
subject.push(:hello => 'world')
|
20
|
+
subject.pop
|
21
|
+
end
|
22
|
+
|
23
|
+
it { should be_empty }
|
24
|
+
specify { expect { subject.pop }.not_to raise_error }
|
25
|
+
end
|
26
|
+
|
27
|
+
context do
|
28
|
+
before do
|
29
|
+
subject.push(:hello => 'world')
|
30
|
+
subject.push(:hello => 'world')
|
31
|
+
subject.pop
|
32
|
+
end
|
33
|
+
|
34
|
+
it { should_not be_empty }
|
35
|
+
end
|
36
|
+
|
37
|
+
context do
|
38
|
+
before do
|
39
|
+
subject.push(:hello => 'world')
|
40
|
+
subject.push(:hello => 'world')
|
41
|
+
subject.pop
|
42
|
+
subject.pop
|
43
|
+
end
|
44
|
+
|
45
|
+
it { should be_empty }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '#[] and key?' do
|
50
|
+
context 'indifferent access' do
|
51
|
+
before { subject.push(:hello => 'world') }
|
52
|
+
|
53
|
+
specify { subject[:hello].should == 'world' }
|
54
|
+
specify { subject['hello'].should == 'world' }
|
55
|
+
specify { subject[:goodbye].should be_nil }
|
56
|
+
specify { subject.key?(:hello).should be_true }
|
57
|
+
specify { subject.key?('hello').should be_true }
|
58
|
+
specify { subject.key?(:goodbye).should be_false }
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'merging' do
|
62
|
+
before do
|
63
|
+
subject.push(:hello => 'world')
|
64
|
+
subject.push(:goodbye => 'hell')
|
65
|
+
end
|
66
|
+
|
67
|
+
specify { subject[:hello].should == 'world' }
|
68
|
+
specify { subject[:goodbye].should == 'hell' }
|
69
|
+
specify { subject.key?(:hello).should be_true }
|
70
|
+
specify { subject.key?(:goodbye).should be_true }
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'overlapping' do
|
74
|
+
before do
|
75
|
+
subject.push(:hello => 'world')
|
76
|
+
subject.push(:hello => 'hell')
|
77
|
+
end
|
78
|
+
|
79
|
+
specify { subject[:hello].should == 'hell' }
|
80
|
+
specify { subject.key?(:hello).should be_true }
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'overlapping cancel' do
|
84
|
+
before do
|
85
|
+
subject.push(:hello => 'world')
|
86
|
+
subject.push(:hello => 'hell')
|
87
|
+
subject.pop
|
88
|
+
end
|
89
|
+
|
90
|
+
specify { subject[:hello].should == 'world' }
|
91
|
+
specify { subject.key?(:hello).should be_true }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe '#method_missing' do
|
96
|
+
before { subject.push(:hello => 'world') }
|
97
|
+
its(:hello) { should == 'world' }
|
98
|
+
its(:goodbye) { should be_nil }
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Contextuality do
|
4
|
+
let(:klass) do
|
5
|
+
Class.new do
|
6
|
+
include Contextuality
|
7
|
+
|
8
|
+
def hello
|
9
|
+
contextuality.hello
|
10
|
+
end
|
11
|
+
|
12
|
+
def goodbye
|
13
|
+
contextuality.goodbye
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
subject { klass.new }
|
18
|
+
|
19
|
+
context 'linear' do
|
20
|
+
specify { subject.hello.should be_nil }
|
21
|
+
|
22
|
+
specify do
|
23
|
+
contextualize do
|
24
|
+
subject.hello.should be_nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
specify do
|
29
|
+
contextualize(:hello => 'world') do
|
30
|
+
subject.hello.should == 'world'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
specify do
|
35
|
+
contextualize(:hello => 'world') do
|
36
|
+
contextualize(:hello => 'hell') do
|
37
|
+
subject.hello.should == 'hell'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
specify do
|
43
|
+
contextualize(:hello => 'world') do
|
44
|
+
contextualize(:goodbye => 'hell') do
|
45
|
+
subject.hello.should == 'world'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'threaded' do
|
52
|
+
specify do
|
53
|
+
contextualize(:hello => 'world') do
|
54
|
+
Thread.new do
|
55
|
+
Thread.current[:output] = subject.hello
|
56
|
+
end.join[:output].should == 'world'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
specify do
|
61
|
+
contextualize(:hello => 'world') do
|
62
|
+
Thread.new do
|
63
|
+
contextualize(:hello => 'hell') do
|
64
|
+
Thread.current[:output] = subject.hello
|
65
|
+
end
|
66
|
+
end.join[:output].should == 'hell'
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
specify do
|
71
|
+
contextualize(:goodbye => 'hell') do
|
72
|
+
Thread.new do
|
73
|
+
contextualize(:hello => 'world') do
|
74
|
+
Thread.current[:output] = [subject.hello, subject.goodbye]
|
75
|
+
end
|
76
|
+
end.join[:output].should == ['world', 'hell']
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'contextuality'
|
2
|
+
|
3
|
+
RSpec.configure do |config|
|
4
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
5
|
+
config.run_all_when_everything_filtered = true
|
6
|
+
config.filter_run :focus
|
7
|
+
|
8
|
+
# Run specs in random order to surface order dependencies. If you find an
|
9
|
+
# order dependency and want to debug it, you can fix the order by providing
|
10
|
+
# the seed, which is printed after each run.
|
11
|
+
# --seed 1234
|
12
|
+
config.order = 'random'
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: contextuality
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- pyromaniac
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-29 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Contextual global variables
|
15
|
+
email:
|
16
|
+
- kinwizard@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .rspec
|
23
|
+
- .rvmrc
|
24
|
+
- Gemfile
|
25
|
+
- Guardfile
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- contextuality.gemspec
|
30
|
+
- lib/contextuality.rb
|
31
|
+
- lib/contextuality/context.rb
|
32
|
+
- lib/contextuality/version.rb
|
33
|
+
- spec/contextuality/context_spec.rb
|
34
|
+
- spec/contextuality_spec.rb
|
35
|
+
- spec/spec_helper.rb
|
36
|
+
homepage: ''
|
37
|
+
licenses: []
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.8.24
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Contextual global variables
|
60
|
+
test_files:
|
61
|
+
- spec/contextuality/context_spec.rb
|
62
|
+
- spec/contextuality_spec.rb
|
63
|
+
- spec/spec_helper.rb
|