bruce-clingwrap 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "clingwrap"
3
+ s.version = "0.0.2"
4
+ s.date = "2009-01-05"
5
+ s.summary = "Simple method wrapping"
6
+ s.email = "bruce@codefluency.com"
7
+ s.homepage = "http://github.com/bruce/clingwrap"
8
+ s.description = "A simple library for wrapping Ruby methods using named or anonymous modules."
9
+ s.has_rdoc = true
10
+ s.authors = ["Bruce Williams"]
11
+ s.files = [
12
+ "clingwrap.gemspec",
13
+ "lib/clingwrap.rb",
14
+ "lib/clingwrap/basic.rb",
15
+ "examples/simple.rb"
16
+ ]
17
+ s.test_files = []
18
+ # s.rdoc_options = ["--main", "README.textile"]
19
+ s.extra_rdoc_files = []
20
+ end
@@ -0,0 +1,44 @@
1
+ $:.unshift(File.dirname(__FILE__) << '/../lib')
2
+
3
+ require 'clingwrap'
4
+
5
+ class Widget
6
+ def do_something
7
+ sleep 0.12
8
+ puts "Did something!"
9
+ end
10
+ end
11
+
12
+ module DoSomethingPreparation
13
+ def do_something
14
+ puts "(prepwork for Widget#do_something)"
15
+ super
16
+ end
17
+ end
18
+
19
+ Widget.clingwrap(DoSomethingPreparation)
20
+
21
+ Widget.clingwrap "Time invocation" do
22
+ def do_something
23
+ start = Time.now
24
+ result = super
25
+ puts "Elapsed: %.4fs" % (Time.now - start)
26
+ result
27
+ end
28
+ end
29
+
30
+ # no-op
31
+ Widget.clingwrap do
32
+ def do_something
33
+ super
34
+ end
35
+ end
36
+
37
+
38
+ widget = Widget.new
39
+ widget.do_something
40
+
41
+ puts "\nUsing super-and-extend, not just an alias hack!"
42
+ puts "---\nIt also gives anonymous wrappers pretty names when inspected:"
43
+ p (class << widget; self; end).ancestors
44
+
@@ -0,0 +1,14 @@
1
+ require 'clingwrap/basic'
2
+
3
+ module Cling::Wrap
4
+
5
+ def clingwrap(*args, &block)
6
+ ::Cling._wrap(self, caller[0], *args, &block)
7
+ end
8
+
9
+ end
10
+
11
+ class Class
12
+ include Cling::Wrap
13
+ end
14
+
@@ -0,0 +1,55 @@
1
+ module Cling
2
+
3
+ module Hook
4
+
5
+ def new(*args, &block)
6
+ ::Cling.on(super, self)
7
+ end
8
+
9
+ end
10
+
11
+ def self.on(instance, level)
12
+ if level.superclass
13
+ on(instance, level.superclass)
14
+ end
15
+ current = wrappers[level]
16
+ if current
17
+ current.each { |wrapper| instance.extend(wrapper) }
18
+ end
19
+ instance
20
+ end
21
+
22
+ def self.label(wrapper, description, location)
23
+ short = File.basename(location)
24
+ contents = if !wrapper.name.empty?
25
+ ":#{wrapper.name}@#{short}"
26
+ elsif description
27
+ ":#{description.inspect}@#{short}"
28
+ else
29
+ "@#{short}"
30
+ end
31
+ label = "(Clingwrap#{contents})"
32
+ (class << wrapper; self; end).class_eval "def inspect; #{label.inspect}; end"
33
+ end
34
+
35
+ def self.wrap(target, *args, &block)
36
+ _wrap(target, caller[0], *args, &block)
37
+ end
38
+
39
+ def self._wrap(target, location, *args, &block)
40
+ description = args.last.is_a?(String) ? args.pop : nil
41
+ wrapper = args.shift || Module.new(&block)
42
+ label(wrapper, description, location)
43
+ (wrappers[target] ||= []) << wrapper
44
+ target.extend Hook
45
+ end
46
+
47
+ def self.wrappers
48
+ @wrappers ||= Hash.new
49
+ end
50
+
51
+ def self.[](target)
52
+ wrappers[target]
53
+ end
54
+
55
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bruce-clingwrap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Bruce Williams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-05 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A simple library for wrapping Ruby methods using named or anonymous modules.
17
+ email: bruce@codefluency.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - clingwrap.gemspec
26
+ - lib/clingwrap.rb
27
+ - lib/clingwrap/basic.rb
28
+ - examples/simple.rb
29
+ has_rdoc: true
30
+ homepage: http://github.com/bruce/clingwrap
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.2.0
52
+ signing_key:
53
+ specification_version: 2
54
+ summary: Simple method wrapping
55
+ test_files: []
56
+