nakajima-nakajima 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/nakajima.rb CHANGED
@@ -1,18 +1,14 @@
1
- $LOAD_PATH << File.join(File.dirname(__FILE__), 'nakajima')
2
- $LOAD_PATH << File.join(File.dirname(__FILE__), 'nakajima', 'core_ext')
1
+ $LOAD_PATH << File.dirname(__FILE__)
3
2
 
4
3
  # gems
5
- require 'rubygems'
6
4
  require 'colored'
7
5
  require 'metaid'
8
6
 
9
7
  # core ruby extensions
10
- require 'basic_object'
11
- require 'array'
12
- require 'symbol'
13
- require 'proc'
14
- require 'object'
8
+ require 'nakajima/core_ext/array'
9
+ require 'nakajima/core_ext/kernel'
10
+ require 'nakajima/core_ext/object'
11
+ require 'nakajima/core_ext/proc'
12
+ require 'nakajima/core_ext/symbol'
15
13
 
16
- module Nakajima
17
- VERSION = '0.0.4'
18
- end
14
+ module Nakajima; end
@@ -0,0 +1,19 @@
1
+ module Kernel
2
+ def blank_context(*args, &block)
3
+ ivars = args.extract_options!
4
+
5
+ args.push(/^__/)
6
+
7
+ klass = Class.new do
8
+ instance_methods.each do |m|
9
+ undef_method(m) unless args.any? { |pattern| m =~ pattern }
10
+ end
11
+ end
12
+
13
+ klass.class_eval(&block) if block_given?
14
+ instance = klass.new
15
+ setter = Object.instance_method(:instance_variable_set).bind(instance)
16
+ ivars.each { |key, value| setter.call("@#{key}", value) }
17
+ instance
18
+ end
19
+ end
@@ -1,62 +1,60 @@
1
1
  module Nakajima
2
2
  module Object
3
- module InstanceMethods
4
- def try(method_id, *args, &block)
5
- respond_to?(method_id) ? send(method_id, *args, &block) : nil
6
- end
7
-
8
- # Like JavaScript, but in Ruby...
9
- def with(hash)
10
- hash.each do |key, value|
11
- meta_def(key) { hash[key] }
12
- meta_def("#{key}=") { |v| hash[key] = v }
13
- end
3
+ def try(method_id, *args, &block)
4
+ respond_to?(method_id) ? send(method_id, *args, &block) : nil
5
+ end
14
6
 
15
- return unless block_given?
7
+ # Like JavaScript, but in Ruby...
8
+ def with(hash)
9
+ hash.each do |key, value|
10
+ meta_def(key) { hash[key] }
11
+ meta_def("#{key}=") { |v| hash[key] = v }
12
+ end
16
13
 
17
- result = yield
14
+ return unless block_given?
18
15
 
19
- hash.each do |key, value|
20
- meta_eval { remove_method(key) }
21
- meta_eval { remove_method("#{key}=") }
22
- end
16
+ result = yield
23
17
 
24
- result
25
- end
26
-
27
- unless respond_to?(:instance_exec)
28
- def instance_exec(*arguments, &block)
29
- block.bind(self)[*arguments]
30
- end
18
+ hash.each do |key, value|
19
+ meta_eval { remove_method(key) }
20
+ meta_eval { remove_method("#{key}=") }
31
21
  end
32
-
33
- # A proxy that returns its target after invoking the method you
34
- # invoke. Useful for #me
35
- class ProxyReturningMe < BasicObject
36
- def initialize(me)
37
- super()
38
- @me = me
22
+
23
+ result
24
+ end
25
+
26
+ unless respond_to?(:instance_exec)
27
+ module InstanceExecHelper; end
28
+ include InstanceExecHelper
29
+ def instance_exec(*args, &block)
30
+ begin
31
+ old_critical, Thread.critical = Thread.critical, true
32
+ n = 0
33
+ n += 1 while respond_to?(mname="__instance_exec#{n}")
34
+ InstanceExecHelper.module_eval{ define_method(mname, &block) }
35
+ ensure
36
+ Thread.critical = old_critical
39
37
  end
40
- def method_missing(sym, *args, &block)
41
- @me.__send__(sym, *args, &block)
42
- @me
38
+ begin
39
+ ret = send(mname, *args)
40
+ ensure
41
+ InstanceExecHelper.module_eval{ remove_method(mname) } rescue nil
43
42
  end
43
+ ret
44
44
  end
45
-
46
- # plucked from raganwald's andand
47
- def tap(p = nil)
48
- if block_given?
49
- yield(self)
50
- self
51
- elsif p
52
- p.to_proc.call(self)
53
- self
54
- else
55
- ProxyReturningMe.new(self)
45
+ end
46
+
47
+ def tap
48
+ block_given? ? (yield(self); self) : begin
49
+ blank_context(:me => self) do
50
+ def method_missing(sym, *args, &block)
51
+ @me.__send__(sym, *args, &block)
52
+ @me
53
+ end
56
54
  end
57
55
  end
58
56
  end
59
57
  end
60
58
  end
61
59
 
62
- Object.send :include, Nakajima::Object::InstanceMethods
60
+ Object.send :include, Nakajima::Object
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nakajima-nakajima
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Nakajima
@@ -49,12 +49,11 @@ extra_rdoc_files: []
49
49
 
50
50
  files:
51
51
  - lib/nakajima
52
- - lib/nakajima/bench.rb
53
52
  - lib/nakajima/core_ext
54
- - lib/nakajima/core_ext/basic_object.rb
55
53
  - lib/nakajima/core_ext/array.rb
56
- - lib/nakajima/core_ext/proc.rb
54
+ - lib/nakajima/core_ext/kernel.rb
57
55
  - lib/nakajima/core_ext/object.rb
56
+ - lib/nakajima/core_ext/proc.rb
58
57
  - lib/nakajima/core_ext/symbol.rb
59
58
  - lib/nakajima/ui
60
59
  - lib/nakajima/ui/progress.rb
@@ -1,10 +0,0 @@
1
- def bench(*args, &block)
2
- options = args.extract_options!
3
-
4
- times = options[:times] || 1000
5
- label = args.first || "running:"
6
-
7
- t = Benchmark.measure { times.times(&block) }
8
-
9
- puts "#{label} #{t.real}"
10
- end
@@ -1,5 +0,0 @@
1
- module Nakajima
2
- class BasicObject
3
- instance_methods.reject { |m| m =~ /^__/ }.each { |m| undef_method m }
4
- end
5
- end