corelib 0.0.1 → 0.0.2
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/.gitignore +1 -0
- data/CHANGELOG.md +0 -0
- data/lib/corelib.rb +8 -0
- data/lib/corelib/array/core.rb +25 -0
- data/lib/corelib/array/helpers.rb +64 -0
- data/lib/corelib/boolean/false.rb +12 -4
- data/lib/corelib/boolean/true.rb +4 -5
- data/lib/corelib/hash/core.rb +17 -0
- data/lib/corelib/nil/core.rb +11 -0
- data/lib/corelib/numeric/core.rb +2 -2
- data/lib/corelib/object/core.rb +7 -0
- data/lib/corelib/string/core.rb +51 -11
- data/lib/corelib/version.rb +1 -1
- data/spec/array/core_spec.rb +59 -2
- data/spec/array/helpers_spec.rb +32 -0
- data/spec/boolean/false_spec.rb +30 -0
- data/spec/boolean/true_spec.rb +30 -0
- data/spec/hash/core_spec.rb +20 -0
- data/spec/nil/core_spec.rb +18 -0
- data/spec/object/core_spec.rb +9 -0
- data/spec/string/core_spec.rb +106 -0
- metadata +21 -2
data/.gitignore
CHANGED
data/CHANGELOG.md
ADDED
File without changes
|
data/lib/corelib.rb
CHANGED
@@ -1,8 +1,16 @@
|
|
1
|
+
#See bottom of http://stackoverflow.com/questions/4527992/require-all-files-in-sub-directory
|
2
|
+
#Dir[File.join(".", "**/*.rb")].each { |f| require f unless f[/^\.\/spec\//]}
|
3
|
+
|
4
|
+
#Old Way
|
1
5
|
require "corelib/version"
|
2
6
|
require "corelib/array/alias"
|
3
7
|
require "corelib/array/core"
|
8
|
+
require "corelib/array/helpers"
|
4
9
|
require "corelib/array/math"
|
5
10
|
require "corelib/boolean/true"
|
6
11
|
require "corelib/boolean/false"
|
12
|
+
require "corelib/hash/core"
|
13
|
+
require "corelib/nil/core"
|
14
|
+
require "corelib/object/core"
|
7
15
|
require "corelib/numeric/core"
|
8
16
|
require "corelib/string/core"
|
data/lib/corelib/array/core.rb
CHANGED
@@ -1,4 +1,29 @@
|
|
1
1
|
class Array
|
2
2
|
|
3
|
+
def to_yes_no(options={})
|
4
|
+
self.collect {|e| e.to_yes_no(options)}
|
5
|
+
end
|
6
|
+
|
7
|
+
def add_blank_option(options={})
|
8
|
+
doit = options.fetch(:doit, true)
|
9
|
+
value = options.fetch(:value, 0)
|
10
|
+
label = options.fetch(:label, "")
|
11
|
+
self.insert(0, [label, value]) if doit
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
#This method iterates over the Array as normal #each method. For each iteration
|
16
|
+
#set two variables in the block, |item, flag|. item will be set tot he current item
|
17
|
+
#in the iteration; flag will be set to "false" on all iterations except the last iteration
|
18
|
+
def each_with_end_flag
|
19
|
+
my_size = self.size
|
20
|
+
self.each_with_index do |item, index|
|
21
|
+
index + 1 == my_size ? yield(item, true) : yield(item, false)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def not_empty?
|
26
|
+
!self.empty?
|
27
|
+
end
|
3
28
|
|
4
29
|
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
class Array
|
2
|
+
|
3
|
+
def options_label_for(value)
|
4
|
+
sub = self.find {|each| each[1] == value}
|
5
|
+
return nil if sub.nil?
|
6
|
+
sub[0]
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.us_state_options(options={})
|
10
|
+
abbrev = options.fetch(:abbrev, false)
|
11
|
+
options[:abbrev] = abbrev #In case abbrev is not in options, set it to the default so sub routines can access
|
12
|
+
|
13
|
+
list = abbrev ? us_state_options_abbrev_raw : us_state_options_raw
|
14
|
+
list.format_raw_us_state_list(options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.us_state_options_raw
|
18
|
+
[['Alabama', 'AL'], ['Alaska', 'AK'], ['Arizona', 'AZ'], ['Arkansas', 'AR'], ['California', 'CA'], ['Colorado', 'CO'],
|
19
|
+
['Connecticut', 'CT'], ['D.C.', 'DC'], ['Delaware', 'DE'], ['Florida', 'FL'], ['Georgia', 'GA'], ['Hawaii', 'HI'], ['Idaho', 'ID'],
|
20
|
+
['Illinois', 'IL'], ['Indiana', 'IN'], ['Iowa', 'IA'], ['Kansas', 'KS'], ['Kentucky', 'KY'], ['Louisiana', 'LA'],
|
21
|
+
['Maine', 'ME'], ['Maryland', 'MD'], ['Massachusetts', 'MA'], ['Michigan', 'MI'], ['Minnesota', 'MN'], ['Mississippi', 'MS'],
|
22
|
+
['Missouri', 'MO'], ['Montana', 'MT'], ['Nebraska', 'NE'], ['Nevada', 'NV'], ['New Hampshire', 'NH'], ['New Jersey', 'NJ'],
|
23
|
+
['New Mexico', 'NM'], ['New York', 'NY'], ['North Carolina', 'NC'], ['North Dakota', 'ND'], ['Ohio', 'OH'], ['Oklahoma', 'OK'],
|
24
|
+
['Oregon', 'OR'], ['Pennsylvania', 'PA'], ['Rhode Island', 'RI'], ['South Carolina', 'SC'], ['South Dakota', 'SD'],
|
25
|
+
['Tennessee', 'TN'], ['Texas', 'TX'], ['Utah', 'UT'], ['Vermont', 'VT'], ['Virginia', 'VA'], ['Washington', 'WA'],
|
26
|
+
['West Virginia', 'WV'], ['Wisconsin', 'WI'], ['Wyoming', 'WY']]
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.us_state_options_abbrev_raw
|
30
|
+
[['AL', 'AL'], ['AK', 'AK'], ['AZ', 'AZ'], ['AR', 'AR'], ['CA', 'CA'], ['CO', 'CO'],
|
31
|
+
['CT', 'CT'], ['DC', 'DC'], ['DE', 'DE'], ['FL', 'FL'], ['GA', 'GA'], ['HI', 'HI'], ['ID', 'ID'],
|
32
|
+
['IL', 'IL'], ['IN', 'IN'], ['IA', 'IA'], ['KS', 'KS'], ['KY', 'KY'], ['LA', 'LA'],
|
33
|
+
['ME', 'ME'], ['MD', 'MD'], ['MA', 'MA'], ['MI', 'MI'], ['MN', 'MN'], ['MS', 'MS'],
|
34
|
+
['MO', 'MO'], ['MT', 'MT'], ['NE', 'NE'], ['NV', 'NV'], ['NH', 'NH'], ['NJ', 'NJ'],
|
35
|
+
['NM', 'NM'], ['NY', 'NY'], ['NC', 'NC'], ['ND', 'ND'], ['OH', 'OH'], ['OK', 'OK'],
|
36
|
+
['OR', 'OR'], ['PA', 'PA'], ['RI', 'RI'], ['SC', 'SC'], ['SD', 'SD'],
|
37
|
+
['TN', 'TN'], ['TX', 'TX'], ['UT', 'UT'], ['VT', 'VT'], ['VA', 'VA'], ['WA', 'WA'],
|
38
|
+
['WV', 'WV'], ['WI', 'WI'], ['WY', 'WY'],]
|
39
|
+
end
|
40
|
+
|
41
|
+
def format_raw_us_state_list(options={})
|
42
|
+
add_blank = options.fetch(:blank, "true")
|
43
|
+
blank_value = options.fetch(:blank_value, 0)
|
44
|
+
blank_label = options.fetch(:blank_label, "")
|
45
|
+
blank_position = options.fetch(:blank_position, 0)
|
46
|
+
self.insert(blank_position, [blank_label, blank_value]) if add_blank
|
47
|
+
change_us_state_list_label_case(options)
|
48
|
+
self
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def change_us_state_list_label_case(options)
|
54
|
+
abbrev = options.fetch(:abbrev)
|
55
|
+
label_format = abbrev ? options.fetch(:format, "Up") : options.fetch(:format, "Cap")
|
56
|
+
return self if (label_format == "Up") and abbrev
|
57
|
+
return self if (label_format == "Cap") and !abbrev
|
58
|
+
|
59
|
+
self.each {|opt| opt[0].upcase!} if label_format == "Up"
|
60
|
+
self.each {|opt| opt[0].capitalize!} if label_format == "Cap"
|
61
|
+
self.each {|opt| opt[0].downcase!} if label_format == "Down"
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -1,9 +1,17 @@
|
|
1
1
|
class FalseClass
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
#Cap, Down, Up
|
4
|
+
def to_yes_no(options={})
|
5
|
+
value = options.fetch(:if_no, "No")
|
6
|
+
FalseClass.format_to_yes_no(value, options)
|
7
7
|
end
|
8
8
|
|
9
|
+
def self.format_to_yes_no(value, options)
|
10
|
+
return value if !options.has_key?(:format) #If format is unspecified, use the default
|
11
|
+
format = options[:format]
|
12
|
+
return value.upcase if format == "Up"
|
13
|
+
return value.downcase if format == "Down"
|
14
|
+
value.capitalize #format has to be capital at this point (or an invalid format, so we will assume capital)
|
15
|
+
end
|
16
|
+
|
9
17
|
end
|
data/lib/corelib/boolean/true.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
class TrueClass
|
2
2
|
|
3
3
|
#format accepts (C-Capitalized, U-Uppercase, L-Lowercase)
|
4
|
-
def to_yes_no(
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
end
|
4
|
+
def to_yes_no(options={})
|
5
|
+
value = options.fetch(:if_yes, "Yes")
|
6
|
+
FalseClass.format_to_yes_no(value, options)
|
7
|
+
end
|
9
8
|
|
10
9
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class Hash
|
2
|
+
|
3
|
+
def not_empty?
|
4
|
+
!self.empty?
|
5
|
+
end
|
6
|
+
|
7
|
+
#This method iterates over the Array as normal #each method. For each iteration
|
8
|
+
#set two variables in the block, |item, flag|. item will be set tot he current item
|
9
|
+
#in the iteration; flag will be set to "false" on all iterations except the last iteration
|
10
|
+
def each_with_end_flag
|
11
|
+
my_size = self.size
|
12
|
+
self.each_with_index do |item, index|
|
13
|
+
index + 1 == my_size ? yield(item, true) : yield(item, false)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
data/lib/corelib/numeric/core.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
class Numeric
|
2
2
|
|
3
3
|
#format accepts (C-Capitalized, U-Uppercase, L-Lowercase)
|
4
|
-
def to_yes_no(
|
5
|
-
(self == 1 || self == 1.0).to_yes_no(
|
4
|
+
def to_yes_no(options={})
|
5
|
+
(self == 1 || self == 1.0).to_yes_no(options)
|
6
6
|
end
|
7
7
|
|
8
8
|
#Assumes numeric value is in seconds
|
data/lib/corelib/string/core.rb
CHANGED
@@ -1,23 +1,63 @@
|
|
1
1
|
class String
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
#Does the same thing as String#contact, but allows a separator to be inserted between the
|
4
|
+
#two strings.
|
5
|
+
def concat_with(str, separator="")
|
6
|
+
return self if str.nil? or str.empty?
|
7
|
+
return self.concat(str) if self.empty?
|
8
|
+
self.concat(separator) if !separator.empty?
|
9
|
+
self.concat(str)
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_yes_no(options={})
|
13
|
+
self.to_bool(options).to_yes_no(options)
|
6
14
|
end
|
7
15
|
|
8
16
|
#true will always be returned if we can clearly match one of the true cases
|
9
17
|
#In unstrict mode, the string is assumed false if we cannot match true
|
10
18
|
#In strict mode, the string must clearly match a false condition to return false
|
11
19
|
#otherise an error is raised
|
12
|
-
|
13
|
-
|
20
|
+
def to_bool(options={})
|
21
|
+
strip = options.fetch(:strip, true)
|
22
|
+
strict = options.fetch(:strict, false)
|
23
|
+
str = strip ? self.strip : self
|
24
|
+
return true if str =~ /\A(true|t|yes|y|1)\Z/i
|
25
|
+
|
26
|
+
if strict
|
27
|
+
return false if str.empty? || str =~ /\A(false|f|no|n|0)\Z/i
|
28
|
+
raise ArgumentError.new("cannot convert \"#{str}\" to boolean")
|
29
|
+
end
|
30
|
+
|
31
|
+
false
|
32
|
+
end
|
33
|
+
|
34
|
+
def not_empty?
|
35
|
+
!self.empty?
|
36
|
+
end
|
37
|
+
|
38
|
+
#Returns the subset of a string from [0, position] if string[position] is a space.
|
39
|
+
#If string[max] is not a space, it is assumed we are in the middle of a word.
|
40
|
+
#and the logic will increase position a little bit to not break in the middle of a word.
|
41
|
+
def excerpt_to_end_of_word(position=nil)
|
42
|
+
return self if position.nil? or position >= self.size
|
43
|
+
|
44
|
+
char = self[position]
|
45
|
+
return self[0, position].rstrip if char == " "
|
46
|
+
|
47
|
+
self[0,index_of_next_space_from(position)].rstrip
|
48
|
+
end
|
14
49
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
50
|
+
#Given a position, return the position of the next space
|
51
|
+
def index_of_next_space_from(position)
|
52
|
+
return nil if self.empty? or position.nil?
|
53
|
+
return nil if position >= self.size
|
19
54
|
|
20
|
-
|
21
|
-
|
55
|
+
idx = position
|
56
|
+
(self.size - position).times do
|
57
|
+
idx = idx + 1
|
58
|
+
return idx if self[idx] == " "
|
59
|
+
end
|
60
|
+
idx
|
61
|
+
end
|
22
62
|
|
23
63
|
end
|
data/lib/corelib/version.rb
CHANGED
data/spec/array/core_spec.rb
CHANGED
@@ -1,7 +1,19 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Array do
|
4
|
-
describe "#
|
4
|
+
describe "#to_yes_no" do
|
5
|
+
it "converts correctly" do
|
6
|
+
[false, true].to_yes_no.should == ["No", "Yes"]
|
7
|
+
[1, 0].to_yes_no.should == ["Yes", "No"]
|
8
|
+
["false", "t"].to_yes_no.should == ["No", "Yes"]
|
9
|
+
end
|
10
|
+
it "converts correctly with options" do
|
11
|
+
#This only tests that the options array is correctly passed along
|
12
|
+
#All the various options are tested in other tests
|
13
|
+
[false, true].to_yes_no(:format => "Up").should == ["NO", "YES"]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
describe "#sum" do
|
5
17
|
it 'sums correctly with all positive or all negative numbers' do
|
6
18
|
[0, 1,2,3,10,100, 5].sum.should == 121
|
7
19
|
[0, -1,-2,-3, -10, -100, -6].sum.should == -122
|
@@ -12,5 +24,50 @@ describe Array do
|
|
12
24
|
it 'throws an error with letters & numbers in strict mode' do
|
13
25
|
lambda {[0, "a","should be 0","", nil, 3, 9].sum({:strict => true})}.should raise_error TypeError
|
14
26
|
end
|
15
|
-
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#add_blank_option" do
|
30
|
+
it 'adds blank to front of array' do
|
31
|
+
array = [["Label 1", 1], ["Label 2", 2]]
|
32
|
+
array.add_blank_option
|
33
|
+
array.first[0].should == ""
|
34
|
+
array.first[1].should == 0
|
35
|
+
end
|
36
|
+
it 'does not add blank to front of array when doit is false' do
|
37
|
+
array = [["Label 1", 1], ["Label 2", 2]]
|
38
|
+
array.add_blank_option(:doit => false)
|
39
|
+
array.first[0].should == "Label 1"
|
40
|
+
array.first[1].should == 1
|
41
|
+
end
|
42
|
+
it 'handles specified label correctly' do
|
43
|
+
array = [["Label 1", 1], ["Label 2", 2]]
|
44
|
+
array.add_blank_option(:label => "Custom")
|
45
|
+
array.first[0].should == "Custom"
|
46
|
+
array.first[1].should == 0
|
47
|
+
end
|
48
|
+
it 'handles specified value correctly' do
|
49
|
+
array = [["Label 1", 1], ["Label 2", 2]]
|
50
|
+
array.add_blank_option(:value => 5)
|
51
|
+
array.first[0].should == ""
|
52
|
+
array.first[1].should == 5
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#each_with_end_flag" do
|
57
|
+
it 'works' do
|
58
|
+
passes = []
|
59
|
+
["a", "b", "c"].each_with_end_flag do |item, flag|
|
60
|
+
passes.push(item) if flag
|
61
|
+
end
|
62
|
+
passes.size.should == 1
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "#not_empty?" do
|
67
|
+
it "works" do
|
68
|
+
[].not_empty?.should == false
|
69
|
+
[1].not_empty?.should == true
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
16
73
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Array do
|
4
|
+
describe "#us_state_options" do
|
5
|
+
it "has the right number of entries" do
|
6
|
+
Array.us_state_options.size.should == 52
|
7
|
+
Array.us_state_options(:blank => false).size.should == 51
|
8
|
+
end
|
9
|
+
it "returns the correct list" do
|
10
|
+
Array.us_state_options[1][0].should == "Alabama"
|
11
|
+
Array.us_state_options(:abbrev => true)[1][0].should == "AL"
|
12
|
+
end
|
13
|
+
it "formats correctly" do
|
14
|
+
Array.us_state_options(:format => "Up")[1][0].should == "ALABAMA"
|
15
|
+
Array.us_state_options(:format => "Down")[1][0].should == "alabama"
|
16
|
+
Array.us_state_options(:format => "Cap")[1][0].should == "Alabama"
|
17
|
+
Array.us_state_options(:format => "Up", :abbrev => true)[1][0].should == "AL"
|
18
|
+
Array.us_state_options(:format => "Down", :abbrev => true)[1][0].should == "al"
|
19
|
+
Array.us_state_options(:format => "Cap", :abbrev => true)[1][0].should == "Al"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#options_label_for" do
|
24
|
+
it "returns the correct label" do
|
25
|
+
Array.us_state_options.options_label_for("AK").should == "Alaska"
|
26
|
+
end
|
27
|
+
it "returns nil when nothing is found" do
|
28
|
+
Array.us_state_options.options_label_for("invalid").should == nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FalseClass do
|
4
|
+
describe "#to_yes_no" do
|
5
|
+
it 'converts correctly with no options' do
|
6
|
+
false.to_yes_no.should == "No"
|
7
|
+
end
|
8
|
+
it 'converts correctly when if_no is set' do
|
9
|
+
false.to_yes_no(:if_no => "no way").should == "no way"
|
10
|
+
end
|
11
|
+
it 'converts correctly to uppercase' do
|
12
|
+
false.to_yes_no(:format => "Up").should == "NO"
|
13
|
+
end
|
14
|
+
it 'converts correctly to uppercase when if_no is set' do
|
15
|
+
false.to_yes_no(:format => "Up", :if_no => "no way").should == "NO WAY"
|
16
|
+
end
|
17
|
+
it 'converts correctly to downcase' do
|
18
|
+
false.to_yes_no(:format => "Down").should == "no"
|
19
|
+
end
|
20
|
+
it 'converts correctly to downcase when if_no is set' do
|
21
|
+
false.to_yes_no(:format => "Down", :if_no => "NO WAY").should == "no way"
|
22
|
+
end
|
23
|
+
it 'converts correctly when format is an unsupported string' do
|
24
|
+
false.to_yes_no(:format => "Hokie's Rules").should == "No"
|
25
|
+
end
|
26
|
+
it 'converts correctly when format is an unsupported string and if_no is set' do
|
27
|
+
false.to_yes_no(:format => "Hokie's Rules", :if_no => "chuck norris").should == "Chuck norris"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TrueClass do
|
4
|
+
describe "#to_yes_no" do
|
5
|
+
it 'converts correctly with no options' do
|
6
|
+
true.to_yes_no.should == "Yes"
|
7
|
+
end
|
8
|
+
it 'converts correctly when if_yes is set' do
|
9
|
+
true.to_yes_no(:if_yes => "yes way").should == "yes way"
|
10
|
+
end
|
11
|
+
it 'converts correctly to uppercase' do
|
12
|
+
true.to_yes_no(:format => "Up").should == "YES"
|
13
|
+
end
|
14
|
+
it 'converts correctly to uppercase when if_yes is set' do
|
15
|
+
true.to_yes_no(:format => "Up", :if_yes => "Yes way").should == "YES WAY"
|
16
|
+
end
|
17
|
+
it 'converts correctly to downcase' do
|
18
|
+
true.to_yes_no(:format => "Down").should == "yes"
|
19
|
+
end
|
20
|
+
it 'converts correctly to downcase when if_yes is set' do
|
21
|
+
true.to_yes_no(:format => "Down", :if_yes => "YES WAY").should == "yes way"
|
22
|
+
end
|
23
|
+
it 'converts correctly when format is an unsupported string' do
|
24
|
+
true.to_yes_no(:format => "Hokie's Rules").should == "Yes"
|
25
|
+
end
|
26
|
+
it 'converts correctly when format is an unsupported string and if_yes is set' do
|
27
|
+
true.to_yes_no(:format => "Hokie's Rules", :if_yes => "chuck norris").should == "Chuck norris"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hash do
|
4
|
+
describe "#each_with_end_flag" do
|
5
|
+
it 'works' do
|
6
|
+
passes = []
|
7
|
+
[:first => "1st", :second => "2nd", :third => "3rd"].each_with_end_flag do |item, flag|
|
8
|
+
passes.push(item) if flag
|
9
|
+
end
|
10
|
+
passes.size.should == 1
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#not_empty?" do
|
15
|
+
it "works" do
|
16
|
+
{}.not_empty?.should == false
|
17
|
+
{:test => "me"}.not_empty?.should == true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe NilClass do
|
4
|
+
describe "#to_yes_no" do
|
5
|
+
it 'converts correctly with no options' do
|
6
|
+
nil.to_yes_no.should == ""
|
7
|
+
end
|
8
|
+
it 'converts correctly with if_nil option' do
|
9
|
+
nil.to_yes_no(:if_nil => "Unspecified") == "Unspecified"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#not_nil?" do
|
14
|
+
it 'works' do
|
15
|
+
nil.not_nil?.should == false
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe String do
|
4
|
+
describe "#to_bool" do
|
5
|
+
it 'converts to true correctly' do
|
6
|
+
"true".to_bool.should == true
|
7
|
+
"TRUE".to_bool.should == true
|
8
|
+
"t".to_bool.should == true
|
9
|
+
"T".to_bool.should == true
|
10
|
+
"1".to_bool.should == true
|
11
|
+
"yes".to_bool.should == true
|
12
|
+
"YES".to_bool.should == true
|
13
|
+
end
|
14
|
+
it 'converts to false correctly in unstrict mode' do
|
15
|
+
"false".to_bool.should == false
|
16
|
+
"r".to_bool.should == false
|
17
|
+
"0".to_bool.should == false
|
18
|
+
"Chuck Norris".to_bool.should == false
|
19
|
+
"".to_bool.should == false
|
20
|
+
end
|
21
|
+
it 'converts correctly when using no_strip' do
|
22
|
+
"false".to_bool(:strip => false).should == false
|
23
|
+
"true".to_bool(:strip => false).should == true
|
24
|
+
"true ".to_bool(:strip => false).should == false
|
25
|
+
" 1 ".to_bool(:strip => false).should == false
|
26
|
+
end
|
27
|
+
it 'converts to false correctly in strict mode' do
|
28
|
+
"false".to_bool(:strict => true).should == false
|
29
|
+
"FAlSE".to_bool(:strict => true).should == false
|
30
|
+
"f".to_bool(:strict => true).should == false
|
31
|
+
"F".to_bool(:strict => true).should == false
|
32
|
+
"0".to_bool(:strict => true).should == false
|
33
|
+
"".to_bool(:strict => true).should == false
|
34
|
+
" ".to_bool(:strict => true).should == false
|
35
|
+
end
|
36
|
+
it 'throws an error when not true and not strictly "false"' do
|
37
|
+
expect{ "blah".to_bool(:strict => true) }.to raise_error(ArgumentError)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "#to_yes_no" do
|
42
|
+
it 'converts correctly with no options' do
|
43
|
+
"false".to_yes_no.should == "No"
|
44
|
+
"FALSE".to_yes_no.should == "No"
|
45
|
+
" FALSE ".to_yes_no.should == "No"
|
46
|
+
"f".to_yes_no.should == "No"
|
47
|
+
"F".to_yes_no.should == "No"
|
48
|
+
"".to_yes_no.should == "No"
|
49
|
+
" ".to_yes_no.should == "No"
|
50
|
+
"0".to_yes_no.should == "No"
|
51
|
+
"true".to_yes_no.should == "Yes"
|
52
|
+
"TRUE".to_yes_no.should == "Yes"
|
53
|
+
"t".to_yes_no.should == "Yes"
|
54
|
+
"T".to_yes_no.should == "Yes"
|
55
|
+
"1".to_yes_no.should == "Yes"
|
56
|
+
" 1 ".to_yes_no.should == "Yes"
|
57
|
+
end
|
58
|
+
it 'converts correctly with format options' do
|
59
|
+
"false".to_yes_no(:format => "Up").should == "NO"
|
60
|
+
"false".to_yes_no(:format => "Down").should == "no"
|
61
|
+
"true".to_yes_no(:format => "Up").should == "YES"
|
62
|
+
"true".to_yes_no(:format => "Down").should == "yes"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "#concat_with" do
|
67
|
+
it 'appends correctly' do
|
68
|
+
"".concat_with("test").should == "test"
|
69
|
+
"".concat_with("test",",").should == "test"
|
70
|
+
"my".concat_with("test").should == "mytest"
|
71
|
+
"my".concat_with("test",",").should == "my,test"
|
72
|
+
end
|
73
|
+
it 'does not append separators with nil or empty strings' do
|
74
|
+
"my".concat_with("",",").should == "my"
|
75
|
+
"my".concat_with(nil,",").should == "my"
|
76
|
+
"".concat_with("",",").should == ""
|
77
|
+
"".concat_with(nil,",").should == ""
|
78
|
+
end
|
79
|
+
it 'does not create a new string' do
|
80
|
+
str = ""
|
81
|
+
str_id = str.object_id
|
82
|
+
new_str = str.concat("test")
|
83
|
+
new_str.object_id.should == str_id
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe "#index_of_next_space_from" do
|
88
|
+
it 'calculates correctly with no trailing whitespace' do
|
89
|
+
str = "This is string"
|
90
|
+
str.index_of_next_space_from(0).should == 4
|
91
|
+
str.index_of_next_space_from(5).should == 7
|
92
|
+
str.index_of_next_space_from(8).should == 14
|
93
|
+
end
|
94
|
+
it 'calculates correctly with trailing whitespace' do
|
95
|
+
str = "This is string "
|
96
|
+
str.index_of_next_space_from(10).should == 14
|
97
|
+
str.index_of_next_space_from(14).should == 15
|
98
|
+
end
|
99
|
+
end
|
100
|
+
describe "#not_empty?" do
|
101
|
+
it "works" do
|
102
|
+
"".not_empty?.should == false
|
103
|
+
"test".not_empty?.should == true
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: corelib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-03-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -43,6 +43,7 @@ extensions: []
|
|
43
43
|
extra_rdoc_files: []
|
44
44
|
files:
|
45
45
|
- .gitignore
|
46
|
+
- CHANGELOG.md
|
46
47
|
- Gemfile
|
47
48
|
- LICENSE.txt
|
48
49
|
- README.md
|
@@ -51,14 +52,25 @@ files:
|
|
51
52
|
- lib/corelib.rb
|
52
53
|
- lib/corelib/array/alias.rb
|
53
54
|
- lib/corelib/array/core.rb
|
55
|
+
- lib/corelib/array/helpers.rb
|
54
56
|
- lib/corelib/array/math.rb
|
55
57
|
- lib/corelib/boolean/false.rb
|
56
58
|
- lib/corelib/boolean/true.rb
|
59
|
+
- lib/corelib/hash/core.rb
|
60
|
+
- lib/corelib/nil/core.rb
|
57
61
|
- lib/corelib/numeric/core.rb
|
62
|
+
- lib/corelib/object/core.rb
|
58
63
|
- lib/corelib/string/core.rb
|
59
64
|
- lib/corelib/version.rb
|
60
65
|
- spec/array/core_spec.rb
|
66
|
+
- spec/array/helpers_spec.rb
|
67
|
+
- spec/boolean/false_spec.rb
|
68
|
+
- spec/boolean/true_spec.rb
|
69
|
+
- spec/hash/core_spec.rb
|
70
|
+
- spec/nil/core_spec.rb
|
71
|
+
- spec/object/core_spec.rb
|
61
72
|
- spec/spec_helper.rb
|
73
|
+
- spec/string/core_spec.rb
|
62
74
|
homepage: https://github.com/corlewsolutions/corelib.git
|
63
75
|
licenses: []
|
64
76
|
post_install_message:
|
@@ -85,4 +97,11 @@ specification_version: 3
|
|
85
97
|
summary: ! '"Useful extensions & additions to the Ruby core classes'
|
86
98
|
test_files:
|
87
99
|
- spec/array/core_spec.rb
|
100
|
+
- spec/array/helpers_spec.rb
|
101
|
+
- spec/boolean/false_spec.rb
|
102
|
+
- spec/boolean/true_spec.rb
|
103
|
+
- spec/hash/core_spec.rb
|
104
|
+
- spec/nil/core_spec.rb
|
105
|
+
- spec/object/core_spec.rb
|
88
106
|
- spec/spec_helper.rb
|
107
|
+
- spec/string/core_spec.rb
|