ok-extensions 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ == 1.0.1
2
+
3
+ * New version
@@ -0,0 +1,25 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/extensions/array.rb
6
+ lib/extensions/core.rb
7
+ lib/extensions/enumerable.rb
8
+ lib/extensions/exception.rb
9
+ lib/extensions/file.rb
10
+ lib/extensions/fixnum.rb
11
+ lib/extensions/hash.rb
12
+ lib/extensions/object.rb
13
+ lib/extensions/range.rb
14
+ lib/extensions/yaml.rb
15
+ lib/ok_extensions.rb
16
+ test/extensions/test_array.rb
17
+ test/extensions/test_enumerable.rb
18
+ test/extensions/test_exception.rb
19
+ test/extensions/test_file.rb
20
+ test/extensions/test_fixnum.rb
21
+ test/extensions/test_hash.rb
22
+ test/extensions/test_object.rb
23
+ test/extensions/test_range.rb
24
+ test/extensions/test_yaml.rb
25
+ test/focused_test_runner.rb
@@ -0,0 +1,46 @@
1
+ OkExtensions
2
+ by oksteevx
3
+ http://code.google.com/p/ok-extensions/
4
+
5
+ == DESCRIPTION:
6
+
7
+ A set of core extensions to the ruby stdlib
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+
12
+ == SYNOPSIS:
13
+
14
+
15
+ == REQUIREMENTS:
16
+
17
+ * facets
18
+
19
+ == INSTALL:
20
+
21
+ gem install ok_extensions
22
+
23
+ == LICENSE:
24
+
25
+ (The MIT License)
26
+
27
+ Copyright (c) 2007 FIX
28
+
29
+ Permission is hereby granted, free of charge, to any person obtaining
30
+ a copy of this software and associated documentation files (the
31
+ 'Software'), to deal in the Software without restriction, including
32
+ without limitation the rights to use, copy, modify, merge, publish,
33
+ distribute, sublicense, and/or sell copies of the Software, and to
34
+ permit persons to whom the Software is furnished to do so, subject to
35
+ the following conditions:
36
+
37
+ The above copyright notice and this permission notice shall be
38
+ included in all copies or substantial portions of the Software.
39
+
40
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
41
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
42
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
43
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
44
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
45
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
46
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,16 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/ok_extensions.rb'
6
+
7
+ Hoe.new('ok-extensions', OkExtensions::VERSION) do |p|
8
+ p.rubyforge_name = 'coderrr'
9
+ # p.author = 'FIX'
10
+ # p.email = 'FIX'
11
+ # p.summary = 'FIX'
12
+ p.description = p.paragraphs_of('README.txt', 0..1).join("\n\n")
13
+ # p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
14
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
15
+ p.remote_rdoc_dir = '' # Release to root
16
+ end
@@ -0,0 +1,14 @@
1
+ class Array
2
+ def flatten_once
3
+ flattened = []
4
+ self.each do |e|
5
+ if e.is_a? Array
6
+ flattened += e
7
+ else
8
+ flattened << e
9
+ end
10
+ end
11
+
12
+ flattened
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ # speed up reading
2
+ class BufferedIO
3
+ private
4
+
5
+ def rbuf_fill
6
+ timeout(@read_timeout) do
7
+ @rbuf << @io.sysread(16384)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ module Enumerable
2
+ def sum_with(starting_value = 0)
3
+ self.inject(starting_value) {|sum, current_value| sum + current_value }
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ require 'facets'
2
+ require 'kernel/pp_exception'
3
+
4
+ class Exception
5
+ def to_pretty
6
+ "#{description}: #{ pp_exception self }"
7
+ end
8
+
9
+ def description
10
+ "#{self.class}/#{self.message}"
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ class << File
2
+ def write(filename, data)
3
+ File.open(filename, "wb") {|f| f.write data }
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ class Fixnum
2
+ def tries(*options)
3
+ rescueable_types = []
4
+ fail_proc = nil
5
+
6
+ options.each do |option|
7
+ rescueable_types << option if option.is_a? Class and option < Exception
8
+ fail_proc = option if option.is_a? Proc
9
+ end
10
+
11
+ rescueable_types << Exception if rescueable_types.empty?
12
+
13
+ self.times do |i|
14
+ begin
15
+ return yield
16
+ rescue *rescueable_types
17
+ raise if (self-1) == i
18
+ fail_proc.call if fail_proc
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,9 @@
1
+ if {:a=>:b}.select{true}.is_a? Array
2
+ class Hash
3
+ alias :old_select :select
4
+
5
+ def select(*a, &block)
6
+ Hash[*old_select(*a, &block).flatten]
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,37 @@
1
+ require 'singleton'
2
+ require 'facets'
3
+ require 'basicobject'
4
+
5
+ # SafeNil
6
+ class Object
7
+ def _?
8
+ self
9
+ end
10
+ end
11
+
12
+ class NilClass
13
+ def _?
14
+ SafeNil.instance
15
+ end
16
+ end
17
+
18
+ class SafeNil < BasicObject
19
+ include Singleton
20
+
21
+ undef inspect
22
+
23
+ def method_missing(method, *args, &b)
24
+ return nil unless nil.respond_to? method
25
+ nil.send(method, *args, &b) rescue nil
26
+ end
27
+ end
28
+
29
+ # me, myself, and i
30
+ class Object
31
+ def me
32
+ yield self
33
+ end
34
+
35
+ alias :myself :me
36
+ alias :i :me
37
+ end
@@ -0,0 +1,7 @@
1
+ class Range
2
+ def rand
3
+ length = last - first
4
+
5
+ (Kernel.rand*length) + first
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ require 'yaml'
2
+
3
+ require 'lib/extensions/file'
4
+
5
+ module YAML
6
+ def self.dump_file(filename, object)
7
+ File.write(filename, object.to_yaml)
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ class OkExtensions
2
+ VERSION = '1.0.1'
3
+ end
4
+
5
+ require 'lib/extensions/core'
6
+ require 'lib/extensions/array'
7
+ require 'lib/extensions/enumerable'
8
+ require 'lib/extensions/exception'
9
+ require 'lib/extensions/file'
10
+ require 'lib/extensions/fixnum'
11
+ require 'lib/extensions/hash'
12
+ require 'lib/extensions/object'
13
+ require 'lib/extensions/range'
14
+ require 'lib/extensions/yaml'
@@ -0,0 +1,12 @@
1
+ require 'test/unit'
2
+ require 'benchmark'
3
+
4
+ require 'lib/extensions/array'
5
+
6
+ class TestArray < Test::Unit::TestCase
7
+ def test_flatten_once
8
+ a = [1, [2, 3, [4, 5, 6, [7, 8, 9]]], [10, 11, 12], 13, 14, 15]
9
+
10
+ assert_equal [1, 2, 3, [4, 5, 6, [7, 8, 9]], 10, 11, 12, 13, 14, 15], a.flatten_once
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ require 'test/unit'
2
+ require 'benchmark'
3
+
4
+ require 'lib/extensions/enumerable'
5
+
6
+ class TestEnumerable < Test::Unit::TestCase
7
+ def test_sum_with
8
+ assert_equal "Hello World", ["Hell", "o W", "orld"].sum_with("")
9
+ assert_equal [1,2,3,4,5], [[1],[2],[3],[4],[5]].sum_with([])
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ require 'test/unit'
2
+ require 'benchmark'
3
+
4
+ require 'lib/extensions/exception'
5
+
6
+ class TestException < Test::Unit::TestCase
7
+ def test_exception
8
+ e = nil
9
+ raise "cool exception" rescue e = $!
10
+
11
+ assert_equal "RuntimeError/cool exception", e.description
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ require 'test/unit'
2
+
3
+ require 'lib/extensions/file'
4
+
5
+ class TestFile < Test::Unit::TestCase
6
+ def test_write
7
+ fn = "tmp.#{$$}"
8
+ s = "this is the hit\n"
9
+ File.write(fn, s)
10
+ assert_equal s, File.read(fn)
11
+ ensure
12
+ File.delete fn
13
+ end
14
+ end
@@ -0,0 +1,37 @@
1
+ require 'test/unit'
2
+ require 'timeout'
3
+
4
+ require 'lib/extensions/fixnum'
5
+
6
+ class TestFixnum < Test::Unit::TestCase
7
+ def test_tries
8
+ i = 0
9
+ 10.tries do
10
+ if i < 5
11
+ i += 1
12
+ raise "WTF!"
13
+ end
14
+ end
15
+
16
+ assert_equal 5, i
17
+
18
+ i = 0
19
+ assert_raises(Timeout::Error) do
20
+ 10.tries do
21
+ i += 1
22
+ raise Timeout::Error, 'huh'
23
+ end
24
+ end
25
+ assert_equal 10, i
26
+
27
+ i = 0
28
+
29
+ assert_raises(StandardError) do
30
+ 10.tries(Timeout::Error) do
31
+ i += 1
32
+ raise StandardError
33
+ end
34
+ end
35
+ assert_equal 1, i
36
+ end
37
+ end
@@ -0,0 +1,11 @@
1
+ require 'test/unit'
2
+ require 'benchmark'
3
+
4
+ require 'lib/extensions/hash'
5
+
6
+ class TestHash < Test::Unit::TestCase
7
+ def test_select
8
+ assert_equal Hash, {:a => :b}.select { true }.class
9
+ assert_equal Hash, {:a => :b}.select { false }.class
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ require 'test/unit'
2
+ require 'benchmark'
3
+
4
+ require 'lib/extensions/object'
5
+
6
+ class TestObject < Test::Unit::TestCase
7
+ def test_safe_nil
8
+ assert_not_nil 5._?.times { }
9
+ assert_nil nil._?.times {}
10
+ assert_equal "", nil._?.to_s
11
+ assert_equal "nil", nil._?.inspect
12
+ assert_nil nil._?.abcdefg
13
+ assert_raises(NoMethodError) { "abc"._?.abcdefg }
14
+ end
15
+
16
+ def test_me_myself_and_i
17
+ o = "abc"
18
+ [:me, :myself, :i].each do |m|
19
+ o.send(m) { |v| assert_equal o, v }
20
+ end
21
+ end
22
+
23
+ def explicit_test_benchmark_safe_nil
24
+ Benchmark.bm do |x|
25
+ x.report { 10000.times { nil._?.abc } }
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,11 @@
1
+ require 'test/unit'
2
+ require 'lib/extensions/range'
3
+
4
+ class TestRange < Test::Unit::TestCase
5
+ def test_rand
6
+ range = (1..10)
7
+ 1000.times do
8
+ assert range.include?(range.rand)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ require 'test/unit'
2
+
3
+ require 'lib/extensions/yaml'
4
+
5
+ class TestYaml < Test::Unit::TestCase
6
+ def test_dump_file
7
+ fn = "tmp.#{$$}"
8
+ h = {:a => :b}
9
+
10
+ YAML.dump_file(fn, h)
11
+ assert_equal h, YAML.load_file(fn)
12
+ ensure
13
+ File.delete fn
14
+ end
15
+ end
@@ -0,0 +1,79 @@
1
+ #!/usr/local/bin/ruby
2
+ require 'optparse'
3
+
4
+
5
+ # ghetto hack to run focused tests that don't start with test_
6
+ require 'test/unit/testcase'
7
+ module Test
8
+ module Unit
9
+ class TestCase
10
+
11
+ def self.suite
12
+ method_names = public_instance_methods(true)
13
+ tests = method_names.delete_if {|method_name| method_name !~ /^(?:explicit_)?test./}
14
+ suite = TestSuite.new(name)
15
+ tests.sort.each do
16
+ |test|
17
+ catch(:invalid_test) do
18
+ suite << new(test)
19
+ end
20
+ end
21
+ if (suite.empty?)
22
+ catch(:invalid_test) do
23
+ suite << new(:default_test)
24
+ end
25
+ end
26
+ return suite
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+
33
+
34
+ file_path = nil
35
+ line_number = 0
36
+ options = OptionParser.new do |o|
37
+ o.on('-f', '--filepath=FILEPATH', String, "File to run test on") do |path|
38
+ file_path = path
39
+ end
40
+
41
+ o.on('-l', '--linenumber=LINENUMBER', Integer, "Line of the test") do |line|
42
+ line_number = line
43
+ end
44
+ end
45
+ options.order(ARGV)
46
+
47
+ test_type = nil
48
+ current_method = nil
49
+ current_line = 0
50
+ IO.foreach(file_path) do |line|
51
+ break if current_line > line_number
52
+ if /def +((?:explicit_)?test_[a-z0-9_!?]*)/i =~ line
53
+ test_type = :test
54
+ current_method = Regexp.last_match(1)
55
+ elsif /^ *(specify +.*)$/ =~ line
56
+ test_type = :spec
57
+ current_method = Regexp.last_match(1)
58
+ end
59
+ current_line += 1
60
+ end
61
+
62
+ if test_type == :test
63
+ require file_path
64
+ runner = Test::Unit::AutoRunner.new(false) do |runner|
65
+ runner.filters << proc{|t| current_method == t.method_name }
66
+ end
67
+ runner.run
68
+ elsif test_type == :spec
69
+ def specify(spec_name)
70
+ @spec_name = spec_name
71
+ end
72
+ current_method.gsub!(/do *$/, "")
73
+ current_method.gsub!(/\{ *$/, "")
74
+ eval(current_method) rescue ""
75
+ current_method = @spec_name
76
+ exec "spec '#{file_path}' -s '#{current_method}' -f specdoc"
77
+ end
78
+
79
+ puts "Running '#{current_method}' in file #{file_path}"
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: ok-extensions
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.1
7
+ date: 2007-11-25 00:00:00 +07:00
8
+ summary: The author was too lazy to write a summary
9
+ require_paths:
10
+ - lib
11
+ email: ryand-ruby@zenspider.com
12
+ homepage: http://www.zenspider.com/ZSS/Products/ok-extensions/
13
+ rubyforge_project: coderrr
14
+ description: "OkExtensions by oksteevx http://code.google.com/p/ok-extensions/ == DESCRIPTION: A set of core extensions to the ruby stdlib"
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Ryan Davis
31
+ files:
32
+ - History.txt
33
+ - Manifest.txt
34
+ - README.txt
35
+ - Rakefile
36
+ - lib/extensions/array.rb
37
+ - lib/extensions/core.rb
38
+ - lib/extensions/enumerable.rb
39
+ - lib/extensions/exception.rb
40
+ - lib/extensions/file.rb
41
+ - lib/extensions/fixnum.rb
42
+ - lib/extensions/hash.rb
43
+ - lib/extensions/object.rb
44
+ - lib/extensions/range.rb
45
+ - lib/extensions/yaml.rb
46
+ - lib/ok_extensions.rb
47
+ - test/extensions/test_array.rb
48
+ - test/extensions/test_enumerable.rb
49
+ - test/extensions/test_exception.rb
50
+ - test/extensions/test_file.rb
51
+ - test/extensions/test_fixnum.rb
52
+ - test/extensions/test_hash.rb
53
+ - test/extensions/test_object.rb
54
+ - test/extensions/test_range.rb
55
+ - test/extensions/test_yaml.rb
56
+ - test/focused_test_runner.rb
57
+ test_files:
58
+ - test/extensions/test_array.rb
59
+ - test/extensions/test_hash.rb
60
+ - test/extensions/test_exception.rb
61
+ - test/extensions/test_object.rb
62
+ - test/extensions/test_enumerable.rb
63
+ - test/extensions/test_yaml.rb
64
+ - test/extensions/test_file.rb
65
+ - test/extensions/test_fixnum.rb
66
+ - test/extensions/test_range.rb
67
+ rdoc_options:
68
+ - --main
69
+ - README.txt
70
+ extra_rdoc_files:
71
+ - History.txt
72
+ - Manifest.txt
73
+ - README.txt
74
+ executables: []
75
+
76
+ extensions: []
77
+
78
+ requirements: []
79
+
80
+ dependencies:
81
+ - !ruby/object:Gem::Dependency
82
+ name: hoe
83
+ version_requirement:
84
+ version_requirements: !ruby/object:Gem::Version::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 1.3.0
89
+ version: