sc-core-ext 1.1.1 → 1.2.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.
- data/History.txt +32 -30
- data/Manifest.txt +31 -29
- data/PostInstall.txt +7 -7
- data/README.rdoc +40 -40
- data/Rakefile +15 -1
- data/lib/sc-core-ext.rb +16 -15
- data/lib/sc-core-ext/active_support/ordered_hash.rb +7 -7
- data/lib/sc-core-ext/array.rb +23 -23
- data/lib/sc-core-ext/dependencies.rb +6 -6
- data/lib/sc-core-ext/float.rb +20 -20
- data/lib/sc-core-ext/hash.rb +84 -66
- data/lib/sc-core-ext/numeric.rb +10 -0
- data/lib/sc-core-ext/object.rb +19 -19
- data/lib/sc-core-ext/regexp.rb +14 -14
- data/lib/sc-core-ext/string.rb +44 -44
- data/script/console +9 -9
- data/script/destroy +14 -14
- data/script/generate +14 -14
- data/spec/rcov.opts +2 -2
- data/spec/sc-core-ext/active_support/ordered_hash_spec.rb +16 -16
- data/spec/sc-core-ext/array_spec.rb +41 -41
- data/spec/sc-core-ext/date_time_spec.rb +15 -15
- data/spec/sc-core-ext/float_spec.rb +28 -28
- data/spec/sc-core-ext/hash_spec.rb +53 -39
- data/spec/sc-core-ext/numeric_spec.rb +24 -0
- data/spec/sc-core-ext/object_spec.rb +28 -28
- data/spec/sc-core-ext/regexp_spec.rb +17 -17
- data/spec/sc-core-ext/string_spec.rb +56 -56
- data/spec/spec.opts +4 -4
- metadata +9 -7
data/lib/sc-core-ext/object.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
|
-
class Object
|
2
|
-
# Attempts to call #dup, and returns itself if the object cannot be duped (Symbol, Fixnum, etc.)
|
3
|
-
def dup?
|
4
|
-
dup rescue self
|
5
|
-
end
|
6
|
-
|
7
|
-
class << self
|
8
|
-
# Creates an attribute accessor similar to attr_accessor, except the reader is appended with a question mark.
|
9
|
-
# The variable name is the same.
|
10
|
-
#
|
11
|
-
# Example:
|
12
|
-
# attr_boolean :new_record # => { #new_record=(bool), #new_record? }
|
13
|
-
#
|
14
|
-
def attr_boolean(*a)
|
15
|
-
attr_writer *a
|
16
|
-
a.each { |i| define_method("#{i}?") { instance_variable_get("@#{i}") } }
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
1
|
+
class Object
|
2
|
+
# Attempts to call #dup, and returns itself if the object cannot be duped (Symbol, Fixnum, etc.)
|
3
|
+
def dup?
|
4
|
+
dup rescue self
|
5
|
+
end
|
6
|
+
|
7
|
+
class << self
|
8
|
+
# Creates an attribute accessor similar to attr_accessor, except the reader is appended with a question mark.
|
9
|
+
# The variable name is the same.
|
10
|
+
#
|
11
|
+
# Example:
|
12
|
+
# attr_boolean :new_record # => { #new_record=(bool), #new_record? }
|
13
|
+
#
|
14
|
+
def attr_boolean(*a)
|
15
|
+
attr_writer *a
|
16
|
+
a.each { |i| define_method("#{i}?") { instance_variable_get("@#{i}") } }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/sc-core-ext/regexp.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
class Regexp
|
2
|
-
# Searches the target for all occurrances of this Regex. If a block is given, yields each match found.
|
3
|
-
# Returns an array of all matches found.
|
4
|
-
def each_match(target)
|
5
|
-
matches = []
|
6
|
-
offset = 0
|
7
|
-
while offset < target.length && !(match = match(target[offset..-1])).nil?
|
8
|
-
offset += match.offset(0)[1]
|
9
|
-
yield match if block_given?
|
10
|
-
matches << match
|
11
|
-
end
|
12
|
-
matches
|
13
|
-
end
|
14
|
-
end
|
1
|
+
class Regexp
|
2
|
+
# Searches the target for all occurrances of this Regex. If a block is given, yields each match found.
|
3
|
+
# Returns an array of all matches found.
|
4
|
+
def each_match(target)
|
5
|
+
matches = []
|
6
|
+
offset = 0
|
7
|
+
while offset < target.length && !(match = match(target[offset..-1])).nil?
|
8
|
+
offset += match.offset(0)[1]
|
9
|
+
yield match if block_given?
|
10
|
+
matches << match
|
11
|
+
end
|
12
|
+
matches
|
13
|
+
end
|
14
|
+
end
|
data/lib/sc-core-ext/string.rb
CHANGED
@@ -1,44 +1,44 @@
|
|
1
|
-
class String
|
2
|
-
# The inverse of +ActiveSupport::Inflection#humanize+: Lowercases the first letter, and turns spaces into underscores.
|
3
|
-
# This is meant to assist in creating method names. A camelCase method name can be created using #dehumanize:
|
4
|
-
# "say_hello_to_the_world".camelize.dehumanize # => "sayHelloToTheWorld"
|
5
|
-
#
|
6
|
-
# This can also be used for creating permalinks:
|
7
|
-
# "Say hello to the world".dehumanize # => "say_hello_to_the_world"
|
8
|
-
def dehumanize
|
9
|
-
self.camelize.gsub(/^([A-Z])/) { |x| x.downcase }.gsub(/ /, '_')
|
10
|
-
end
|
11
|
-
|
12
|
-
def parenthesize(with = "()")
|
13
|
-
with[0].chr + self + with[(with.length == 1 ? 0 : 1)].chr
|
14
|
-
end
|
15
|
-
|
16
|
-
# This method assumes that this string represents a Ruby-like method name.
|
17
|
-
# Removes question marks and exclamation marks from this string, prepending "is_" or "force_", respectively.
|
18
|
-
# If neither of these punctuation marks exist, the original string is returned.
|
19
|
-
def depunctuate
|
20
|
-
if self[/\?/]
|
21
|
-
"is_"+self.gsub(/\?/, '')
|
22
|
-
elsif self[/\!/]
|
23
|
-
"force_"+self.gsub(/\!/, '')
|
24
|
-
else self
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
unless defined?(indent)
|
29
|
-
# Returns a copy of itself, except that the first character is preceded by a tabstop (\t) and a tabstop
|
30
|
-
# also follows every subsequent newline (\n) character.
|
31
|
-
#
|
32
|
-
# The unused argument is for compatibility with treetop, a cucumber dependency.
|
33
|
-
def indent(unused = 0)
|
34
|
-
"\t#{self.gsub(/\n/m, "\n\t")}"
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def hex_to_bin
|
39
|
-
temp = gsub("\s", "");
|
40
|
-
ret = []
|
41
|
-
(0...temp.length / 2).each { |index| ret[index] = [temp[index*2, 2]].pack("H2") }
|
42
|
-
return ret.join
|
43
|
-
end
|
44
|
-
end
|
1
|
+
class String
|
2
|
+
# The inverse of +ActiveSupport::Inflection#humanize+: Lowercases the first letter, and turns spaces into underscores.
|
3
|
+
# This is meant to assist in creating method names. A camelCase method name can be created using #dehumanize:
|
4
|
+
# "say_hello_to_the_world".camelize.dehumanize # => "sayHelloToTheWorld"
|
5
|
+
#
|
6
|
+
# This can also be used for creating permalinks:
|
7
|
+
# "Say hello to the world".dehumanize # => "say_hello_to_the_world"
|
8
|
+
def dehumanize
|
9
|
+
self.camelize.gsub(/^([A-Z])/) { |x| x.downcase }.gsub(/ /, '_')
|
10
|
+
end
|
11
|
+
|
12
|
+
def parenthesize(with = "()")
|
13
|
+
with[0].chr + self + with[(with.length == 1 ? 0 : 1)].chr
|
14
|
+
end
|
15
|
+
|
16
|
+
# This method assumes that this string represents a Ruby-like method name.
|
17
|
+
# Removes question marks and exclamation marks from this string, prepending "is_" or "force_", respectively.
|
18
|
+
# If neither of these punctuation marks exist, the original string is returned.
|
19
|
+
def depunctuate
|
20
|
+
if self[/\?/]
|
21
|
+
"is_"+self.gsub(/\?/, '')
|
22
|
+
elsif self[/\!/]
|
23
|
+
"force_"+self.gsub(/\!/, '')
|
24
|
+
else self
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
unless defined?(indent)
|
29
|
+
# Returns a copy of itself, except that the first character is preceded by a tabstop (\t) and a tabstop
|
30
|
+
# also follows every subsequent newline (\n) character.
|
31
|
+
#
|
32
|
+
# The unused argument is for compatibility with treetop, a cucumber dependency.
|
33
|
+
def indent(unused = 0)
|
34
|
+
"\t#{self.gsub(/\n/m, "\n\t")}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def hex_to_bin
|
39
|
+
temp = gsub("\s", "");
|
40
|
+
ret = []
|
41
|
+
(0...temp.length / 2).each { |index| ret[index] = [temp[index*2, 2]].pack("H2") }
|
42
|
+
return ret.join
|
43
|
+
end
|
44
|
+
end
|
data/script/console
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# File: script/console
|
3
|
-
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
-
|
5
|
-
libs = " -r irb/completion"
|
6
|
-
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
-
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
-
libs << " -r #{File.dirname(__FILE__) + '/../lib/sc-core-ext.rb'}"
|
9
|
-
puts "Loading sc-core-ext gem"
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/sc-core-ext.rb'}"
|
9
|
+
puts "Loading sc-core-ext gem"
|
10
10
|
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
-
|
4
|
-
begin
|
5
|
-
require 'rubigen'
|
6
|
-
rescue LoadError
|
7
|
-
require 'rubygems'
|
8
|
-
require 'rubigen'
|
9
|
-
end
|
10
|
-
require 'rubigen/scripts/destroy'
|
11
|
-
|
12
|
-
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
-
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
-
RubiGen::Scripts::Destroy.new.run(ARGV)
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
-
|
4
|
-
begin
|
5
|
-
require 'rubigen'
|
6
|
-
rescue LoadError
|
7
|
-
require 'rubygems'
|
8
|
-
require 'rubigen'
|
9
|
-
end
|
10
|
-
require 'rubigen/scripts/generate'
|
11
|
-
|
12
|
-
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
-
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
-
RubiGen::Scripts::Generate.new.run(ARGV)
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
data/spec/rcov.opts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
--exclude "spec/*,gems/*"
|
2
|
-
--aggregate coverage.data
|
1
|
+
--exclude "spec/*,gems/*"
|
2
|
+
--aggregate coverage.data
|
@@ -1,16 +1,16 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe ActiveSupport::OrderedHash do
|
4
|
-
context "#to_yaml" do
|
5
|
-
subject do
|
6
|
-
o = ActiveSupport::OrderedHash.new
|
7
|
-
o[:one] = 1
|
8
|
-
o[:two] = 2
|
9
|
-
o
|
10
|
-
end
|
11
|
-
|
12
|
-
it "converts to yaml" do
|
13
|
-
subject.to_yaml.should == "--- !map:ActiveSupport::OrderedHash \n:one: 1\n:two: 2\n"
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveSupport::OrderedHash do
|
4
|
+
context "#to_yaml" do
|
5
|
+
subject do
|
6
|
+
o = ActiveSupport::OrderedHash.new
|
7
|
+
o[:one] = 1
|
8
|
+
o[:two] = 2
|
9
|
+
o
|
10
|
+
end
|
11
|
+
|
12
|
+
it "converts to yaml" do
|
13
|
+
subject.to_yaml.should == "--- !map:ActiveSupport::OrderedHash \n:one: 1\n:two: 2\n"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -1,41 +1,41 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Array do
|
4
|
-
subject { [ 1, 2, "abc", "def", ["foo", "bar"], { 'baz' => '42' } ] }
|
5
|
-
|
6
|
-
context "#%" do
|
7
|
-
context "with another array" do
|
8
|
-
it "results in an array intersection" do
|
9
|
-
(subject % [1,2]).should == [1,2]
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
context "with a non-array value" do
|
14
|
-
it "results in elements equal to only that value" do
|
15
|
-
(subject % 2).should == [2]
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
context "#deep_dup" do
|
21
|
-
it "is not itself" do
|
22
|
-
subject.deep_dup.should_not equal(subject)
|
23
|
-
end
|
24
|
-
|
25
|
-
it "elements are not themselves" do
|
26
|
-
subject.deep_dup.each_with_index do |ele, index|
|
27
|
-
subject[index].should_not equal(ele) unless ele.dup?.equal?(ele)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
it "hashes' keys are not themselves" do
|
32
|
-
hash = subject.deep_dup.last
|
33
|
-
hash.keys[0].should_not equal(subject.last.keys[0])
|
34
|
-
end
|
35
|
-
|
36
|
-
it "hashes' values are not themselves" do
|
37
|
-
hash = subject.deep_dup.last
|
38
|
-
hash.values[0].should_not equal(subject.last.values[0])
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Array do
|
4
|
+
subject { [ 1, 2, "abc", "def", ["foo", "bar"], { 'baz' => '42' } ] }
|
5
|
+
|
6
|
+
context "#%" do
|
7
|
+
context "with another array" do
|
8
|
+
it "results in an array intersection" do
|
9
|
+
(subject % [1,2]).should == [1,2]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context "with a non-array value" do
|
14
|
+
it "results in elements equal to only that value" do
|
15
|
+
(subject % 2).should == [2]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "#deep_dup" do
|
21
|
+
it "is not itself" do
|
22
|
+
subject.deep_dup.should_not equal(subject)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "elements are not themselves" do
|
26
|
+
subject.deep_dup.each_with_index do |ele, index|
|
27
|
+
subject[index].should_not equal(ele) unless ele.dup?.equal?(ele)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it "hashes' keys are not themselves" do
|
32
|
+
hash = subject.deep_dup.last
|
33
|
+
hash.keys[0].should_not equal(subject.last.keys[0])
|
34
|
+
end
|
35
|
+
|
36
|
+
it "hashes' values are not themselves" do
|
37
|
+
hash = subject.deep_dup.last
|
38
|
+
hash.values[0].should_not equal(subject.last.values[0])
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe DateTime do
|
4
|
-
context "#to_i" do
|
5
|
-
it "works" do
|
6
|
-
DateTime.civil(2000).to_i.should == 946684800
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
context "#to_f" do
|
11
|
-
it "works" do
|
12
|
-
DateTime.civil(1999,12,31,19,0,0,Rational(-5,24)).to_f.should == 946684800.0
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DateTime do
|
4
|
+
context "#to_i" do
|
5
|
+
it "works" do
|
6
|
+
DateTime.civil(2000).to_i.should == 946684800
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
context "#to_f" do
|
11
|
+
it "works" do
|
12
|
+
DateTime.civil(1999,12,31,19,0,0,Rational(-5,24)).to_f.should == 946684800.0
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,28 +1,28 @@
|
|
1
|
-
describe Float do
|
2
|
-
context "#to_frac" do
|
3
|
-
context "1.0" do
|
4
|
-
subject { 1.0 }
|
5
|
-
it("== 1") { subject.to_frac.should == "1" }
|
6
|
-
end
|
7
|
-
|
8
|
-
context "1.25" do
|
9
|
-
subject { 1.25 }
|
10
|
-
it("== 1 1/4") { subject.to_frac.should == "1¼" }
|
11
|
-
end
|
12
|
-
|
13
|
-
context "1.5" do
|
14
|
-
subject { 1.5 }
|
15
|
-
it("== 1 1/2") { subject.to_frac.should == "1½" }
|
16
|
-
end
|
17
|
-
|
18
|
-
context "1.75" do
|
19
|
-
subject { 1.75 }
|
20
|
-
it("== 1 3/4") { subject.to_frac.should == "1¾" }
|
21
|
-
end
|
22
|
-
|
23
|
-
context "1.88" do
|
24
|
-
subject { 1.88 }
|
25
|
-
it("== 1.88") { subject.to_frac.should == '1.88' }
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
1
|
+
describe Float do
|
2
|
+
context "#to_frac" do
|
3
|
+
context "1.0" do
|
4
|
+
subject { 1.0 }
|
5
|
+
it("== 1") { subject.to_frac.should == "1" }
|
6
|
+
end
|
7
|
+
|
8
|
+
context "1.25" do
|
9
|
+
subject { 1.25 }
|
10
|
+
it("== 1 1/4") { subject.to_frac.should == "1¼" }
|
11
|
+
end
|
12
|
+
|
13
|
+
context "1.5" do
|
14
|
+
subject { 1.5 }
|
15
|
+
it("== 1 1/2") { subject.to_frac.should == "1½" }
|
16
|
+
end
|
17
|
+
|
18
|
+
context "1.75" do
|
19
|
+
subject { 1.75 }
|
20
|
+
it("== 1 3/4") { subject.to_frac.should == "1¾" }
|
21
|
+
end
|
22
|
+
|
23
|
+
context "1.88" do
|
24
|
+
subject { 1.88 }
|
25
|
+
it("== 1.88") { subject.to_frac.should == '1.88' }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|