corelib 0.0.6 → 0.0.7
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.
- checksums.yaml +5 -5
- data/lib/corelib/array/alias.rb +9 -9
- data/lib/corelib/array/core.rb +38 -38
- data/lib/corelib/array/helpers.rb +112 -112
- data/lib/corelib/array/math.rb +26 -26
- data/lib/corelib/boolean/false.rb +16 -16
- data/lib/corelib/boolean/true.rb +8 -8
- data/lib/corelib/hash/core.rb +16 -16
- data/lib/corelib/nil/core.rb +10 -10
- data/lib/corelib/numeric/core.rb +56 -56
- data/lib/corelib/object/core.rb +6 -6
- data/lib/corelib/string/core.rb +113 -113
- data/lib/corelib/version.rb +1 -1
- data/spec/array/core_spec.rb +89 -89
- data/spec/array/helpers_spec.rb +50 -50
- data/spec/boolean/false_spec.rb +29 -29
- data/spec/boolean/true_spec.rb +29 -29
- data/spec/hash/core_spec.rb +19 -19
- data/spec/nil/core_spec.rb +17 -17
- data/spec/object/core_spec.rb +9 -9
- data/spec/spec_helper.rb +6 -6
- data/spec/string/core_spec.rb +124 -124
- metadata +11 -12
data/lib/corelib/nil/core.rb
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
class NilClass
|
|
2
|
-
|
|
3
|
-
def to_yes_no(options={})
|
|
4
|
-
options.fetch(:if_nil, "")
|
|
5
|
-
end
|
|
6
|
-
|
|
7
|
-
def not_nil?
|
|
8
|
-
false
|
|
9
|
-
end
|
|
10
|
-
|
|
1
|
+
class NilClass
|
|
2
|
+
|
|
3
|
+
def to_yes_no(options={})
|
|
4
|
+
options.fetch(:if_nil, "")
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def not_nil?
|
|
8
|
+
false
|
|
9
|
+
end
|
|
10
|
+
|
|
11
11
|
end
|
data/lib/corelib/numeric/core.rb
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
class Numeric
|
|
2
|
-
|
|
3
|
-
#format accepts (C-Capitalized, U-Uppercase, L-Lowercase)
|
|
4
|
-
def to_yes_no(options={})
|
|
5
|
-
(self == 1 || self == 1.0).to_yes_no(options)
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
#Assumes numeric value is in seconds
|
|
9
|
-
def to_days_hours_minutes_seconds
|
|
10
|
-
total_seconds = self.to_i
|
|
11
|
-
|
|
12
|
-
days = total_seconds / 86400
|
|
13
|
-
hours = (total_seconds / 3600) - (days * 24)
|
|
14
|
-
minutes = (total_seconds / 60) - (hours * 60) - (days * 1440)
|
|
15
|
-
seconds = total_seconds % 60
|
|
16
|
-
|
|
17
|
-
display = ''
|
|
18
|
-
display_concat = ''
|
|
19
|
-
if days > 0
|
|
20
|
-
display = display + display_concat + "#{days}d"
|
|
21
|
-
display_concat = ' '
|
|
22
|
-
end
|
|
23
|
-
if hours > 0 || display.length > 0
|
|
24
|
-
display = display + display_concat + "#{hours}h"
|
|
25
|
-
display_concat = ' '
|
|
26
|
-
end
|
|
27
|
-
if minutes > 0 || display.length > 0
|
|
28
|
-
display = display + display_concat + "#{minutes}m"
|
|
29
|
-
display_concat = ' '
|
|
30
|
-
end
|
|
31
|
-
display = display + display_concat + "#{seconds}s"
|
|
32
|
-
display
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
#Assumes numeric value is in seconds
|
|
36
|
-
def to_hours_minutes(if_zero="")
|
|
37
|
-
total_seconds = self.to_i
|
|
38
|
-
|
|
39
|
-
return if_zero if total_seconds < 60
|
|
40
|
-
|
|
41
|
-
hours = (total_seconds / 3600)
|
|
42
|
-
minutes = (total_seconds / 60) - (hours * 60)
|
|
43
|
-
|
|
44
|
-
display = ''
|
|
45
|
-
display_concat = ''
|
|
46
|
-
|
|
47
|
-
if hours > 0
|
|
48
|
-
display = display + "#{hours}h"
|
|
49
|
-
display_concat = ' '
|
|
50
|
-
end
|
|
51
|
-
if minutes > 0 || display.length > 0
|
|
52
|
-
display = display + display_concat + "#{minutes}m"
|
|
53
|
-
end
|
|
54
|
-
display
|
|
55
|
-
end
|
|
56
|
-
|
|
1
|
+
class Numeric
|
|
2
|
+
|
|
3
|
+
#format accepts (C-Capitalized, U-Uppercase, L-Lowercase)
|
|
4
|
+
def to_yes_no(options={})
|
|
5
|
+
(self == 1 || self == 1.0).to_yes_no(options)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
#Assumes numeric value is in seconds
|
|
9
|
+
def to_days_hours_minutes_seconds
|
|
10
|
+
total_seconds = self.to_i
|
|
11
|
+
|
|
12
|
+
days = total_seconds / 86400
|
|
13
|
+
hours = (total_seconds / 3600) - (days * 24)
|
|
14
|
+
minutes = (total_seconds / 60) - (hours * 60) - (days * 1440)
|
|
15
|
+
seconds = total_seconds % 60
|
|
16
|
+
|
|
17
|
+
display = ''
|
|
18
|
+
display_concat = ''
|
|
19
|
+
if days > 0
|
|
20
|
+
display = display + display_concat + "#{days}d"
|
|
21
|
+
display_concat = ' '
|
|
22
|
+
end
|
|
23
|
+
if hours > 0 || display.length > 0
|
|
24
|
+
display = display + display_concat + "#{hours}h"
|
|
25
|
+
display_concat = ' '
|
|
26
|
+
end
|
|
27
|
+
if minutes > 0 || display.length > 0
|
|
28
|
+
display = display + display_concat + "#{minutes}m"
|
|
29
|
+
display_concat = ' '
|
|
30
|
+
end
|
|
31
|
+
display = display + display_concat + "#{seconds}s"
|
|
32
|
+
display
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
#Assumes numeric value is in seconds
|
|
36
|
+
def to_hours_minutes(if_zero="")
|
|
37
|
+
total_seconds = self.to_i
|
|
38
|
+
|
|
39
|
+
return if_zero if total_seconds < 60
|
|
40
|
+
|
|
41
|
+
hours = (total_seconds / 3600)
|
|
42
|
+
minutes = (total_seconds / 60) - (hours * 60)
|
|
43
|
+
|
|
44
|
+
display = ''
|
|
45
|
+
display_concat = ''
|
|
46
|
+
|
|
47
|
+
if hours > 0
|
|
48
|
+
display = display + "#{hours}h"
|
|
49
|
+
display_concat = ' '
|
|
50
|
+
end
|
|
51
|
+
if minutes > 0 || display.length > 0
|
|
52
|
+
display = display + display_concat + "#{minutes}m"
|
|
53
|
+
end
|
|
54
|
+
display
|
|
55
|
+
end
|
|
56
|
+
|
|
57
57
|
end
|
data/lib/corelib/object/core.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
class Object
|
|
2
|
-
|
|
3
|
-
def not_nil?
|
|
4
|
-
true
|
|
5
|
-
end
|
|
6
|
-
|
|
1
|
+
class Object
|
|
2
|
+
|
|
3
|
+
def not_nil?
|
|
4
|
+
true
|
|
5
|
+
end
|
|
6
|
+
|
|
7
7
|
end
|
data/lib/corelib/string/core.rb
CHANGED
|
@@ -1,113 +1,113 @@
|
|
|
1
|
-
class String
|
|
2
|
-
|
|
3
|
-
#TODO - Needs Tests
|
|
4
|
-
def last
|
|
5
|
-
self.empty? ? nil : self[-1,1]
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
#TODO - Needs Tests
|
|
9
|
-
def first
|
|
10
|
-
self
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
#TODO - Needs Tests
|
|
14
|
-
# Combines two strings together with a separator.
|
|
15
|
-
def combine(*args)
|
|
16
|
-
options = args.extract_options!
|
|
17
|
-
raise ArgumentError, "You need to supply at least one string" if args.empty?
|
|
18
|
-
str = self
|
|
19
|
-
args.each { |val| str = str.priv_combine(val, options) }
|
|
20
|
-
|
|
21
|
-
return options.fetch(:if_empty, "") if str.blank?
|
|
22
|
-
|
|
23
|
-
prefix = options.fetch(:prefix, nil)
|
|
24
|
-
str = "#{prefix}#{str}" if options.fetch(:wrap, "true") and (prefix.not_nil?)
|
|
25
|
-
suffix = options.fetch(:suffix, nil)
|
|
26
|
-
str = "#{str}#{suffix}" if options.fetch(:wrap, "true") and (suffix.not_nil?)
|
|
27
|
-
str
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
#Does the same thing as String#contact, but allows a separator to be inserted between the
|
|
31
|
-
#two strings.
|
|
32
|
-
def concat_with(str, separator="")
|
|
33
|
-
return self if str.nil? or str.empty?
|
|
34
|
-
return self.concat(str) if self.empty?
|
|
35
|
-
self.concat(separator) unless separator.empty?
|
|
36
|
-
self.concat(str)
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
def to_yes_no(options={})
|
|
40
|
-
self.to_bool(options).to_yes_no(options)
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
#true will always be returned if we can clearly match one of the true cases
|
|
44
|
-
#In unstrict mode, the string is assumed false if we cannot match true
|
|
45
|
-
#In strict mode, the string must clearly match a false condition to return false
|
|
46
|
-
#otherise an error is raised
|
|
47
|
-
def to_bool(options={})
|
|
48
|
-
strip = options.fetch(:strip, true)
|
|
49
|
-
strict = options.fetch(:strict, false)
|
|
50
|
-
str = strip ? self.strip : self
|
|
51
|
-
return true if str =~ /\A(true|t|yes|y|1)\Z/i
|
|
52
|
-
|
|
53
|
-
if strict
|
|
54
|
-
return false if str.empty? || str =~ /\A(false|f|no|n|0)\Z/i
|
|
55
|
-
raise ArgumentError.new("cannot convert \"#{str}\" to boolean")
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
false
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
def not_empty?
|
|
62
|
-
!self.empty?
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
#Returns the subset of a string from [0, position] if string[position] is a space.
|
|
66
|
-
#If string[max] is not a space, it is assumed we are in the middle of a word.
|
|
67
|
-
#and the logic will increase position a little bit to not break in the middle of a word.
|
|
68
|
-
def excerpt_to_end_of_word(position=nil)
|
|
69
|
-
return self if position.nil? or position >= self.size
|
|
70
|
-
|
|
71
|
-
char = self[position]
|
|
72
|
-
return self[0, position].rstrip if char == " "
|
|
73
|
-
|
|
74
|
-
self[0,index_of_next_space_from(position)].rstrip
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
#Given a position, return the position of the next space
|
|
78
|
-
def index_of_next_space_from(position)
|
|
79
|
-
return nil if self.empty? or position.nil?
|
|
80
|
-
return nil if position >= self.size
|
|
81
|
-
|
|
82
|
-
idx = position
|
|
83
|
-
(self.size - position).times do
|
|
84
|
-
idx = idx + 1
|
|
85
|
-
return idx if self[idx] == " "
|
|
86
|
-
end
|
|
87
|
-
idx
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
protected
|
|
91
|
-
|
|
92
|
-
def priv_combine(str, options={})
|
|
93
|
-
strip = options.fetch(:strip, true)
|
|
94
|
-
(return strip ? self.strip : self.dup) if str.nil? or str.empty?
|
|
95
|
-
(return strip ? str.strip : str.dup) if self.empty?
|
|
96
|
-
separator = options.fetch(:separator, " ")
|
|
97
|
-
|
|
98
|
-
if strip
|
|
99
|
-
pre = self.strip
|
|
100
|
-
post = str.strip
|
|
101
|
-
else
|
|
102
|
-
pre = self.dup
|
|
103
|
-
post = str.dup
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
return pre + post if separator.empty?
|
|
107
|
-
|
|
108
|
-
# TODO - Support other separators other than spaces. For instance if someone wanted to join with a comma
|
|
109
|
-
# and pre ended with a comma, we could have an option to disallow repeating
|
|
110
|
-
pre + separator + post
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
end
|
|
1
|
+
class String
|
|
2
|
+
|
|
3
|
+
#TODO - Needs Tests
|
|
4
|
+
def last
|
|
5
|
+
self.empty? ? nil : self[-1,1]
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
#TODO - Needs Tests
|
|
9
|
+
def first(limit = 1)
|
|
10
|
+
self[0, limit] || raise(ArgumentError, "negative limit")
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
#TODO - Needs Tests
|
|
14
|
+
# Combines two strings together with a separator.
|
|
15
|
+
def combine(*args)
|
|
16
|
+
options = args.extract_options!
|
|
17
|
+
raise ArgumentError, "You need to supply at least one string" if args.empty?
|
|
18
|
+
str = self
|
|
19
|
+
args.each { |val| str = str.priv_combine(val, options) }
|
|
20
|
+
|
|
21
|
+
return options.fetch(:if_empty, "") if str.blank?
|
|
22
|
+
|
|
23
|
+
prefix = options.fetch(:prefix, nil)
|
|
24
|
+
str = "#{prefix}#{str}" if options.fetch(:wrap, "true") and (prefix.not_nil?)
|
|
25
|
+
suffix = options.fetch(:suffix, nil)
|
|
26
|
+
str = "#{str}#{suffix}" if options.fetch(:wrap, "true") and (suffix.not_nil?)
|
|
27
|
+
str
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
#Does the same thing as String#contact, but allows a separator to be inserted between the
|
|
31
|
+
#two strings.
|
|
32
|
+
def concat_with(str, separator="")
|
|
33
|
+
return self if str.nil? or str.empty?
|
|
34
|
+
return self.concat(str) if self.empty?
|
|
35
|
+
self.concat(separator) unless separator.empty?
|
|
36
|
+
self.concat(str)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def to_yes_no(options={})
|
|
40
|
+
self.to_bool(options).to_yes_no(options)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
#true will always be returned if we can clearly match one of the true cases
|
|
44
|
+
#In unstrict mode, the string is assumed false if we cannot match true
|
|
45
|
+
#In strict mode, the string must clearly match a false condition to return false
|
|
46
|
+
#otherise an error is raised
|
|
47
|
+
def to_bool(options={})
|
|
48
|
+
strip = options.fetch(:strip, true)
|
|
49
|
+
strict = options.fetch(:strict, false)
|
|
50
|
+
str = strip ? self.strip : self
|
|
51
|
+
return true if str =~ /\A(true|t|yes|y|1)\Z/i
|
|
52
|
+
|
|
53
|
+
if strict
|
|
54
|
+
return false if str.empty? || str =~ /\A(false|f|no|n|0)\Z/i
|
|
55
|
+
raise ArgumentError.new("cannot convert \"#{str}\" to boolean")
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
false
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def not_empty?
|
|
62
|
+
!self.empty?
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
#Returns the subset of a string from [0, position] if string[position] is a space.
|
|
66
|
+
#If string[max] is not a space, it is assumed we are in the middle of a word.
|
|
67
|
+
#and the logic will increase position a little bit to not break in the middle of a word.
|
|
68
|
+
def excerpt_to_end_of_word(position=nil)
|
|
69
|
+
return self if position.nil? or position >= self.size
|
|
70
|
+
|
|
71
|
+
char = self[position]
|
|
72
|
+
return self[0, position].rstrip if char == " "
|
|
73
|
+
|
|
74
|
+
self[0,index_of_next_space_from(position)].rstrip
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
#Given a position, return the position of the next space
|
|
78
|
+
def index_of_next_space_from(position)
|
|
79
|
+
return nil if self.empty? or position.nil?
|
|
80
|
+
return nil if position >= self.size
|
|
81
|
+
|
|
82
|
+
idx = position
|
|
83
|
+
(self.size - position).times do
|
|
84
|
+
idx = idx + 1
|
|
85
|
+
return idx if self[idx] == " "
|
|
86
|
+
end
|
|
87
|
+
idx
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
protected
|
|
91
|
+
|
|
92
|
+
def priv_combine(str, options={})
|
|
93
|
+
strip = options.fetch(:strip, true)
|
|
94
|
+
(return strip ? self.strip : self.dup) if str.nil? or str.empty?
|
|
95
|
+
(return strip ? str.strip : str.dup) if self.empty?
|
|
96
|
+
separator = options.fetch(:separator, " ")
|
|
97
|
+
|
|
98
|
+
if strip
|
|
99
|
+
pre = self.strip
|
|
100
|
+
post = str.strip
|
|
101
|
+
else
|
|
102
|
+
pre = self.dup
|
|
103
|
+
post = str.dup
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
return pre + post if separator.empty?
|
|
107
|
+
|
|
108
|
+
# TODO - Support other separators other than spaces. For instance if someone wanted to join with a comma
|
|
109
|
+
# and pre ended with a comma, we could have an option to disallow repeating
|
|
110
|
+
pre + separator + post
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
end
|
data/lib/corelib/version.rb
CHANGED
data/spec/array/core_spec.rb
CHANGED
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe Array do
|
|
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
|
|
17
|
-
it 'sums correctly with all positive or all negative numbers' do
|
|
18
|
-
[0, 1,2,3,10,100, 5].sum.should == 121
|
|
19
|
-
[0, -1,-2,-3, -10, -100, -6].sum.should == -122
|
|
20
|
-
end
|
|
21
|
-
it 'sums correctly with letters & numbers in loose mode' do
|
|
22
|
-
[0, "a","should be 0","", nil, 3, 9].sum({:strict => false}).should == 12
|
|
23
|
-
end
|
|
24
|
-
it 'throws an error with letters & numbers in strict mode' do
|
|
25
|
-
lambda {[0, "a","should be 0","", nil, 3, 9].sum({:strict => true})}.should raise_error TypeError
|
|
26
|
-
end
|
|
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
|
-
|
|
73
|
-
describe "#add_all" do
|
|
74
|
-
it "adds items correctly" do
|
|
75
|
-
[].add_all([1,2]).should == [1,2]
|
|
76
|
-
end
|
|
77
|
-
it "does not create a new array" do
|
|
78
|
-
a = []
|
|
79
|
-
b = a.add_all([1,2])
|
|
80
|
-
a.should === b
|
|
81
|
-
end
|
|
82
|
-
it "flattens multidimensional array" do
|
|
83
|
-
[].add_all([0,[1,2]]).should == [0,1,2]
|
|
84
|
-
end
|
|
85
|
-
it "does not flatten multidimensional array when option is set" do
|
|
86
|
-
[].add_all([0,[1,2]], :flatten => false).should == [0,[1,2]]
|
|
87
|
-
end
|
|
88
|
-
end
|
|
89
|
-
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Array do
|
|
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
|
|
17
|
+
it 'sums correctly with all positive or all negative numbers' do
|
|
18
|
+
[0, 1,2,3,10,100, 5].sum.should == 121
|
|
19
|
+
[0, -1,-2,-3, -10, -100, -6].sum.should == -122
|
|
20
|
+
end
|
|
21
|
+
it 'sums correctly with letters & numbers in loose mode' do
|
|
22
|
+
[0, "a","should be 0","", nil, 3, 9].sum({:strict => false}).should == 12
|
|
23
|
+
end
|
|
24
|
+
it 'throws an error with letters & numbers in strict mode' do
|
|
25
|
+
lambda {[0, "a","should be 0","", nil, 3, 9].sum({:strict => true})}.should raise_error TypeError
|
|
26
|
+
end
|
|
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
|
+
|
|
73
|
+
describe "#add_all" do
|
|
74
|
+
it "adds items correctly" do
|
|
75
|
+
[].add_all([1,2]).should == [1,2]
|
|
76
|
+
end
|
|
77
|
+
it "does not create a new array" do
|
|
78
|
+
a = []
|
|
79
|
+
b = a.add_all([1,2])
|
|
80
|
+
a.should === b
|
|
81
|
+
end
|
|
82
|
+
it "flattens multidimensional array" do
|
|
83
|
+
[].add_all([0,[1,2]]).should == [0,1,2]
|
|
84
|
+
end
|
|
85
|
+
it "does not flatten multidimensional array when option is set" do
|
|
86
|
+
[].add_all([0,[1,2]], :flatten => false).should == [0,[1,2]]
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
90
|
end
|