ctx 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -0
- data/lib/ctx.rb +55 -0
- metadata +62 -0
data/README.md
ADDED
data/lib/ctx.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
module CTX
|
2
|
+
|
3
|
+
class ::Array
|
4
|
+
def select_first(&block)
|
5
|
+
selected = nil
|
6
|
+
each do |item|
|
7
|
+
break if (selected = block.call(item))
|
8
|
+
end
|
9
|
+
selected
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Context < Hash
|
14
|
+
attr_reader :name
|
15
|
+
def initialize(name) @name = name.to_sym end
|
16
|
+
def eql?(other) @name.eql?(other.name) end
|
17
|
+
def hash() @name.hash end
|
18
|
+
end
|
19
|
+
|
20
|
+
class ::BasicObject
|
21
|
+
|
22
|
+
def ctx(name = :anonymous, &scoped) self.class.ctx(name, &scoped) end
|
23
|
+
|
24
|
+
class << self
|
25
|
+
@@contexts = [nil]
|
26
|
+
def ctx(name = :anonymous, &scoped)
|
27
|
+
return @@contexts.last if scoped.nil?
|
28
|
+
|
29
|
+
context = Context.new(name)
|
30
|
+
@@contexts.push(context)
|
31
|
+
scoped.call(self)
|
32
|
+
@@contexts.pop
|
33
|
+
end
|
34
|
+
|
35
|
+
def ctx_define(context = :anonymous, method, &definition)
|
36
|
+
method = method.to_sym
|
37
|
+
@@ctx_methods ||= {}
|
38
|
+
@@ctx_methods[method] ||= {}
|
39
|
+
|
40
|
+
if @@ctx_methods[method][nil].nil?
|
41
|
+
@@ctx_methods[method][nil] = instance_method(method) if method_defined? method
|
42
|
+
define_method(method) do |*args|
|
43
|
+
methods = @@ctx_methods[method]
|
44
|
+
matched = @@contexts.reverse.select_first { |ctx| methods[ctx ? ctx.name : nil] }
|
45
|
+
matched.bind(self).(*args)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
scoped_method = "#{context}_#{method}".to_sym
|
50
|
+
define_method(scoped_method, &definition)
|
51
|
+
@@ctx_methods[method][context] = instance_method(scoped_method)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ctx
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mason
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Scoped define and context for use in writing more expressive DSLs
|
31
|
+
email: ctx@chipped.net
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- lib/ctx.rb
|
37
|
+
- README.md
|
38
|
+
homepage: http://rubygems.org/gems/ctx
|
39
|
+
licenses: []
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.8.24
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: Scoped define and context for use in writing more expressive DSLs
|
62
|
+
test_files: []
|