utilise 0.1.0 → 0.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.
- checksums.yaml +4 -4
- data/README.md +38 -1
- data/lib/utilise/bool.rb +53 -10
- data/lib/utilise/matchers.rb +7 -0
- data/lib/utilise/time.rb +7 -0
- data/lib/utilise/version.rb +2 -2
- data/lib/utilise.rb +18 -0
- data/spec/array_spec.rb +73 -0
- data/spec/hash_spec.rb +73 -0
- data/spec/object_spec.rb +27 -0
- data/spec/time_spec.rb +17 -0
- metadata +12 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 145f9d52668c217f3df8bebb864e790f133f256d
|
4
|
+
data.tar.gz: 4dc0b61dee4aefd9b8fa6b94e0e55133ba0a5579
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25eedc6220d366c0ce79051a70f9e32cceb501abed91ffab9d05238b253a578904365e8c70c5a9a691d9458068917affd8155e3427ce3b02ecc72d9bb9b24112
|
7
|
+
data.tar.gz: bacbd6cfb4e0c54c9e7146625e07adc0ef9510c8006e078346ae5641fcac21cd0c597ad079e45f0a9d303bfbd3e075a8f0ac09e4414e96b31c4eace47ef50a9b
|
data/README.md
CHANGED
@@ -8,10 +8,16 @@ Utilise
|
|
8
8
|
[](http://badge.fury.io/rb/utilise)
|
9
9
|
|
10
10
|
Extends classes to include the to_bool method
|
11
|
-
|
11
|
+
|
12
|
+
Currently extends:
|
12
13
|
* Fixnum
|
13
14
|
* String
|
14
15
|
* Symbol
|
16
|
+
* Hash
|
17
|
+
* Array
|
18
|
+
* Object
|
19
|
+
|
20
|
+
Hashes and Arrays will have all values iterated.
|
15
21
|
|
16
22
|
```ruby
|
17
23
|
gem install utilise
|
@@ -30,6 +36,12 @@ require 'utilise'
|
|
30
36
|
|
31
37
|
:t.to_bool
|
32
38
|
=> true
|
39
|
+
|
40
|
+
{'key' => 1}.to_bool
|
41
|
+
=> {'key' => true}
|
42
|
+
|
43
|
+
[1].to_bool
|
44
|
+
=> [true]
|
33
45
|
```
|
34
46
|
|
35
47
|
```ruby
|
@@ -41,4 +53,29 @@ require 'utilise'
|
|
41
53
|
|
42
54
|
:f.to_bool
|
43
55
|
=> false
|
56
|
+
|
57
|
+
{'key' => 0}.to_bool
|
58
|
+
=> {'key' => false}
|
59
|
+
|
60
|
+
[0].to_bool
|
61
|
+
=> [false]
|
62
|
+
```
|
63
|
+
|
64
|
+
```
|
65
|
+
Time.unique
|
66
|
+
=> "80347765"
|
44
67
|
```
|
68
|
+
|
69
|
+
```
|
70
|
+
true.is_bool?
|
71
|
+
=> true
|
72
|
+
|
73
|
+
false.is_bool?
|
74
|
+
=> true
|
75
|
+
|
76
|
+
1.is_bool?
|
77
|
+
=> false
|
78
|
+
|
79
|
+
"true".is_bool?
|
80
|
+
=> false
|
81
|
+
```
|
data/lib/utilise/bool.rb
CHANGED
@@ -1,12 +1,55 @@
|
|
1
1
|
module Utilise
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
2
|
+
module Bool
|
3
|
+
|
4
|
+
# Returns bool based on object passed
|
5
|
+
#
|
6
|
+
# @param object [Object] the object
|
7
|
+
# @return [Object] returns object boolean
|
8
|
+
def to_bool object = self
|
9
|
+
case object
|
10
|
+
when String, Fixnum, Symbol
|
11
|
+
bool_it object
|
12
|
+
when Hash
|
13
|
+
bool_hash object
|
14
|
+
when Array
|
15
|
+
bool_array object
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Iterates through hash and updates each key
|
20
|
+
# value to a boolean if it matches rules
|
21
|
+
#
|
22
|
+
# @param hash [Hash] the hash
|
23
|
+
def bool_hash hash
|
24
|
+
hash.each do |k,v|
|
25
|
+
val = to_bool(hash[k])
|
26
|
+
hash[k] = val unless val.nil?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Iterates through array and updates each
|
31
|
+
# element to a boolean if it matches rules
|
32
|
+
#
|
33
|
+
# @param array [Array] the array
|
34
|
+
def bool_array array
|
35
|
+
array.each_index do |i|
|
36
|
+
val = to_bool(array[i])
|
37
|
+
array[i] = val unless val.nil?
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Returns boolean value if object matches rules
|
42
|
+
#
|
43
|
+
# @param array [Array] the array
|
44
|
+
# @returns [Boolean] true/false returned
|
45
|
+
def bool_it object
|
46
|
+
case object.to_s
|
47
|
+
when /true/i, /yes/i, /t/i, /1/i
|
48
|
+
true
|
49
|
+
when /false/i, /no/i, /f/i, /0/i
|
50
|
+
false
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
12
55
|
end
|
data/lib/utilise/time.rb
ADDED
data/lib/utilise/version.rb
CHANGED
data/lib/utilise.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require 'utilise/bool.rb'
|
2
|
+
require 'utilise/matchers.rb'
|
3
|
+
require 'utilise/time.rb'
|
2
4
|
require 'utilise/version.rb'
|
3
5
|
|
4
6
|
class String
|
@@ -11,4 +13,20 @@ end
|
|
11
13
|
|
12
14
|
class Symbol
|
13
15
|
include Utilise::Bool
|
16
|
+
end
|
17
|
+
|
18
|
+
class Object
|
19
|
+
include Utilise::Matchers
|
20
|
+
end
|
21
|
+
|
22
|
+
class Time
|
23
|
+
extend Utilise::Time
|
24
|
+
end
|
25
|
+
|
26
|
+
class Hash
|
27
|
+
include Utilise::Bool
|
28
|
+
end
|
29
|
+
|
30
|
+
class Array
|
31
|
+
include Utilise::Bool
|
14
32
|
end
|
data/spec/array_spec.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe Array do
|
4
|
+
describe "#to_bool" do
|
5
|
+
it "returns array with true when key value is 1" do
|
6
|
+
expect([1].to_bool).to eql([true])
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns array with false when key value is 0" do
|
10
|
+
expect([0].to_bool).to eql([false])
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns original array when key value is not 0 or 1" do
|
14
|
+
expect([2].to_bool).to eql([2])
|
15
|
+
end
|
16
|
+
|
17
|
+
it "returns array with true when key value is 'yes'" do
|
18
|
+
expect(['yes'].to_bool).to eql([true])
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns array with false when key value is 'no'" do
|
22
|
+
expect(['no'].to_bool).to eql([false])
|
23
|
+
end
|
24
|
+
|
25
|
+
it "returns array with true when key value is 'true'" do
|
26
|
+
expect(['true'].to_bool).to eql([true])
|
27
|
+
end
|
28
|
+
|
29
|
+
it "returns array with false when key value is 'false'" do
|
30
|
+
expect(['false'].to_bool).to eql([false])
|
31
|
+
end
|
32
|
+
|
33
|
+
it "returns original array when key value is not 'yes', 'no', 'true' or 'false'" do
|
34
|
+
expect(['value'].to_bool).to eql(['value'])
|
35
|
+
end
|
36
|
+
|
37
|
+
it "returns array with true when key value is :t" do
|
38
|
+
expect([:t].to_bool).to eql([true])
|
39
|
+
end
|
40
|
+
|
41
|
+
it "returns array with false when key value is :f" do
|
42
|
+
expect([:f].to_bool).to eql([false])
|
43
|
+
end
|
44
|
+
|
45
|
+
it "returns array with true when key value is :t" do
|
46
|
+
expect([:yes].to_bool).to eql([true])
|
47
|
+
end
|
48
|
+
|
49
|
+
it "returns array with false when key value is :f" do
|
50
|
+
expect([:f].to_bool).to eql([false])
|
51
|
+
end
|
52
|
+
|
53
|
+
it "returns array with true when key value is :true" do
|
54
|
+
expect([:true].to_bool).to eql([true])
|
55
|
+
end
|
56
|
+
|
57
|
+
it "returns array with false when key value is :false" do
|
58
|
+
expect([:false].to_bool).to eql([false])
|
59
|
+
end
|
60
|
+
|
61
|
+
it "returns original array when key value is not :t or :n" do
|
62
|
+
expect([:symbol].to_bool).to eql([:symbol])
|
63
|
+
end
|
64
|
+
|
65
|
+
it "returns array with multiple correct values" do
|
66
|
+
expect([1,0,'yes','false',:t,:f,:true,:false].to_bool).to eql([true,false,true,false,true,false,true,false])
|
67
|
+
end
|
68
|
+
|
69
|
+
it "returns multi level array with correct values" do
|
70
|
+
expect([[1,[0]]].to_bool).to eql([[true,[false]]])
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/spec/hash_spec.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe Hash do
|
4
|
+
describe "#to_bool" do
|
5
|
+
it "returns hash with true when key value is 1" do
|
6
|
+
expect({'key' => 1}.to_bool).to eql({'key' => true})
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns hash with false when key value is 0" do
|
10
|
+
expect({'key' => 0}.to_bool).to eql({'key' => false})
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns original hash when key value is not 0 or 1" do
|
14
|
+
expect({'key' => 2}.to_bool).to eql({'key' => 2})
|
15
|
+
end
|
16
|
+
|
17
|
+
it "returns hash with true when key value is 'yes'" do
|
18
|
+
expect({'key' => 'yes'}.to_bool).to eql({'key' => true})
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns hash with false when key value is 'no'" do
|
22
|
+
expect({'key' => 'no'}.to_bool).to eql({'key' => false})
|
23
|
+
end
|
24
|
+
|
25
|
+
it "returns hash with true when key value is 'true'" do
|
26
|
+
expect({'key' => 'true'}.to_bool).to eql({'key' => true})
|
27
|
+
end
|
28
|
+
|
29
|
+
it "returns hash with false when key value is 'false'" do
|
30
|
+
expect({'key' => 'false'}.to_bool).to eql({'key' => false})
|
31
|
+
end
|
32
|
+
|
33
|
+
it "returns original hash when key value is not 'yes', 'no', 'true or 'false'" do
|
34
|
+
expect({'key' => 'value'}.to_bool).to eql({'key' => 'value'})
|
35
|
+
end
|
36
|
+
|
37
|
+
it "returns hash with true when key value is :t" do
|
38
|
+
expect({'key' => :t}.to_bool).to eql({'key' => true})
|
39
|
+
end
|
40
|
+
|
41
|
+
it "returns hash with false when key value is :f" do
|
42
|
+
expect({'key' => :f}.to_bool).to eql({'key' => false})
|
43
|
+
end
|
44
|
+
|
45
|
+
it "returns hash with true when key value is :t" do
|
46
|
+
expect({'key' => :yes}.to_bool).to eql({'key' => true})
|
47
|
+
end
|
48
|
+
|
49
|
+
it "returns hash with false when key value is :f" do
|
50
|
+
expect({'key' => :no}.to_bool).to eql({'key' => false})
|
51
|
+
end
|
52
|
+
|
53
|
+
it "returns hash with true when key value is :true" do
|
54
|
+
expect({'key' => :true}.to_bool).to eql({'key' => true})
|
55
|
+
end
|
56
|
+
|
57
|
+
it "returns hash with false when key value is :false" do
|
58
|
+
expect({'key' => :false}.to_bool).to eql({'key' => false})
|
59
|
+
end
|
60
|
+
|
61
|
+
it "returns original hash when key value is not :t or :n" do
|
62
|
+
expect({'key' => :symbol}.to_bool).to eql({'key' => :symbol})
|
63
|
+
end
|
64
|
+
|
65
|
+
it "returns correct hash with multiple values" do
|
66
|
+
expect({'key0'=> 1, 'key1'=>0, 'key2'=>'yes', 'key3'=>'false', 'key4'=>:t, 'key4'=>:f, 'key5'=>:true, 'key6' =>:false}.to_bool).to eql({'key0'=> true, 'key1'=>false, 'key2'=>true, 'key3'=>false, 'key4'=>true, 'key4'=>false, 'key5'=>true, 'key6' =>false})
|
67
|
+
end
|
68
|
+
|
69
|
+
it "returns correct multi-level hash with multiple values" do
|
70
|
+
expect({'key0' => {'key1' => 1}, 'key2' => {'key3' => 0}}.to_bool).to eql({'key0' => {'key1' => true}, 'key2' => {'key3' => false}})
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/spec/object_spec.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe TrueClass do
|
4
|
+
describe "#is_bool?" do
|
5
|
+
it "returns true when matching" do
|
6
|
+
expect(true.is_bool?).to be true
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe FalseClass do
|
12
|
+
describe "#is_bool?" do
|
13
|
+
it "returns true when matching" do
|
14
|
+
expect(false.is_bool?).to be true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe Object do
|
20
|
+
describe "#is_bool?" do
|
21
|
+
it "returns false when not matching" do
|
22
|
+
expect("true".is_bool?).to be false
|
23
|
+
expect(0.is_bool?).to be false
|
24
|
+
expect(:false.is_bool?).to be false
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/spec/time_spec.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe Time do
|
4
|
+
describe "#unique" do
|
5
|
+
it "returns a string" do
|
6
|
+
expect(Time.unique.class).to be String
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns a string number" do
|
10
|
+
expect(Time.unique.to_i.class).to be Fixnum
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns a number from time to 3 decimal places" do
|
14
|
+
expect(Time.unique).to eq Time.now.strftime("%s%3N")[-8..-1]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: utilise
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Slaughter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -75,12 +75,18 @@ files:
|
|
75
75
|
- README.md
|
76
76
|
- License.md
|
77
77
|
- lib/utilise/bool.rb
|
78
|
+
- lib/utilise/matchers.rb
|
79
|
+
- lib/utilise/time.rb
|
78
80
|
- lib/utilise/version.rb
|
79
81
|
- lib/utilise.rb
|
82
|
+
- spec/array_spec.rb
|
80
83
|
- spec/fixnum_spec.rb
|
84
|
+
- spec/hash_spec.rb
|
81
85
|
- spec/helper.rb
|
86
|
+
- spec/object_spec.rb
|
82
87
|
- spec/string_spec.rb
|
83
88
|
- spec/symbol_spec.rb
|
89
|
+
- spec/time_spec.rb
|
84
90
|
homepage: http://benslaughter.github.io/utilise/
|
85
91
|
licenses:
|
86
92
|
- MIT
|
@@ -106,8 +112,12 @@ signing_key:
|
|
106
112
|
specification_version: 4
|
107
113
|
summary: Utilises a few extra tools
|
108
114
|
test_files:
|
115
|
+
- spec/array_spec.rb
|
109
116
|
- spec/fixnum_spec.rb
|
117
|
+
- spec/hash_spec.rb
|
110
118
|
- spec/helper.rb
|
119
|
+
- spec/object_spec.rb
|
111
120
|
- spec/string_spec.rb
|
112
121
|
- spec/symbol_spec.rb
|
122
|
+
- spec/time_spec.rb
|
113
123
|
has_rdoc:
|