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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b40273d8345128c15662dbb6b8405b9cbe478f33
4
- data.tar.gz: 10e8b878e22f4bb36d968b528726e56b998dce83
3
+ metadata.gz: c0ac87a91fb82f694988f255c4be34ef9c573435
4
+ data.tar.gz: e455b0b884bdea156b1546bcf651b7f91d540c50
5
5
  SHA512:
6
- metadata.gz: c3eed0d06beb744ac7105a1a6bf9b6755735d8db87cce9929b71569299c301934b84559b5df2e15e2a2e776953234e0a204090ebc630a0a28ff970f6ae64a724
7
- data.tar.gz: 865b337465c7d99d0c8d91e1c5747ce4df155f1399c65317529732d8190f8887b6d9448c7070e259d6dcb759f90de89ef1a79333a7d17ed1defb3e146e53b75d
6
+ metadata.gz: cadb1a59d488749bbf0a67614262556374489506788a49e3a2cd0687c84521904ec3180aa5769c6c36514eff337ebc94fa7918e64e092449cdef4b35c9330f22
7
+ data.tar.gz: 90f8e2a12a538c73ad8006ffc3fd0971e9952806dabab9362fde193f57ec3d095f99ca89ea81ad161847456829ecd3b768d96817e38a201f108b77e0b5c77a8f
@@ -20,5 +20,7 @@ Gem::Specification.new do |gem|
20
20
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
21
  gem.require_paths = ["lib"]
22
22
 
23
+ gem.add_dependency 'activesupport', '>= 4.0.2'
24
+
23
25
  gem.add_development_dependency 'rspec'
24
26
  end
@@ -13,4 +13,6 @@ require "corelib/hash/core"
13
13
  require "corelib/nil/core"
14
14
  require "corelib/object/core"
15
15
  require "corelib/numeric/core"
16
- require "corelib/string/core"
16
+ require "corelib/string/core"
17
+
18
+ require "active_support/all"
@@ -1,5 +1,9 @@
1
1
  class Array
2
2
 
3
+ def extract_options!
4
+ last.is_a?(::Hash) ? pop : {}
5
+ end
6
+
3
7
  def to_yes_no(options={})
4
8
  self.collect {|e| e.to_yes_no(options)}
5
9
  end
@@ -2,38 +2,29 @@ class String
2
2
 
3
3
  #TODO - Needs Tests
4
4
  def last
5
- return nil if self.empty?
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
- return nil if self.empty?
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(str, options={})
18
- strip = options.fetch(:strip, true)
19
- (return strip ? self.strip : self.dup) if str.nil? or str.empty?
20
- (return strip ? str.strip : str.dup) if self.empty?
21
- separator = options.fetch(:separator, " ")
22
-
23
- if strip
24
- pre = self.strip
25
- post = str.strip
26
- else
27
- pre = self.dup
28
- post = str.dup
29
- end
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
- self.to_bool(options).to_yes_no(options)
50
- end
40
+ self.to_bool(options).to_yes_no(options)
41
+ end
51
42
 
52
- #true will always be returned if we can clearly match one of the true cases
53
- #In unstrict mode, the string is assumed false if we cannot match true
54
- #In strict mode, the string must clearly match a false condition to return false
55
- #otherise an error is raised
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
- raise ArgumentError.new("cannot convert \"#{str}\" to boolean")
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
@@ -1,3 +1,3 @@
1
1
  module Corelib
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -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.5
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-10 00:00:00.000000000 Z
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