monkey-lib 0.1.6 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,51 +0,0 @@
1
- Sort of my own ruby extlib. There is a spec for everything and it is very clean.
2
- It does avoid monkey patching and relies on modules.
3
-
4
- = Template for extentiones
5
-
6
- Say we want to extend Object by adding some methods dealing with magic.
7
- This would go to the file lib/monkey/object/magic_stuff.rb:
8
-
9
- module Monkey
10
- module Object
11
-
12
- # It's magic!
13
- module MagicStuff
14
-
15
- ::Object.send :include, self
16
-
17
- def hokus_pokus
18
- Spell.perform(self)
19
- end
20
-
21
- def vanish_in_thin_air
22
- class << self
23
- instance_methods.each { |m| undef_method(m) }
24
- end
25
- end
26
-
27
- end
28
-
29
- end
30
- end
31
-
32
- Add to lib/monkey/object.rb:
33
-
34
- require "monkey/object/magic_stuff"
35
-
36
- If that file didn't exist before, add to lib/monkey.rb:
37
-
38
- require "monkey/object"
39
-
40
- Write a spec in spec/monkey/object/magic_stuff_spec.rb:
41
-
42
- require __FILE__.sub("monkey/object/magic_stuff_spec.rb", "spec_helper")
43
- require "monkey/object/magic_stuff"
44
-
45
- describe Monkey::Object::MagicStuff do
46
-
47
- it "performs magic" do
48
- # ...
49
- end
50
-
51
- end
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require "rake/rdoctask"
4
4
  require "rake/gempackagetask"
5
5
  require "rubygems/specification"
6
6
 
7
- MONKEY_VERSION = "0.1.6"
7
+ MONKEY_VERSION = "0.2.1"
8
8
 
9
9
  spec = Gem::Specification.new do |s|
10
10
  s.name = "monkey-lib"
data/lib/monkey-lib.rb CHANGED
@@ -1 +1,2 @@
1
- require "monkey"
1
+ require "monkey"
2
+ MonkeyLib = Monkey
data/lib/monkey.rb CHANGED
@@ -1,4 +1,6 @@
1
- require "monkey/object"
2
- require "monkey/hash"
3
- require "monkey/string"
4
- require "monkey/engine"
1
+ module Monkey
2
+ Dir[File.dirname(__FILE__) + "/monkey/*.rb"].sort.each do |path|
3
+ filename = File.basename(path, '.rb')
4
+ require "monkey/#{filename}"
5
+ end
6
+ end
@@ -0,0 +1,21 @@
1
+ module Monkey
2
+ module Autoloader
3
+ def const_missing(const_name)
4
+ const_name = const_name.to_s
5
+ file = File.join(const_name.to_const_path, self.name.to_const_path)
6
+ begin
7
+ require file
8
+ if const_defined? const_name
9
+ const = const_get const_get
10
+ const.extend Monkey::Autoloader
11
+ const
12
+ else
13
+ warn "expected #{file} to define #{name}::#{const_name}"
14
+ raise LoadError
15
+ end
16
+ rescue LoadError
17
+ super
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,45 @@
1
+ require "set"
2
+
3
+ module Monkey
4
+ module Backend
5
+
6
+ autoload :ActiveSupport, "monkey/backend/active_support"
7
+ autoload :BackendDsl, "monkey/backend/backend_dsl"
8
+ autoload :Backports, "monkey/backend/backports"
9
+ autoload :Extlib, "monkey/backend/extlib"
10
+
11
+ def self.register(definition, expected_name, backend_name = nil)
12
+ klass = definition.core_klass
13
+ add_hook klass, expected_name
14
+ add_hook klass, backend_name if backend_name
15
+ backend_name ||= expected_name
16
+ end
17
+
18
+ def preferred_backend
19
+ available_backends.detect { |b| eval "defined? ::#{b}" }
20
+ end
21
+
22
+ def available_backends
23
+ @available_backends ||= Set[:ActiveSupport, :Extlib, :Backports]
24
+ end
25
+
26
+ def backend
27
+ @backend || preferred_backend || available_backends.first
28
+ end
29
+
30
+ private
31
+
32
+ def self.add_hook(klass, name)
33
+ mod = module_for klass
34
+ mod.class_eval "def #{name}(*a, &b); Monkey::Backend.call(#{klass.name}, #{name}, *a, &b); end"
35
+ end
36
+
37
+ def self.module_for(klass, &block)
38
+ mod = eval "module Monkey::Ext; module #{klass.name}; self; end; end"
39
+ klass.send :include, mod
40
+ mod.class_eval(&block) if block
41
+ mod
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,17 @@
1
+ module Monkey
2
+ module Backend
3
+ module ActiveSupport
4
+ def self.setup
5
+ require "active_support/core_ext/array/extract_options"
6
+ require "active_support/core_ext/object/metaclass"
7
+ require "active_support/core_ext/object/misc"
8
+ require "active_support/core_ext/string/inflection"
9
+ require "active_support/core_ext/module/introspection"
10
+ ::String.class_eval do
11
+ alias to_const_string camelcase
12
+ alias to_const_path underscore
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Monkey
2
+ module Backend
3
+ module Backports
4
+ def setup
5
+ require "backports/1.8.7/kernel"
6
+ require "backports/rails/array"
7
+ require "backports/rails/string"
8
+ require "monkey/backend/common/parent"
9
+ require "monkey/backend/common/metaclass"
10
+ ::String.class_eval do
11
+ alias to_const_string camelcase
12
+ alias to_const_path underscore
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ module Monkey
2
+ module Backend
3
+ module Common
4
+ module ExtractOptions
5
+ ::Array.send :include, self
6
+ def extract_options!
7
+ last.is_a?(::Hash) ? pop : {}
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ module Monkey
2
+ module Backend
3
+ module Common
4
+ module Metaclass
5
+ ::Object.send :include, self
6
+ def metaclass
7
+ class << self
8
+ self
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Monkey
2
+ module Backend
3
+ module Common
4
+ module Metaclass
5
+ ::Object.send :include, self
6
+ def metaclass
7
+ class << self
8
+ self
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ module Monkey
2
+ module Backend
3
+ module Common
4
+ module Tap
5
+ ::Kernel.send :include, self
6
+ def tap
7
+ yield self
8
+ self
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+ module Monkey
2
+ module Backend
3
+ module Extlib
4
+ def setup
5
+ require "extlib/object"
6
+ require "extlib/string"
7
+ require "extlib/constantize"
8
+ require "monkey/backend/common/parent"
9
+ require "monkey/backend/common/extract_options"
10
+ require "monkey/backend/common/tap"
11
+ ::Object.class_eval { alias metaclass meta_class }
12
+ ::String.class_eval do
13
+ alias camelcase to_const_string
14
+ alias underscore to_const_path
15
+ def constantize
16
+ Extlib::Inflection.constantize(self)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ module Monkey
2
+ module Backend
3
+ module Facets
4
+ def setup
5
+ # Actually, facets has Kernel#tap, but it behaves different if the block takes no argument.
6
+ require "monkey/backend/common/tap"
7
+ require "monkey/backend/common/extract_options"
8
+ require "monkey/backend/common/parent"
9
+ require "core/facets/kernel/meta_class"
10
+ require "core/facets/kernel/constant"
11
+ ::String.class_eval do
12
+ def constantize
13
+ constant modulize
14
+ end
15
+ alias to_const_string camelcase
16
+ alias to_const_path snakecase
17
+ alias underscore snakecase
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
data/lib/monkey/engine.rb CHANGED
@@ -1,17 +1,45 @@
1
1
  module Monkey
2
-
2
+
3
3
  # Makes sure we always have RUBY_ENGINE, RUBY_ENGINE_VERSION and RUBY_DESCRIPTION
4
+ # TODO: Check IronRuby version detection.
4
5
  module Engine
5
6
 
7
+ def jruby?; RUBY_ENGINE == "jruby"; end
8
+ def mri?; RUBY_ENGINE == "ruby"; end
9
+ def rbx?; RUBY_ENGINE == "rbx"; end
10
+ def ironruby?; RUBY_ENGINE == "ironruby"; end
11
+ def macruby?; RUBY_ENGINE == "macruby"; end
12
+ def maglev?; RUBY_ENGINE == "maglev"; end
13
+
14
+ alias rubinius? rbx?
15
+
16
+ def ree?
17
+ RUBY_DESCRIPTION =~ /Ruby Enterprise Edition/
18
+ end
19
+
20
+ module_function :jruby?
21
+ module_function :mri?
22
+ module_function :rbx?
23
+ module_function :rubinius?
24
+ module_function :ironruby?
25
+ module_function :macruby?
26
+ module_function :maglev?
27
+ module_function :ree?
28
+
29
+ def ruby_core?
30
+ maglev? or rubinius?
31
+ end
32
+
33
+ module_function :ruby_core?
34
+
6
35
  include Rubinius if defined? Rubinius
7
36
 
8
37
  unless defined? RUBY_ENGINE
9
- if defined? JRUBY_VERSION
10
- ::RUBY_ENGINE = "jruby"
11
- elsif defined? Rubinius
12
- ::RUBY_ENGINE = "rbx"
13
- else
14
- ::RUBY_ENGINE = "ruby"
38
+ if defined? JRUBY_VERSION then ::RUBY_ENGINE = "jruby"
39
+ elsif defined? Rubinius then ::RUBY_ENGINE = "rbx"
40
+ elsif defined? NSObject then ::RUBY_ENINGE = "macruby"
41
+ elsif defined? Maglev then ::RUBY_ENINGE = "maglev"
42
+ else ::RUBY_ENGINE = "ruby"
15
43
  end
16
44
  end
17
45
 
@@ -21,7 +49,15 @@ module Monkey
21
49
  RUBY_ENGINE.freeze
22
50
  end
23
51
 
24
- ::RUBY_ENGINE_VERSION = const_get("#{RUBY_ENGINE.upcase}_VERSION")
52
+ unless defined? RUBY_ENGINE_VERSION
53
+ begin
54
+ # ruby, jruby, macruby, some rubinius versions
55
+ ::RUBY_ENGINE_VERSION = const_get("#{RUBY_ENGINE.upcase}_VERSION")
56
+ rescue NameError
57
+ # maglev, some rubinius versions
58
+ ::RUBY_ENGINE_VERSION = const_get("VERSION")
59
+ end
60
+ end
25
61
 
26
62
  unless defined? RUBY_DESCRIPTION
27
63
  ::RUBY_DESCRIPTION = "#{RUBY_ENGINE} #{RUBY_ENGINE_VERSION} "
@@ -38,52 +74,17 @@ module Monkey
38
74
  ::RUBY_DESCRIPTION << "[#{RUBY_PLATFORM}]"
39
75
  end
40
76
 
41
- def jruby?
42
- RUBY_ENGINE == "jruby"
43
- end
44
-
45
- module_function :jruby?
46
-
47
- def mri?
48
- RUBY_ENGINE == "ruby"
49
- end
50
-
51
- module_function :mri?
52
-
53
- def rbx?
54
- RUBY_ENGINE == "rbx"
55
- end
56
-
57
- alias rubinius? rbx?
58
- module_function :rbx?
59
- module_function :rubinius?
60
-
61
- def ironruby?
62
- RUBY_ENGINE == "ironruby"
63
- end
64
-
65
- module_function :ironruby?
66
-
67
- def macruby?
68
- RUBY_ENGINE == "macruby"
69
- end
70
-
71
- module_function :macruby?
72
-
73
77
  def ruby_engine(pretty = true)
74
78
  return RUBY_ENGINE unless pretty
75
79
  case RUBY_ENGINE
76
- when "ruby" then "CRuby"
77
- when "rbx" then "Rubinius"
78
- when /ruby$/ then RUBY_ENGINE.capitalize.gsub("ruby", "Ruby")
80
+ when "ruby" then "MatzRuby"
81
+ when "rbx" then "Rubinius"
82
+ when "maglev" then "MagLev"
83
+ else RUBY_ENGINE.capitalize.gsub("ruby", "Ruby")
79
84
  end
80
85
  end
81
86
 
82
87
  module_function :ruby_engine
83
88
 
84
89
  end
85
-
86
- extend Engine
87
- include Engine
88
-
89
90
  end
data/lib/monkey/ext.rb ADDED
@@ -0,0 +1,35 @@
1
+ module Monkey
2
+ module Ext
3
+
4
+ module ExtDSL
5
+
6
+ def core_class(klass = nil)
7
+ if klass
8
+ @core_class = klass
9
+ klass.send :include, self
10
+ end
11
+ return @core_class
12
+ end
13
+
14
+ def expects(*list)
15
+ list.each do |name|
16
+ unless instance_method name
17
+ # Note: Ruby < 1.8.7 does not support { |*a, &b| } syntax.
18
+ class_eval "def #{name}(*a, &b); Monkey::Backend.call(#{core_class.inspect}, #{name.to_s.inspect}, *a, &b); end"
19
+ end
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ Dir[File.dirname(__FILE__) + "/ext/*.rb"].sort.each do |path|
26
+ filename = File.basename(path, '.rb')
27
+ class_name = filename.capitalize
28
+ extension = class_eval "module #{class_name}; self; end"
29
+ extension.extend ExtDSL
30
+ extension.core_class = Object.const_get class_name
31
+ require "ext/monkey/#{filename}"
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,8 @@
1
+ module Monkey
2
+ module Ext
3
+ module Array
4
+ # Defined by backend.
5
+ expects :extract_options!
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,24 @@
1
+ module Monkey
2
+ module Ext
3
+ module Module
4
+
5
+ # Defined by backend.
6
+ expects :parent
7
+
8
+ def nested_method_missing(mod, meth, *args, &block)
9
+ # Triggers method_missing chain inside mod.
10
+ raise Monkey::Watcher::NestedMethodMissingNotHandled
11
+ end
12
+
13
+ def class_added(klass)
14
+ end
15
+
16
+ def module_added(mod)
17
+ return if self == parent
18
+ class_added mod if mod.is_a? Class
19
+ parent.module_added mod
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,38 @@
1
+ module Monkey
2
+ module Ext
3
+ module Object
4
+
5
+ # Defined by backend.
6
+ expects :metaclass, :tap
7
+
8
+ # Behaves like instance_eval or yield depending on whether a block takes an argument or not.
9
+ #
10
+ # class Foo
11
+ # define_method(:foo) { 42 }
12
+ # end
13
+ #
14
+ # Foo.new.instance_yield { foo } # => 42
15
+ # Foo.new.instance_yield { |c| c.foo } # => 42
16
+ #
17
+ # Also, you can pass a proc directly:
18
+ #
19
+ # block = proc { }
20
+ # instance_yield(block)
21
+ def instance_yield(block = nil, &alternate_block)
22
+ raise ArgumentError, "too many blocks given" if block && alternate_block
23
+ block ||= alternate_block
24
+ raise LocalJumpError, "no block given (yield)" unless block
25
+ block.arity > 0 ? yield(self) : instance_eval(&block)
26
+ end
27
+
28
+ def metaclass_eval(str = nil, &block)
29
+ metaclass.class_eval(str, &block)
30
+ end
31
+
32
+ def define_singleton_method(name, &block)
33
+ metaclass_eval { define_method(name, &block) }
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,8 @@
1
+ module Monkey
2
+ module Ext
3
+ module String
4
+ # Defined by backend.
5
+ expects :constantize, :to_const_string, :to_const_path, :underscore, :camelcase
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,139 @@
1
+ module Monkey
2
+ # NOTE: Not thread-safe!
3
+ module Watcher
4
+
5
+ IDENTIFIER = "@__module_detected__"
6
+
7
+ class NestedMethodMissingNotHandled < ScriptError
8
+ end
9
+
10
+ module ObjectWatcher
11
+ ::Object.extend self
12
+
13
+ def inherited(klass)
14
+ Monkey::Watcher.module_defined(klass)
15
+ super
16
+ end
17
+
18
+ end
19
+
20
+ module MethodWatcher
21
+ ::Module.send :include, self
22
+
23
+ def method_added(name)
24
+ Monkey::Watcher.method_defined(self, name)
25
+ super
26
+ end
27
+
28
+ def method_missing(name, *args, &block)
29
+ if Monkey::Watcher.detected? self
30
+ begin
31
+ Monkey::Watcher.module_method_missing(self, *args, &block)
32
+ rescue Monkey::Watcher::NestedMethodMissingNotHandled
33
+ super
34
+ end
35
+ else
36
+ Monkey::Watcher.module_defined self
37
+ if Monkey::Watcher.detected? self
38
+ __send__(name, *args, &block)
39
+ else
40
+ super
41
+ end
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ module ModuleWatcher
48
+ ::Module.extend self
49
+
50
+ def new(*what, &ever)
51
+ super.tap { |k| Monkey::Watcher.module_defined(klass) }
52
+ end
53
+
54
+ end
55
+
56
+ module NamelessModuleWatcher
57
+
58
+ def __watcher_initialize__
59
+ @__original_methods__ = {}
60
+ if respond_to? :set_name_if_necessary
61
+ __watcher_wrap_method__ "set_name_if_necessary"
62
+ else
63
+ methods.each do |name|
64
+ name = name.to_s
65
+ next if name =~ /^__/
66
+ __watcher_wrap_method__ name
67
+ end
68
+ end
69
+ define_singleton_method("method_addded") do |name|
70
+ __watcher_wrap_method__ name unless Monkey::Watcher.detected? self
71
+ super
72
+ end
73
+ end
74
+
75
+ def __watcher_wrap_method__(name)
76
+ @__original_methods__[name] = method(name)
77
+ eval "def self.#{name}(*a, &b); __watcher_wrap_call__(#{name.inspect}, *a, &b); end"
78
+ end
79
+
80
+ def __watcher_wrap_call__(meth, *args, &block)
81
+ Monkey::Watcher.module_defined(klass)
82
+ @__original_methods__[meth].call(*args, &block).tap do
83
+ next if name.blank?
84
+ Monkey::Watcher.module_defined(klass)
85
+ methods_names = @__original_methods__.keys
86
+ methods_names.each { |m| define_singleton_method(m, &@__original_methods__.delete(m)) }
87
+ undef __watcher_initialize__
88
+ undef __watcher_wrap_method__
89
+ undef __watcher_wrap_call__
90
+ end
91
+ end
92
+
93
+ def self.extended(klass)
94
+ klass.__watcher_initialize__
95
+ super
96
+ end
97
+
98
+ end
99
+
100
+ def self.module_defined(klass)
101
+ return if detected? klass
102
+ return nameless_module_defined(klass) unless klass.name
103
+ klass.instance_variable_set IDENTIFIER, true
104
+ klass.parent.module_added(klass)
105
+ klass.constants.each do |name|
106
+ mod = klass.const_get name
107
+ module_defined mod if mod.is_a? Module
108
+ end
109
+ end
110
+
111
+ def self.nameless_module_defined(klass)
112
+ return if hooked? klass
113
+ klass.instance_variable_set IDENTIFIER, false
114
+ klass.extend NamelessModuleWatcher
115
+ end
116
+
117
+ def self.method_defined(klass, name)
118
+ module_defined(klass)
119
+ end
120
+
121
+ def self.hooked?(klass)
122
+ klass.instance_variable_defined? IDENTIFIER
123
+ end
124
+
125
+ def self.detected?(klass)
126
+ !!klass.instance_variable_get IDENTIFIER
127
+ end
128
+
129
+ def self.setup
130
+ begin
131
+ ObjectSpace.each_object(Module) { |klass| module_defined(klass) }
132
+ rescue Exception
133
+ # Thus we meet at last, JRuby!
134
+ module_defined Object
135
+ end
136
+ end
137
+
138
+ end
139
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monkey-lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Konstantin Haase
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-22 00:00:00 +02:00
12
+ date: 2009-12-11 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -31,28 +31,48 @@ extensions: []
31
31
  extra_rdoc_files:
32
32
  - README.rdoc
33
33
  - LICENSE
34
+ - lib/monkey/autoloader.rb
35
+ - lib/monkey/backend/active_support.rb
36
+ - lib/monkey/backend/backports.rb
37
+ - lib/monkey/backend/common/extract_options.rb
38
+ - lib/monkey/backend/common/metaclass.rb
39
+ - lib/monkey/backend/common/parent.rb
40
+ - lib/monkey/backend/common/tap.rb
41
+ - lib/monkey/backend/extlib.rb
42
+ - lib/monkey/backend/facets.rb
43
+ - lib/monkey/backend.rb
34
44
  - lib/monkey/engine.rb
35
- - lib/monkey/hash/sub_hash.rb
36
- - lib/monkey/hash.rb
37
- - lib/monkey/object/backports.rb
38
- - lib/monkey/object/instance_yield.rb
39
- - lib/monkey/object.rb
45
+ - lib/monkey/ext/array.rb
46
+ - lib/monkey/ext/module.rb
47
+ - lib/monkey/ext/object.rb
48
+ - lib/monkey/ext/string.rb
49
+ - lib/monkey/ext.rb
40
50
  - lib/monkey/string/like_pathname.rb
41
- - lib/monkey/string.rb
51
+ - lib/monkey/watcher.rb
42
52
  - lib/monkey-lib.rb
43
53
  - lib/monkey.rb
44
54
  files:
45
55
  - LICENSE
46
56
  - Rakefile
47
57
  - README.rdoc
58
+ - lib/monkey/autoloader.rb
59
+ - lib/monkey/backend/active_support.rb
60
+ - lib/monkey/backend/backports.rb
61
+ - lib/monkey/backend/common/extract_options.rb
62
+ - lib/monkey/backend/common/metaclass.rb
63
+ - lib/monkey/backend/common/parent.rb
64
+ - lib/monkey/backend/common/tap.rb
65
+ - lib/monkey/backend/extlib.rb
66
+ - lib/monkey/backend/facets.rb
67
+ - lib/monkey/backend.rb
48
68
  - lib/monkey/engine.rb
49
- - lib/monkey/hash/sub_hash.rb
50
- - lib/monkey/hash.rb
51
- - lib/monkey/object/backports.rb
52
- - lib/monkey/object/instance_yield.rb
53
- - lib/monkey/object.rb
69
+ - lib/monkey/ext/array.rb
70
+ - lib/monkey/ext/module.rb
71
+ - lib/monkey/ext/object.rb
72
+ - lib/monkey/ext/string.rb
73
+ - lib/monkey/ext.rb
54
74
  - lib/monkey/string/like_pathname.rb
55
- - lib/monkey/string.rb
75
+ - lib/monkey/watcher.rb
56
76
  - lib/monkey-lib.rb
57
77
  - lib/monkey.rb
58
78
  - spec/monkey/engine_spec.rb
data/lib/monkey/hash.rb DELETED
@@ -1,4 +0,0 @@
1
- require "extlib/class"
2
- require "extlib/hash"
3
- require "extlib/mash"
4
- require "monkey/hash/sub_hash"
@@ -1,23 +0,0 @@
1
- module Monkey
2
- module Hash
3
-
4
- # Adds sub hash ablilty (like Hash#[], but returning a hash rather than an array).
5
- module SubHash
6
-
7
- ::Hash.send :include, self
8
-
9
- # Example:
10
- # {:a => 42, :b => 23, :c => 1337}.sub_hash :a, :b, :d
11
- # # => {:a => 42, :b => 23}
12
- def sub_hash(*keys)
13
- keys.inject({}) do |hash, key|
14
- hash.merge! key => self[key] if include? key
15
- hash
16
- end
17
- end
18
-
19
- alias subhash sub_hash
20
-
21
- end
22
- end
23
- end
data/lib/monkey/object.rb DELETED
@@ -1,2 +0,0 @@
1
- require "monkey/object/backports"
2
- require "monkey/object/instance_yield"
@@ -1,24 +0,0 @@
1
- module Monkey
2
- module Object
3
-
4
- # Backports from newer / alternate ruby implementations.
5
- module Backports
6
-
7
- ::Object.send :include, self
8
-
9
- # From Ruby >= 1.8.7
10
- def tap
11
- yield self
12
- self
13
- end
14
-
15
- # From Rubinius
16
- def metaclass
17
- class << self
18
- self
19
- end
20
- end
21
-
22
- end
23
- end
24
- end
@@ -1,38 +0,0 @@
1
- module Monkey
2
- module Object
3
-
4
- # Adds the private method instance_eval which is like instance_eval
5
- # and yield depending on whether a block takes an argument or not.
6
- #
7
- # class Foo
8
- #
9
- # def foo
10
- # 42
11
- # end
12
- #
13
- # def bar(&block)
14
- # instance_yield block
15
- # end
16
- #
17
- # end
18
- #
19
- # conf = Foo.new
20
- # conf.bar { foo }
21
- # conf.bar { |c| c.foo }
22
- module InstanceYield
23
-
24
- ::Object.send :include, self
25
-
26
- private
27
-
28
- # See InstanceYield description.
29
- def instance_yield(block = nil, &alternate_block)
30
- raise ArgumentError, "too many blocks given" if block && alternate_block
31
- block ||= alternate_block
32
- raise LocalJumpError, "no block given (yield)" unless block
33
- block.arity > 0 ? yield(self) : instance_eval(&block)
34
- end
35
-
36
- end
37
- end
38
- end
data/lib/monkey/string.rb DELETED
@@ -1,2 +0,0 @@
1
- require "extlib/string"
2
- require "monkey/string/like_pathname"