rkh-monkey-lib 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ copyright (c) 2009 Konstantin Haase. All rights reserved.
2
+
3
+ Developed by: Konstantin Haase
4
+ http://github.com/rkh/monkey-lib
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to
8
+ deal with the Software without restriction, including without limitation the
9
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10
+ sell copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+ 1. Redistributions of source code must retain the above copyright notice,
13
+ this list of conditions and the following disclaimers.
14
+ 2. Redistributions in binary form must reproduce the above copyright
15
+ notice, this list of conditions and the following disclaimers in the
16
+ documentation and/or other materials provided with the distribution.
17
+ 3. Neither the name of Konstantin Haase, nor the names of other contributors
18
+ may be used to endorse or promote products derived from this Software without
19
+ specific prior written permission.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27
+ WITH THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,30 @@
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
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ require "spec/rake/spectask"
2
+ require "rake/clean"
3
+ require "rake/rdoctask"
4
+ require "rake/gempackagetask"
5
+ require "rubygems/specification"
6
+
7
+ MONKEY_VERSION = "0.1.4"
8
+
9
+ spec = Gem::Specification.new do |s|
10
+ s.name = "monkey-lib"
11
+ s.version = MONKEY_VERSION
12
+ s.authors = ["Konstantin Haase"]
13
+ s.description = "collection of sane ruby extensions"
14
+ s.email = "konstantin.mailinglists@googlemail.com"
15
+ s.extra_rdoc_files = Dir["*.rdoc", "LICENSE", "lib/**/*.rb"]
16
+ s.files = Dir["LICENSE", "Rakefile", "README.rdoc", "lib/**/*.rb", "spec/**/*.rb"]
17
+ s.has_rdoc = true
18
+ s.homepage = "http://github.com/rkh/monkey-lib"
19
+ s.require_paths = ["lib"]
20
+ s.summary = s.description
21
+ s.add_dependency "extlib"
22
+ end
23
+
24
+ Rake::GemPackageTask.new(spec) do |pkg|
25
+ pkg.gem_spec = spec
26
+ end
27
+
28
+ desc "install the gem locally"
29
+ task :install => [:package] do
30
+ sh %{gem install pkg/monkey-lib-#{MONKEY_VERSION}.gem}
31
+ end
32
+
33
+ desc "create a gemspec file"
34
+ task :make_spec do
35
+ File.open("monkey-lib.gemspec", "w") do |file|
36
+ file.puts spec.to_ruby
37
+ end
38
+ end
39
+
40
+ Rake::RDocTask.new("doc") do |rdoc|
41
+ rdoc.rdoc_dir = 'doc'
42
+ rdoc.options += %w[--all --inline-source --line-numbers --main README.rdoc --quiet
43
+ --tab-width 2 --title monkey-lib]
44
+ rdoc.rdoc_files.add ['*.rdoc', 'lib/**/*.rb']
45
+ end
46
+
47
+ Spec::Rake::SpecTask.new('spec') do |t|
48
+ t.spec_files = Dir.glob 'spec/**/*_spec.rb'
49
+ end
data/lib/monkey-lib.rb ADDED
@@ -0,0 +1 @@
1
+ require "monkey"
data/lib/monkey.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "monkey/object"
2
+ require "monkey/hash"
3
+ require "monkey/string"
4
+ require "monkey/engine"
@@ -0,0 +1,89 @@
1
+ module Monkey
2
+
3
+ # Makes sure we always have RUBY_ENGINE, RUBY_ENGINE_VERSION and RUBY_DESCRIPTION
4
+ module Engine
5
+
6
+ include Rubinius if defined? Rubinius
7
+
8
+ 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"
15
+ end
16
+ end
17
+
18
+ unless RUBY_ENGINE.frozen?
19
+ RUBY_ENGINE.replace "rbx" if RUBY_ENGINE == "rubinius"
20
+ RUBY_ENGINE.downcase!
21
+ RUBY_ENGINE.freeze
22
+ end
23
+
24
+ ::RUBY_ENGINE_VERSION = const_get("#{RUBY_ENGINE.upcase}_VERSION")
25
+
26
+ unless defined? RUBY_DESCRIPTION
27
+ ::RUBY_DESCRIPTION = "#{RUBY_ENGINE} #{RUBY_ENGINE_VERSION} "
28
+ unless RUBY_ENGINE == "ruby"
29
+ ::RUBY_DESCRIPTION << "(ruby #{RUBY_VERSION}"
30
+ ::RUBY_DESCRIPTION << " patchlevel #{RUBY_PATCHLEVEL}" if defined? RUBY_PATCHLEVEL
31
+ ::RUBY_DESCRIPTION << ") "
32
+ end
33
+ if defined? RUBY_RELEASE_DATE
34
+ ::RUBY_DESCRIPTION << "("
35
+ ::RUBY_DESCRIPTION << BUILDREV[0..8] << " " if defined? BUILDREV
36
+ ::RUBY_DESCRIPTION << RUBY_RELEASE_DATE << ") "
37
+ end
38
+ ::RUBY_DESCRIPTION << "[#{RUBY_PLATFORM}]"
39
+ end
40
+
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
+ def ruby_engine(pretty = true)
74
+ return RUBY_ENGINE unless pretty
75
+ case RUBY_ENGINE
76
+ when "ruby" then "CRuby"
77
+ when "rbx" then "Rubinius"
78
+ when /ruby$/ then RUBY_ENGINE.capitalize.gsub("ruby", "Ruby")
79
+ end
80
+ end
81
+
82
+ module_function :ruby_engine
83
+
84
+ end
85
+
86
+ extend Engine
87
+ include Engine
88
+
89
+ end
@@ -0,0 +1,4 @@
1
+ require "extlib/class"
2
+ require "extlib/hash"
3
+ require "extlib/mash"
4
+ require "monkey/hash/sub_hash"
@@ -0,0 +1,23 @@
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
@@ -0,0 +1 @@
1
+ require "monkey/object/backports"
@@ -0,0 +1,24 @@
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
@@ -0,0 +1,2 @@
1
+ require "extlib/string"
2
+ require "monkey/string/like_pathname"
@@ -0,0 +1,36 @@
1
+ require "pathname"
2
+ require "extlib/pathname"
3
+
4
+ module Monkey
5
+ module String
6
+
7
+ # Adds Pathname behaviour to String.
8
+ module LikePathname
9
+
10
+ ::String.send :include, self
11
+
12
+ Pathname.instance_methods(false).each do |m|
13
+ # Syntax error in Ruby < 1.9:
14
+ # define_method(m) { |*a, &b| ... }
15
+ next if "".respond_to? m or m.to_s == "to_path"
16
+ class_eval <<-EOS
17
+ def #{m}(*a, &b)
18
+ result = Pathname(self).#{m}(*a, &b)
19
+ return result unless result.is_a? Pathname
20
+ result.to_s
21
+ end
22
+ EOS
23
+ end
24
+
25
+ #def method_missing(meth, *args, &block)
26
+ # pathname = Pathname self
27
+ # return super unless pathname.respond_to? meth
28
+ # result = pathname.send(meth, *args, &block)
29
+ # return result.to_s if result.is_a? Pathname
30
+ # result
31
+ #end
32
+
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,18 @@
1
+ require __FILE__.sub("monkey/engine_spec.rb", "spec_helper")
2
+ require "monkey/engine"
3
+
4
+ describe Monkey::Engine do
5
+
6
+ it "defines RUBY_VERSION" do
7
+ defined?(RUBY_VERSION).should == "constant"
8
+ end
9
+
10
+ it "defines RUBY_ENGINE_VERSION" do
11
+ defined?(RUBY_ENGINE_VERSION).should == "constant"
12
+ end
13
+
14
+ it "defines RUBY_DESCRIPTION" do
15
+ defined?(RUBY_DESCRIPTION).should == "constant"
16
+ end
17
+
18
+ end
@@ -0,0 +1,15 @@
1
+ require __FILE__.sub("monkey/hash/sub_hash_spec.rb", "spec_helper")
2
+ require "monkey/hash/sub_hash"
3
+
4
+ describe Monkey::Hash::SubHash do
5
+
6
+ it "returns a hash with only the key-value-pairs requested for sub_hash" do
7
+ {:a => 10, :b => 20}.sub_hash(:a).should == {:a => 10}
8
+ {:a => 10, :b => 20}.sub_hash(:a, :b).should == {:a => 10, :b => 20}
9
+ end
10
+
11
+ it "should ignore keys passed to sub_hash, but not present" do
12
+ {:a => 10, :b => 20}.sub_hash(:a, :c).should == {:a => 10}
13
+ end
14
+
15
+ end
@@ -0,0 +1,24 @@
1
+ require __FILE__.sub("monkey/object/backports_spec.rb", "spec_helper")
2
+ require "monkey/object/backports"
3
+
4
+ describe Monkey::Object::Backports do
5
+
6
+ it "returns returns the reciever of #tap" do
7
+ obj = Object.new
8
+ obj.tap {}.should == obj
9
+ "example".tap {}.should == "example"
10
+ end
11
+
12
+ it "passes the reciever of #tap to the given block" do
13
+ "foo".tap { |s| s.replace "bar" }.should == "bar"
14
+ end
15
+
16
+ it "returns eigenclass for #metaclass" do
17
+ obj = Object.new
18
+ eigenclass = class << obj
19
+ self
20
+ end
21
+ obj.metaclass.should == eigenclass
22
+ end
23
+
24
+ end
@@ -0,0 +1,271 @@
1
+ require __FILE__.sub("monkey/string/like_pathname_spec.rb", "spec_helper")
2
+ require "monkey/string/like_pathname"
3
+
4
+ describe Monkey::String::LikePathname do
5
+
6
+ before do
7
+ @strings = ["/usr/bin/ruby", ".", "..", ENV["HOME"]]
8
+ @methods = Pathname.instance_methods(false).reject do |m|
9
+ String.methods.include? m or m.to_s == "to_path"
10
+ end
11
+ end
12
+
13
+ it "exposes sub_ext to String" do
14
+ @strings.each do |s|
15
+ s.sub_ext(".png").should == Pathname(s).sub_ext(".png").to_s
16
+ end
17
+ end
18
+
19
+ it "exposes cleanpath to String" do
20
+ @strings.each do |s|
21
+ s.cleanpath.should == Pathname(s).cleanpath.to_s
22
+ end
23
+ end
24
+
25
+ it "exposes realpath to String" do
26
+ @strings.each do |s|
27
+ s.realpath.should == Pathname(s).realpath.to_s
28
+ end
29
+ end
30
+
31
+ it "exposes parent to String" do
32
+ @strings.each do |s|
33
+ s.parent.should == Pathname(s).parent.to_s
34
+ end
35
+ end
36
+
37
+ it "exposes mountpoint? to String" do
38
+ @strings.each do |s|
39
+ s.mountpoint?.should == Pathname(s).mountpoint?
40
+ end
41
+ end
42
+
43
+ it "exposes root? to String" do
44
+ @strings.each do |s|
45
+ s.root?.should == Pathname(s).root?
46
+ end
47
+ end
48
+
49
+ it "exposes absolute? to String" do
50
+ @strings.each do |s|
51
+ s.absolute?.should == Pathname(s).absolute?
52
+ end
53
+ end
54
+
55
+ it "exposes relative? to String" do
56
+ @strings.each do |s|
57
+ s.relative?.should == Pathname(s).relative?
58
+ end
59
+ end
60
+
61
+ it "exposes each_filename to String" do
62
+ @strings.each do |s|
63
+ s.each_filename.to_a.should == Pathname(s).each_filename.to_a
64
+ end
65
+ end
66
+
67
+ it "exposes join to String" do
68
+ @strings.each do |s|
69
+ s.join.should == Pathname(s).join.to_s
70
+ end
71
+ end
72
+
73
+ it "exposes children to String" do
74
+ @strings.each do |s|
75
+ s.children.should == Pathname(s).children if s.directory?
76
+ end
77
+ end
78
+
79
+ it "exposes atime to String" do
80
+ @strings.each do |s|
81
+ s.atime.should == Pathname(s).atime
82
+ end
83
+ end
84
+
85
+ it "exposes ctime to String" do
86
+ @strings.each do |s|
87
+ s.ctime.should == Pathname(s).ctime
88
+ end
89
+ end
90
+
91
+ it "exposes mtime to String" do
92
+ @strings.each do |s|
93
+ s.mtime.should == Pathname(s).mtime
94
+ end
95
+ end
96
+
97
+ it "exposes ftype to String" do
98
+ @strings.each do |s|
99
+ s.ftype.should == Pathname(s).ftype.to_s
100
+ end
101
+ end
102
+
103
+ it "exposes basename to String" do
104
+ @strings.each do |s|
105
+ s.basename.should == Pathname(s).basename.to_s
106
+ end
107
+ end
108
+
109
+ it "exposes dirname to String" do
110
+ @strings.each do |s|
111
+ s.dirname.should == Pathname(s).dirname.to_s
112
+ end
113
+ end
114
+
115
+ it "exposes extname to String" do
116
+ @strings.each do |s|
117
+ s.extname.should == Pathname(s).extname.to_s
118
+ end
119
+ end
120
+
121
+ it "exposes expand_path to String" do
122
+ @strings.each do |s|
123
+ s.expand_path.should == Pathname(s).expand_path.to_s
124
+ end
125
+ end
126
+
127
+ it "exposes blockdev? to String" do
128
+ @strings.each do |s|
129
+ s.blockdev?.should == Pathname(s).blockdev?
130
+ end
131
+ end
132
+
133
+ it "exposes chardev? to String" do
134
+ @strings.each do |s|
135
+ s.chardev?.should == Pathname(s).chardev?
136
+ end
137
+ end
138
+
139
+ it "exposes executable? to String" do
140
+ @strings.each do |s|
141
+ s.executable?.should == Pathname(s).executable?
142
+ end
143
+ end
144
+
145
+ it "exposes executable_real? to String" do
146
+ @strings.each do |s|
147
+ s.executable_real?.should == Pathname(s).executable_real?
148
+ end
149
+ end
150
+
151
+ it "exposes exist? to String" do
152
+ @strings.each do |s|
153
+ s.exist?.should == Pathname(s).exist?
154
+ end
155
+ end
156
+
157
+ it "exposes grpowned? to String" do
158
+ @strings.each do |s|
159
+ s.grpowned?.should == Pathname(s).grpowned?
160
+ end
161
+ end
162
+
163
+ it "exposes directory? to String" do
164
+ @strings.each do |s|
165
+ s.directory?.should == Pathname(s).directory?
166
+ end
167
+ end
168
+
169
+ it "exposes file? to String" do
170
+ @strings.each do |s|
171
+ s.file?.should == Pathname(s).file?
172
+ end
173
+ end
174
+
175
+ it "exposes pipe? to String" do
176
+ @strings.each do |s|
177
+ s.pipe?.should == Pathname(s).pipe?
178
+ end
179
+ end
180
+
181
+ it "exposes socket? to String" do
182
+ @strings.each do |s|
183
+ s.socket?.should == Pathname(s).socket?
184
+ end
185
+ end
186
+
187
+ it "exposes owned? to String" do
188
+ @strings.each do |s|
189
+ s.owned?.should == Pathname(s).owned?
190
+ end
191
+ end
192
+
193
+ it "exposes readable? to String" do
194
+ @strings.each do |s|
195
+ s.readable?.should == Pathname(s).readable?
196
+ end
197
+ end
198
+
199
+ it "exposes world_readable? to String" do
200
+ @strings.each do |s|
201
+ s.world_readable?.should == Pathname(s).world_readable?
202
+ end
203
+ end
204
+
205
+ it "exposes readable_real? to String" do
206
+ @strings.each do |s|
207
+ s.readable_real?.should == Pathname(s).readable_real?
208
+ end
209
+ end
210
+
211
+ it "exposes setuid? to String" do
212
+ @strings.each do |s|
213
+ s.setuid?.should == Pathname(s).setuid?
214
+ end
215
+ end
216
+
217
+ it "exposes setgid? to String" do
218
+ @strings.each do |s|
219
+ s.setgid?.should == Pathname(s).setgid?
220
+ end
221
+ end
222
+
223
+ it "exposes size? to String" do
224
+ @strings.each do |s|
225
+ s.size?.should == Pathname(s).size?
226
+ end
227
+ end
228
+
229
+ it "exposes sticky? to String" do
230
+ @strings.each do |s|
231
+ s.sticky?.should == Pathname(s).sticky?
232
+ end
233
+ end
234
+
235
+ it "exposes symlink? to String" do
236
+ @strings.each do |s|
237
+ s.symlink?.should == Pathname(s).symlink?
238
+ end
239
+ end
240
+
241
+ it "exposes writable? to String" do
242
+ @strings.each do |s|
243
+ s.writable?.should == Pathname(s).writable?
244
+ end
245
+ end
246
+
247
+ it "exposes world_writable? to String" do
248
+ @strings.each do |s|
249
+ s.world_writable?.should == Pathname(s).world_writable?
250
+ end
251
+ end
252
+
253
+ it "exposes writable_real? to String" do
254
+ @strings.each do |s|
255
+ s.writable_real?.should == Pathname(s).writable_real?
256
+ end
257
+ end
258
+
259
+ it "exposes zero? to String" do
260
+ @strings.each do |s|
261
+ s.zero?.should == Pathname(s).zero?
262
+ end
263
+ end
264
+
265
+ it "exposes entries to String" do
266
+ @strings.each do |s|
267
+ s.entries.should == Pathname(s).entries if s.directory?
268
+ end
269
+ end
270
+
271
+ end
@@ -0,0 +1 @@
1
+ $LOAD_PATH.unshift File.expand_path(__FILE__.sub("spec/spec_helper.rb", "lib"))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rkh-monkey-lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
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-05-16 00:00:00 -07:00
12
+ date: 2009-07-13 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -28,11 +28,37 @@ executables: []
28
28
 
29
29
  extensions: []
30
30
 
31
- extra_rdoc_files: []
32
-
33
- files: []
34
-
35
- has_rdoc: true
31
+ extra_rdoc_files:
32
+ - README.rdoc
33
+ - LICENSE
34
+ - 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.rb
39
+ - lib/monkey/string/like_pathname.rb
40
+ - lib/monkey/string.rb
41
+ - lib/monkey-lib.rb
42
+ - lib/monkey.rb
43
+ files:
44
+ - LICENSE
45
+ - Rakefile
46
+ - README.rdoc
47
+ - lib/monkey/engine.rb
48
+ - lib/monkey/hash/sub_hash.rb
49
+ - lib/monkey/hash.rb
50
+ - lib/monkey/object/backports.rb
51
+ - lib/monkey/object.rb
52
+ - lib/monkey/string/like_pathname.rb
53
+ - lib/monkey/string.rb
54
+ - lib/monkey-lib.rb
55
+ - lib/monkey.rb
56
+ - spec/monkey/engine_spec.rb
57
+ - spec/monkey/hash/sub_hash_spec.rb
58
+ - spec/monkey/object/backports_spec.rb
59
+ - spec/monkey/string/like_pathname_spec.rb
60
+ - spec/spec_helper.rb
61
+ has_rdoc: false
36
62
  homepage: http://github.com/rkh/monkey-lib
37
63
  post_install_message:
38
64
  rdoc_options: []
@@ -56,7 +82,7 @@ requirements: []
56
82
  rubyforge_project:
57
83
  rubygems_version: 1.2.0
58
84
  signing_key:
59
- specification_version: 2
85
+ specification_version: 3
60
86
  summary: collection of sane ruby extensions
61
87
  test_files: []
62
88