ruby_patch 0.0.0

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,14 @@
1
+ module RubyPatch
2
+ class ::Class
3
+ def children()
4
+ posterities.select{|c|
5
+ c.superclass == self
6
+ }
7
+ end
8
+
9
+ def posterities()
10
+ classes = ObjectSpace.each_object(Class)\
11
+ .select{|c| c.ancestors.include?(self)} - [self]
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,42 @@
1
+ module RubyPatch
2
+ class ::Object
3
+
4
+ # @return [Hash]
5
+ def parse_caller(backtrace)
6
+ if /\A(.+?):(\d+)(?::in `(.*)')?/ =~ backtrace
7
+ file = $1
8
+ line = $2.to_i
9
+ method = $3
10
+ {
11
+ file: file,
12
+ line: line,
13
+ method: method,
14
+ }
15
+ end
16
+ end
17
+
18
+ def __DIR__()
19
+ called_from = parse_caller(caller(1).first)[:file]
20
+ dir = File.dirname(called_from)
21
+ File.expand_path(dir)
22
+ end
23
+
24
+ # Create a deep clone by using Marshal.
25
+ def deep_clone()
26
+ Marshal.load(Marshal.dump(self))
27
+ end
28
+
29
+ # Check type of var.
30
+ # +allow+ is an array which contains Symbol, String or Class object which represents allowable classes.
31
+ # +allow+ can be nested array because they are flattened.
32
+ def check_type(var, *allow)
33
+ if allow.flatten\
34
+ .map{|klass| klass.to_s.to_sym}\
35
+ .include?(var.class.to_s.to_sym)
36
+ var
37
+ else
38
+ raise TypeError, "#{var}:#{var.class} is not one of #{allow.flatten}"
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,8 @@
1
+ class String
2
+ def quote(q1 = '"', q2 = nil)
3
+ q_left = q1
4
+ q_right = q2 || q1
5
+
6
+ "#{q_left}#{self}#{q_right}"
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ class Time
2
+ YYMMDD = '%y%m%d'
3
+ YYMMDDHHMMSS = '%y%m%d' + '%H%M%S'
4
+
5
+ def ymd()
6
+ self.strftime(YYMMDD)
7
+ end
8
+
9
+ def ymdhms()
10
+ self.strftime(YYMMDDHHMMSS)
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module RubyPatch
2
+ VERSION = '0.0.0'
3
+ end
data/lib/ruby_patch.rb ADDED
@@ -0,0 +1,7 @@
1
+ module RubyPatch
2
+ require 'ruby_patch/version'
3
+ require 'ruby_patch/object'
4
+ require 'ruby_patch/class'
5
+ require 'ruby_patch/string'
6
+ require 'ruby_patch/time'
7
+ end
data/rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ task default: :spec
4
+
5
+ desc "Run specs"
6
+ RSpec::Core::RakeTask.new(:spec){|s|
7
+ s.rspec_opts = '-c'
8
+ }
@@ -0,0 +1,14 @@
1
+ require './lib/ruby_patch/version'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.files = `git ls-files`.split
5
+ s.name = 'ruby_patch'
6
+ s.summary = "Monky patches for ruby."
7
+ s.version = RubyPatch::VERSION
8
+ s.add_development_dependency 'rspec', '~> 2'
9
+ s.add_development_dependency 'simplecov', '~> 0'
10
+ s.author = 'kshramt'
11
+ s.description = "Monkey patches used by kshramt's libraries."
12
+ s.required_ruby_version = '>= 1.9.0'
13
+ s.test_files.concat `git ls-files spec`.split.select{|path| path =~ /_spec\.rb/}
14
+ end
@@ -0,0 +1,22 @@
1
+ require 'rspec'
2
+ require 'spec_helper'
3
+ require 'ruby_patch/class'
4
+
5
+ describe Class do
6
+ before :all do
7
+ class Dummy; end
8
+ class Dummy::Child < Dummy; end
9
+ class Dummy::Child::GrandChild < Dummy::Child; end
10
+ end
11
+ describe '#posterities' do
12
+ it do
13
+ Dummy.posterities.sort.should == [Dummy::Child, Dummy::Child::GrandChild].sort
14
+ end
15
+ end
16
+
17
+ describe '#children' do
18
+ it do
19
+ Dummy.children.sort.should == [Dummy::Child].sort
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,34 @@
1
+ require 'rspec'
2
+ require 'spec_helper'
3
+ require 'ruby_patch/object'
4
+
5
+ describe Object do
6
+ describe '#__DIR__' do
7
+ it do
8
+ __DIR__.should == File.dirname(__FILE__)
9
+ end
10
+ end
11
+
12
+ describe '#deep_clone' do
13
+ it do
14
+ @a = [1]
15
+ @b = @a.deep_clone
16
+ @b[0] = 2
17
+ @a.should == [1]
18
+ end
19
+ end
20
+
21
+ describe '#check_type' do
22
+ context 'valid case' do
23
+ it do
24
+ check_type(3, Numeric.posterities).should == 3
25
+ end
26
+ end
27
+
28
+ context 'invalid case' do
29
+ it do
30
+ lambda{check_type(:a, Numeric.posterities)}.should raise_error(::TypeError)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,21 @@
1
+ require 'rspec'
2
+ require 'spec_helper'
3
+ require 'ruby_patch/string'
4
+
5
+ describe String do
6
+ describe '#quote' do
7
+ context 'default' do
8
+ it do
9
+ 'a'.quote.should == '"a"'
10
+ end
11
+ end
12
+
13
+ context 'one arg' do
14
+ 'a'.quote('->').should == '->a->'
15
+ end
16
+
17
+ context 'two args' do
18
+ 'a'.quote("\e[30m", "\e[0m").should == "\e[30ma\e[0m"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ require 'rspec'
2
+ require 'time'
3
+ require 'spec_helper'
4
+ require 'ruby_patch/time'
5
+
6
+ describe Time do
7
+ before :each do
8
+ @time = Time.parse('12/04/01 01:23:45')
9
+ end
10
+
11
+ describe '#ymd' do
12
+ it do
13
+ @time.ymd.should == '120401'
14
+ end
15
+ end
16
+
17
+ describe '#ymdhms' do
18
+ it do
19
+ @time.ymdhms.should == '120401012345'
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ require 'rspec'
2
+ require 'spec_helper'
3
+ require 'ruby_patch'
@@ -0,0 +1,15 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require 'simplecov'
9
+ SimpleCov.start
10
+
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_patch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - kshramt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &76415490 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *76415490
25
+ - !ruby/object:Gem::Dependency
26
+ name: simplecov
27
+ requirement: &76415070 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *76415070
36
+ description: Monkey patches used by kshramt's libraries.
37
+ email:
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files: []
41
+ files:
42
+ - lib/ruby_patch.rb
43
+ - lib/ruby_patch/class.rb
44
+ - lib/ruby_patch/object.rb
45
+ - lib/ruby_patch/string.rb
46
+ - lib/ruby_patch/time.rb
47
+ - lib/ruby_patch/version.rb
48
+ - rakefile
49
+ - ruby_patch.gemspec
50
+ - spec/ruby_patch/class_spec.rb
51
+ - spec/ruby_patch/object_spec.rb
52
+ - spec/ruby_patch/string_spec.rb
53
+ - spec/ruby_patch/time_spec.rb
54
+ - spec/ruby_patch_spec.rb
55
+ - spec/spec_helper.rb
56
+ homepage:
57
+ licenses: []
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: 1.9.0
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.11
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Monky patches for ruby.
80
+ test_files:
81
+ - spec/ruby_patch/class_spec.rb
82
+ - spec/ruby_patch/object_spec.rb
83
+ - spec/ruby_patch/string_spec.rb
84
+ - spec/ruby_patch/time_spec.rb
85
+ - spec/ruby_patch_spec.rb
86
+ has_rdoc: