corelib 0.0.5 → 0.0.6
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 +4 -4
- data/corelib.gemspec +2 -0
- data/lib/corelib.rb +3 -1
- data/lib/corelib/array/core.rb +4 -0
- data/lib/corelib/string/core.rb +45 -31
- data/lib/corelib/version.rb +1 -1
- data/spec/string/core_spec.rb +11 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0ac87a91fb82f694988f255c4be34ef9c573435
|
4
|
+
data.tar.gz: e455b0b884bdea156b1546bcf651b7f91d540c50
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cadb1a59d488749bbf0a67614262556374489506788a49e3a2cd0687c84521904ec3180aa5769c6c36514eff337ebc94fa7918e64e092449cdef4b35c9330f22
|
7
|
+
data.tar.gz: 90f8e2a12a538c73ad8006ffc3fd0971e9952806dabab9362fde193f57ec3d095f99ca89ea81ad161847456829ecd3b768d96817e38a201f108b77e0b5c77a8f
|
data/corelib.gemspec
CHANGED
data/lib/corelib.rb
CHANGED
data/lib/corelib/array/core.rb
CHANGED
data/lib/corelib/string/core.rb
CHANGED
@@ -2,38 +2,29 @@ class String
|
|
2
2
|
|
3
3
|
#TODO - Needs Tests
|
4
4
|
def last
|
5
|
-
|
6
|
-
self[-1,1]
|
5
|
+
self.empty? ? nil : self[-1,1]
|
7
6
|
end
|
8
7
|
|
9
8
|
#TODO - Needs Tests
|
10
9
|
def first
|
11
|
-
|
12
|
-
self[0,1]
|
10
|
+
self.empty? ? nil : self[0,1]
|
13
11
|
end
|
14
12
|
|
15
13
|
#TODO - Needs Tests
|
16
14
|
# Combines two strings together with a separator.
|
17
|
-
def combine(
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
if
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
return pre + post if separator.empty?
|
32
|
-
|
33
|
-
# TODO - Support other separators other than spaces. For instance if someone wanted to join with a comma
|
34
|
-
# and pre ended with a comma, we could have an option to disallow repeating
|
35
|
-
pre + separator + post
|
36
|
-
|
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
|
37
28
|
end
|
38
29
|
|
39
30
|
#Does the same thing as String#contact, but allows a separator to be inserted between the
|
@@ -46,13 +37,13 @@ class String
|
|
46
37
|
end
|
47
38
|
|
48
39
|
def to_yes_no(options={})
|
49
|
-
|
50
|
-
|
40
|
+
self.to_bool(options).to_yes_no(options)
|
41
|
+
end
|
51
42
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
56
47
|
def to_bool(options={})
|
57
48
|
strip = options.fetch(:strip, true)
|
58
49
|
strict = options.fetch(:strict, false)
|
@@ -61,7 +52,7 @@ class String
|
|
61
52
|
|
62
53
|
if strict
|
63
54
|
return false if str.empty? || str =~ /\A(false|f|no|n|0)\Z/i
|
64
|
-
|
55
|
+
raise ArgumentError.new("cannot convert \"#{str}\" to boolean")
|
65
56
|
end
|
66
57
|
|
67
58
|
false
|
@@ -96,4 +87,27 @@ class String
|
|
96
87
|
idx
|
97
88
|
end
|
98
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
|
+
|
99
113
|
end
|
data/lib/corelib/version.rb
CHANGED
data/spec/string/core_spec.rb
CHANGED
@@ -1,11 +1,22 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe String do
|
4
|
+
|
4
5
|
describe "#combine" do
|
5
6
|
it 'defaults work' do
|
6
7
|
"test".combine("test2").should == "test test2"
|
7
8
|
"".combine("test2").should=="test2"
|
8
9
|
"test".combine("").should == "test"
|
10
|
+
"test".combine("me", "out").should == "test me out"
|
11
|
+
end
|
12
|
+
it "setting prefix & suffix works" do
|
13
|
+
"test".combine("me", :prefix => "(").should == "(test me"
|
14
|
+
"test".combine("me", :suffix => ")").should == "test me)"
|
15
|
+
"test".combine("me", :prefix => "(", :suffix => ")").should == "(test me)"
|
16
|
+
"test".combine("me", :prefix => "(", :suffix => ")", :wrap => false).should == "test me"
|
17
|
+
end
|
18
|
+
it "if_empty option works" do
|
19
|
+
"".combine("", :if_empty => "-").should == "-"
|
9
20
|
end
|
10
21
|
end
|
11
22
|
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
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.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Corlew Solutions
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.0.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.0.2
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: rspec
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|