royw-roys_extensions 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2009 Roy Wright
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,8 @@
1
+ = roys_extensions
2
+
3
+ Various extensions to the standard ruby library classes that I find useful.
4
+ No particular theme or mission.
5
+
6
+ == Copyright
7
+
8
+ Copyright (c) 2009 Roy Wright. See LICENSE for details.
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "roys_extensions"
8
+ gem.summary = %Q{TODO}
9
+ gem.email = "roy@wright.org"
10
+ gem.homepage = "http://github.com/royw/roys_extensions"
11
+ gem.authors = ["Roy Wright"]
12
+
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
18
+
19
+ require 'spec/rake/spectask'
20
+ Spec::Rake::SpecTask.new(:spec) do |spec|
21
+ spec.libs << 'lib' << 'spec'
22
+ spec.spec_files = FileList['spec/**/*_spec.rb']
23
+ spec.spec_opts = ["--color"]
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+
33
+ task :default => :spec
34
+
35
+ require 'rake/rdoctask'
36
+ Rake::RDocTask.new do |rdoc|
37
+ if File.exist?('VERSION.yml')
38
+ config = YAML.load(File.read('VERSION.yml'))
39
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
40
+ else
41
+ version = ""
42
+ end
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "roys_extensions #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
49
+
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 0
3
+ :patch: 2
4
+ :major: 0
@@ -0,0 +1,22 @@
1
+ require 'module_extensions'
2
+
3
+ # == Synopsis
4
+ # Various extensions to the File class
5
+ # Note, uses the Module.my_extension method to only add the method if
6
+ # it doesn't already exist.
7
+ class File
8
+ class << self
9
+ my_extension("mkdirs") do
10
+ ##
11
+ # make directories including any missing in the path
12
+ #
13
+ # @param [String] dirspec the path to make sure exists
14
+ def File.mkdirs(dirspec)
15
+ unless File.exists?(dirspec)
16
+ mkdirs(File.dirname(dirspec))
17
+ Dir.mkdir(dirspec)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ require 'module_extensions'
2
+
3
+ # == Synopsis
4
+ # Various extensions to the Kernel class
5
+ # Note, uses the Module.my_extension method to only add the method if
6
+ # it doesn't already exist.
7
+ module Kernel
8
+ my_extension("timer") do
9
+ # == Synopsis
10
+ # a simple elapse time for the give block
11
+ # == Usage
12
+ # elapse_seconds = timer {...}
13
+ def timer
14
+ start_time = Time.now
15
+ yield
16
+ Time.now - start_time
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,27 @@
1
+ ######################################################################
2
+ # my extensions to Module. (taken from rake, named changed to not clash
3
+ # when rake is used)
4
+ #
5
+ class Module
6
+ # Check for an existing method in the current class before extending. If
7
+ # the method already exists, then a warning is printed and the extension is
8
+ # not added. Otherwise the block is yielded and any definitions in the
9
+ # block will take effect.
10
+ #
11
+ # Usage:
12
+ #
13
+ # class String
14
+ # rake_extension("xyz") do
15
+ # def xyz
16
+ # ...
17
+ # end
18
+ # end
19
+ # end
20
+ #
21
+ def my_extension(method)
22
+ unless instance_methods.include?(method.to_s) || instance_methods.include?(method.to_sym)
23
+ yield
24
+ end
25
+ end
26
+ end # module Module
27
+
@@ -0,0 +1,22 @@
1
+ require 'module_extensions'
2
+
3
+ # == Synopsis
4
+ # Various extensions to the Numeric class
5
+ # Note, uses the Module.my_extension method to only add the method if
6
+ # it doesn't already exist.
7
+ class Numeric
8
+ my_extension("elapsed_time_s") do
9
+ # == Synopsis
10
+ # return String formated as "HH:MM:SS"
11
+ def elapsed_time_s
12
+ seconds = self
13
+ hours = minutes = 0
14
+ hours = seconds.div 3600
15
+ seconds = seconds - (hours * 3600)
16
+ minutes = seconds.div 60
17
+ seconds = seconds - (minutes * 60)
18
+ sprintf("%.2d:%2.2d:%2.2d", hours, minutes, seconds)
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,29 @@
1
+ require 'module_extensions'
2
+
3
+ # == Synopsis
4
+ # Various extensions to the Object class
5
+ # Note, uses the Module.my_extension method to only add the method if
6
+ # it doesn't already exist.
7
+ class Object
8
+ my_extension("blank?") do
9
+ # == Synopsis
10
+ # return asserted if object is nil or empty
11
+ def blank?
12
+ result = nil?
13
+ unless result
14
+ if respond_to? 'empty?'
15
+ if respond_to? 'strip'
16
+ result = strip.empty?
17
+ else
18
+ if respond_to? 'compact'
19
+ result = compact.empty?
20
+ else
21
+ result = empty?
22
+ end
23
+ end
24
+ end
25
+ end
26
+ result
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,10 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'rubygems'
5
+ require 'module_extensions'
6
+ require 'string_extensions'
7
+ require 'object_extensions'
8
+ require 'numeric_extensions'
9
+ require 'kernel_extensions'
10
+ require 'file_extensions'
@@ -0,0 +1,51 @@
1
+ require 'cgi'
2
+ require 'iconv'
3
+ require 'module_extensions'
4
+
5
+ # == Synopsis
6
+ # Various extensions to the String class
7
+ # Note, uses the Module.my_extension method to only add the method if
8
+ # it doesn't already exist.
9
+ class String
10
+ my_extension("unescape_html") do
11
+ # == Synopsis
12
+ # unescape HTML
13
+ def unescape_html
14
+ Iconv.conv("UTF-8", 'ISO-8859-1', CGI::unescapeHTML(self))
15
+ end
16
+ end
17
+
18
+ my_extension("escape_unicode") do
19
+ # == Synopsis
20
+ # this handles unicode characters by converting each byte to "%XX"
21
+ # where XX is the hex value
22
+ def escape_unicode
23
+ self.each_byte.collect{|c| c.to_i > 127 ? "%#{c.to_i.to_s(16)}" : c.chr}.join('')
24
+ end
25
+ end
26
+
27
+ my_extension("strip_tags") do
28
+ # == Synopsis
29
+ # remove angle bracket tags from the string
30
+ def strip_tags
31
+ gsub(/<\/?[^>]*>/, "")
32
+ end
33
+ end
34
+
35
+ my_extension("ext") do
36
+ # == Synopsis
37
+ # Replace the file extension with +newext+. If there is no extenson on
38
+ # the string, append the new extension to the end. If the new extension
39
+ # is not given, or is the empty string, remove any existing extension.
40
+ #
41
+ # +ext+ is a user added method for the String class.
42
+ def ext(newext='')
43
+ return self.dup if ['.', '..'].include? self
44
+ if newext != ''
45
+ newext = (newext =~ /^\./) ? newext : ("." + newext)
46
+ end
47
+ dup.sub!(%r(([^/\\])\.[^./\\]*$)) { $1 + newext } || self + newext
48
+ end
49
+ end
50
+ end
51
+
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+ require 'tempfile'
3
+ require 'fileutils'
4
+ include FileUtils
5
+
6
+ TMPDIR = File.join(File.dirname(__FILE__), '../tmp')
7
+
8
+ describe "FileExtensions" do
9
+ before(:all) do
10
+ Dir.mkdir(TMPDIR) unless File.exist?(TMPDIR)
11
+ end
12
+
13
+ after(:each) do
14
+ rm_rf(Dir.glob(File.join(TMPDIR,'file_extensions_spec*')))
15
+ end
16
+
17
+ it "should make a single directory when it does not exist" do
18
+ dirspec = get_temp_filename
19
+ File.mkdirs(dirspec)
20
+ (File.exist?(dirspec).should be_true) && (File.directory?(dirspec).should be_true)
21
+ end
22
+
23
+ it "should make a multiple directories when they do not exist" do
24
+ dirspec = File.join(get_temp_filename, 'foo', 'bar')
25
+ File.mkdirs(dirspec)
26
+ (File.exist?(dirspec).should be_true) && (File.directory?(dirspec).should be_true)
27
+ end
28
+
29
+ it "should not make any directories when they already exist" do
30
+ dirspec = File.join(get_temp_filename, 'foo', 'bar')
31
+ File.mkdirs(dirspec)
32
+ File.mkdirs(dirspec)
33
+ (File.exist?(dirspec).should be_true) && (File.directory?(dirspec).should be_true)
34
+ end
35
+
36
+
37
+ def get_temp_filename
38
+ outfile = Tempfile.new('file_extensions_spec', TMPDIR)
39
+ filespec = outfile.path
40
+ outfile.unlink
41
+ filespec
42
+ end
43
+
44
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe "KernelExtensions" do
4
+ it "should time a block of code" do
5
+ elapse = timer { sleep 1.1 }
6
+ (elapse.should > 1) && (elapse.should < 2)
7
+ end
8
+
9
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+ require 'ruby-debug'
3
+
4
+ describe "ModuleExtensions" do
5
+ it "should allow a method to be added to a class when the method does not already exists" do
6
+ class A
7
+ my_extension("foo") do
8
+ def foo
9
+ 'foo'
10
+ end
11
+ end
12
+ end
13
+ A.instance_methods.include?('foo').should be_true
14
+ end
15
+
16
+ it "should not allow adding a method to a class if the method already exists" do
17
+ class A
18
+ def foo
19
+ 'bar'
20
+ end
21
+ end
22
+
23
+ class A
24
+ my_extension("foo") do
25
+ def foo
26
+ 'foo'
27
+ end
28
+ end
29
+ end
30
+ a = A.new
31
+ a.foo.should == 'bar'
32
+ end
33
+
34
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe "NumericExtensions" do
4
+ it "should convert 0 seconds to '00:00:00'" do
5
+ 0.elapsed_time_s.should == '00:00:00'
6
+ end
7
+
8
+ it "should convert 59 seconds to '00:00:59'" do
9
+ 59.elapsed_time_s.should == '00:00:59'
10
+ end
11
+
12
+ it "should convert 60 seconds to '00:01:00'" do
13
+ 60.elapsed_time_s.should == '00:01:00'
14
+ end
15
+
16
+ it "should convert 3599 seconds to '00:59:59'" do
17
+ 3599.elapsed_time_s.should == '00:59:59'
18
+ end
19
+
20
+ it "should convert 3600 seconds to '01:00:00'" do
21
+ 3600.elapsed_time_s.should == '01:00:00'
22
+ end
23
+
24
+ it "should convert 86399 seconds to '23:59:59'" do
25
+ 86399.elapsed_time_s.should == '23:59:59'
26
+ end
27
+
28
+ it "should convert 86400 seconds to '24:00:00'" do
29
+ 86400.elapsed_time_s.should == '24:00:00'
30
+ end
31
+
32
+ it "should convert 359999 seconds to '99:59:59'" do
33
+ 359999.elapsed_time_s.should == '99:59:59'
34
+ end
35
+
36
+ it "should convert 360000 seconds to '100:00:00'" do
37
+ 360000.elapsed_time_s.should == '100:00:00'
38
+ end
39
+
40
+
41
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe "ObjectExtensions" do
4
+ it "should be blank for nil" do
5
+ nil.blank?.should be_true
6
+ end
7
+
8
+ it "should be blank for empty String" do
9
+ ''.blank?.should be_true
10
+ end
11
+
12
+ it "should be blank for pure white space String" do
13
+ ' '.blank?.should be_true
14
+ end
15
+
16
+ it "should be blank for empty Array" do
17
+ [].blank?.should be_true
18
+ end
19
+
20
+ it "should be blank for Array of nils" do
21
+ [nil, nil].blank?.should be_true
22
+ end
23
+
24
+ it "should be blank for empty Hash" do
25
+ {}.blank?.should be_true
26
+ end
27
+
28
+ it "should not be blank for non-empty String" do
29
+ ' foo '.blank?.should be_false
30
+ end
31
+
32
+ it "should not be blank for non-empty Array" do
33
+ [1].blank?.should be_false
34
+ end
35
+
36
+ it "should not be blank for empty embedded Arrays" do
37
+ [[]].blank?.should be_false
38
+ end
39
+
40
+ it "should not be blank for non-empty Hash" do
41
+ {:a => nil}.blank?.should be_false
42
+ end
43
+
44
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require 'roys_extensions'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ puts "string_extensions"
4
+ describe "StringExtensions" do
5
+ it "should escape unicode binary in URL strings to %xx notation" do
6
+ s = "http://www.themoviedb.org/image/backdrops/23357/W\303\244hrend_Du_schliefst.jpg"
7
+ s.escape_unicode.should == "http://www.themoviedb.org/image/backdrops/23357/W%c3%a4hrend_Du_schliefst.jpg"
8
+ end
9
+
10
+ it "should strip angle bracket tags" do
11
+ s = 'Now is the <b>time</b> for all <i>good</i> Aggies'
12
+ s.strip_tags.should == 'Now is the time for all good Aggies'
13
+ end
14
+
15
+ it "should replace file extension" do
16
+ s = 'a/b.c'
17
+ s.ext('d').should == 'a/b.d'
18
+ end
19
+
20
+ it "should replace only the last file extension in the string" do
21
+ s = 'a.b.c'
22
+ s.ext('d').should == 'a.b.d'
23
+ end
24
+
25
+ it "should append file extension when there is not one" do
26
+ s = 'a/b/c'
27
+ s.ext('d').should == 'a/b/c.d'
28
+ end
29
+
30
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: royw-roys_extensions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Roy Wright
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-21 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: roy@wright.org
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - LICENSE
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION.yml
30
+ - lib/file_extensions.rb
31
+ - lib/kernel_extensions.rb
32
+ - lib/module_extensions.rb
33
+ - lib/numeric_extensions.rb
34
+ - lib/object_extensions.rb
35
+ - lib/roys_extensions.rb
36
+ - lib/string_extensions.rb
37
+ - spec/file_extensions_spec.rb
38
+ - spec/kernel_extensions_spec.rb
39
+ - spec/module_extensions_spec.rb
40
+ - spec/numeric_extensions_spec.rb
41
+ - spec/object_extensions_spec.rb
42
+ - spec/spec_helper.rb
43
+ - spec/string_extensions_spec.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/royw/roys_extensions
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.2.0
67
+ signing_key:
68
+ specification_version: 2
69
+ summary: TODO
70
+ test_files:
71
+ - spec/spec_helper.rb
72
+ - spec/numeric_extensions_spec.rb
73
+ - spec/object_extensions_spec.rb
74
+ - spec/file_extensions_spec.rb
75
+ - spec/string_extensions_spec.rb
76
+ - spec/module_extensions_spec.rb
77
+ - spec/kernel_extensions_spec.rb