ctx 2.1.3 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +18 -2
- data/lib/ctx.rb +41 -34
- metadata +21 -4
data/README.md
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
ctx
|
2
2
|
===
|
3
3
|
|
4
|
-
|
4
|
+
Tired of waiting for refinements in ruby 2.0?
|
5
|
+
|
6
|
+
Or something like it?
|
7
|
+
|
8
|
+
Me, too.
|
9
|
+
|
10
|
+
So, this.
|
5
11
|
|
6
12
|
Example
|
7
13
|
===
|
@@ -67,5 +73,15 @@ There are bugs. You'll find them.
|
|
67
73
|
Caveats and such
|
68
74
|
===
|
69
75
|
|
70
|
-
For whatever
|
76
|
+
For whatever reason, this stuff doesn't play well with rspec all the time,
|
77
|
+
particularly if you override stuff the matchers are hoping to also override,
|
78
|
+
like == or =~ on String, who knew?
|
79
|
+
|
80
|
+
*So, don't do that.*
|
81
|
+
|
82
|
+
...not unless you like stuff to break in inexplicable ways.
|
83
|
+
|
84
|
+
Personally, I *do* like things breaking in inexplicable ways (why else would I write things like this?)
|
85
|
+
since makes life interesting (also kind of explains my dating choices, when you get right down to it),
|
86
|
+
but, honestly, it's your call.
|
71
87
|
|
data/lib/ctx.rb
CHANGED
@@ -1,12 +1,6 @@
|
|
1
|
-
|
1
|
+
require 'mobj'
|
2
2
|
|
3
|
-
|
4
|
-
def return_first(&block)
|
5
|
-
returned = nil
|
6
|
-
each { |item| break if (returned = block.call(item)) }
|
7
|
-
returned
|
8
|
-
end
|
9
|
-
end
|
3
|
+
module CTX
|
10
4
|
|
11
5
|
class Context < Hash
|
12
6
|
attr_reader :name
|
@@ -17,14 +11,13 @@ module CTX
|
|
17
11
|
end
|
18
12
|
|
19
13
|
class ::Object
|
20
|
-
def sym() respond_to?(:to_sym) ? to_sym : to_s.to_sym end
|
21
14
|
def ctxputs(*args)
|
22
|
-
ctxs = @@contexts.map(&:name)[1..-1].join(":")
|
15
|
+
ctxs = @@contexts.map(&:name)[1..-1].reverse.join(":")
|
23
16
|
ctxs = "[CTX #{ctxs}] " unless ctxs.empty?
|
24
17
|
puts "#{ctxs}#{args.map(&:to_s).join(', ')}"
|
25
18
|
end
|
26
19
|
def ctxp(*args)
|
27
|
-
ctxs = @@contexts.map(&:name)[1..-1].join(":")
|
20
|
+
ctxs = @@contexts.map(&:name)[1..-1].reverse.join(":")
|
28
21
|
ctxs = "[CTX #{ctxs.empty? ? "-" : ctxs}]"
|
29
22
|
puts "#{ctxs}\n#{args.map(&:inspect).join("\n")}"
|
30
23
|
end
|
@@ -32,53 +25,67 @@ module CTX
|
|
32
25
|
@@contexts ||= [Context.new(nil)]
|
33
26
|
def ctx(context = :anonymous, &contextual)
|
34
27
|
if contextual.nil?
|
35
|
-
@@contexts.
|
28
|
+
@@contexts.first
|
36
29
|
else
|
37
|
-
@@contexts.
|
30
|
+
@@contexts.unshift(Context.new(context))
|
38
31
|
instance_eval(&contextual)
|
39
|
-
@@contexts.
|
32
|
+
@@contexts.shift()
|
40
33
|
end
|
41
34
|
end
|
42
35
|
|
43
36
|
end
|
44
37
|
|
45
|
-
class
|
38
|
+
class Template < BasicObject
|
46
39
|
|
47
|
-
|
48
|
-
def
|
49
|
-
def
|
40
|
+
attr_accessor :added
|
41
|
+
def initialize() @added ||= [] end
|
42
|
+
def singleton_method_added(name) added << name end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
class ::Class
|
50
47
|
|
51
48
|
attr_accessor :ctx_methods
|
52
49
|
|
53
50
|
def ctx(context = :anonymous, &contextual)
|
54
51
|
@ctx_methods ||= {}
|
55
52
|
|
56
|
-
template =
|
57
|
-
template.
|
53
|
+
template = Template.new
|
54
|
+
template.instance_exec(self, &contextual)
|
55
|
+
|
56
|
+
matching = template.added
|
58
57
|
|
59
|
-
matching = self.object_methods & template.object_methods
|
60
58
|
matching.each do |method_name|
|
61
59
|
if ctx_methods[method_name].nil?
|
62
60
|
ctx_methods[method_name] = {}
|
63
|
-
|
61
|
+
if instance_methods(true).includes? method_name
|
62
|
+
ctx_methods[method_name][nil.sym] = instance_method(method_name)
|
63
|
+
elsif singleton_methods(true).include? method_name
|
64
|
+
ctx_methods[method_name][nil.sym] = singleton_class.instance_method(method_name)
|
65
|
+
end
|
64
66
|
end
|
65
67
|
end
|
66
68
|
|
67
69
|
self.class_eval(&contextual)
|
68
70
|
|
69
|
-
|
70
|
-
if
|
71
|
-
ctx_methods[method_name] =
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
71
|
+
matching.each do |method_name|
|
72
|
+
if instance_methods(true).includes? method_name
|
73
|
+
ctx_methods[method_name][context] = instance_method(method_name)
|
74
|
+
define_method(method_name) do |*args|
|
75
|
+
methods = self.class.ctx_methods[method_name]
|
76
|
+
matched = @@contexts.return_first { |currentctx| methods[currentctx.name] }
|
77
|
+
matched.bind(self).(*args)
|
78
|
+
end
|
79
|
+
|
80
|
+
elsif singleton_methods(true).include? method_name
|
81
|
+
ctx_methods[method_name][context] = singleton_class.instance_method(method_name)
|
82
|
+
|
83
|
+
define_singleton_method(method_name) do |*args|
|
84
|
+
methods = self.ctx_methods[method_name]
|
85
|
+
matched = @@contexts.return_first { |currentctx| methods[currentctx.name] }
|
86
|
+
matched.bind(self).(*args)
|
87
|
+
end
|
80
88
|
end
|
81
|
-
|
82
89
|
end
|
83
90
|
end
|
84
91
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ctx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,24 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mobj
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
14
30
|
- !ruby/object:Gem::Dependency
|
15
31
|
name: rspec
|
16
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -59,7 +75,8 @@ dependencies:
|
|
59
75
|
- - ! '>='
|
60
76
|
- !ruby/object:Gem::Version
|
61
77
|
version: '0'
|
62
|
-
description:
|
78
|
+
description: Scoped and contextual method definition for use in writing more expressive
|
79
|
+
DSLs without screwing defintions in other pieces of code
|
63
80
|
email: ctx@chipped.net
|
64
81
|
executables: []
|
65
82
|
extensions: []
|
@@ -90,5 +107,5 @@ rubyforge_project:
|
|
90
107
|
rubygems_version: 1.8.24
|
91
108
|
signing_key:
|
92
109
|
specification_version: 3
|
93
|
-
summary:
|
110
|
+
summary: Contextual method define
|
94
111
|
test_files: []
|