inactive_support 1.1.0 → 1.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 +7 -0
- data/Gemfile +1 -0
- data/README.md +109 -21
- data/Rakefile +19 -1
- data/inactive_support.gemspec +12 -12
- data/lib/inactive_support/array/butfirst.rb +12 -0
- data/lib/inactive_support/array/butlast.rb +12 -0
- data/lib/inactive_support/array.rb +3 -0
- data/lib/inactive_support/enumerable/consecutive_by.rb +1 -0
- data/lib/inactive_support/enumerable.rb +1 -0
- data/lib/inactive_support/hash/deep_delete_blank.rb +9 -0
- data/lib/inactive_support/hash/delete_blank.rb +1 -7
- data/lib/inactive_support/hash.rb +1 -0
- data/lib/inactive_support/numeric/precision.rb +1 -0
- data/lib/inactive_support/numeric.rb +1 -0
- data/lib/inactive_support/object/blank.rb +7 -0
- data/lib/inactive_support/object/deep_dup.rb +18 -0
- data/lib/inactive_support/object/identity.rb +1 -0
- data/lib/inactive_support/object/try.rb +3 -2
- data/lib/inactive_support/object.rb +2 -0
- data/lib/inactive_support/version.rb +2 -1
- data/lib/inactive_support.rb +2 -1
- data/spec/lib/array_spec.rb +24 -0
- data/spec/lib/enumerable_spec.rb +10 -9
- data/spec/lib/hash_spec.rb +25 -24
- data/spec/lib/numeric_spec.rb +20 -19
- data/spec/lib/object_spec.rb +83 -6
- data/spec/spec_helper.rb +1 -2
- metadata +27 -34
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 68dfe17ac231e2265d7fbce57336eec8e8722ae9
|
4
|
+
data.tar.gz: cc5a0e382e812d8420adf6a531f28ee973f3501f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bb4bb36a96a270068643645fe0018019bf3f343043a895a080c7e8527e567e22e7c15381f73760e7d1f57df8f29904690d2deef74cd5bd8471448b2ea2995df7
|
7
|
+
data.tar.gz: 4cb0dcbce228f3115c735a2ce38e21094191919f497b3e77748a1bda0885fa7a088b5376fe2194cd15515224dc29b3fead696d137eed4af62e51602c1e833916
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# InactiveSupport
|
2
2
|
|
3
|
-
A collection of
|
3
|
+
A collection of useful extensions for the Ruby programming language.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -16,41 +16,129 @@ Or install it yourself as:
|
|
16
16
|
|
17
17
|
$ gem install inactive_support
|
18
18
|
|
19
|
-
##
|
19
|
+
## Extensions
|
20
20
|
|
21
|
-
|
22
|
-
|
21
|
+
#### Object
|
22
|
+
|
23
|
+
##### #blank?
|
24
|
+
Returns true if the object is blank:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
[].blank?
|
28
|
+
# => true
|
29
|
+
|
30
|
+
{}.blank?
|
31
|
+
# => true
|
32
|
+
|
33
|
+
''.blank?
|
34
|
+
# => true
|
35
|
+
|
36
|
+
' '.blank?
|
37
|
+
# => true
|
38
|
+
|
39
|
+
"\n\t".blank?
|
40
|
+
# => true
|
41
|
+
|
42
|
+
'A man'.blank?
|
43
|
+
# => false
|
44
|
+
""
|
45
|
+
```
|
46
|
+
|
47
|
+
##### #identity
|
23
48
|
returns self
|
24
49
|
|
25
|
-
|
26
|
-
|
50
|
+
```ruby
|
51
|
+
[1, 2, 3, 3, 4, 5, 5].group_by(&:identity)
|
52
|
+
# => [[1], [2], [3, 3], [4], [5, 5]]
|
53
|
+
```
|
27
54
|
|
28
|
-
|
55
|
+
##### #try
|
29
56
|
send a message to the receiver and if it doesn't respond to the message, return nil
|
30
57
|
|
31
|
-
|
32
|
-
|
58
|
+
```ruby
|
59
|
+
'A string'.some_method
|
60
|
+
# => NoMethodError: undefined method `some_method' for "A string":String
|
61
|
+
|
62
|
+
'A string'.try(:some_method)
|
63
|
+
# => nil
|
64
|
+
```
|
33
65
|
|
34
|
-
|
66
|
+
##### #ctry
|
35
67
|
chained try, for methods with no arguments
|
36
68
|
|
37
|
-
|
38
|
-
|
69
|
+
```ruby
|
70
|
+
"Somestring".ctry(:mb_chars, :downcase, :some_method)
|
71
|
+
# => nil
|
72
|
+
```
|
73
|
+
|
74
|
+
##### #deep_dup
|
75
|
+
Object#dup has a hidden pitfall:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
person = { name: { first: 'Gavin' } }
|
79
|
+
dupl = person.dup
|
39
80
|
|
40
|
-
|
41
|
-
|
81
|
+
#this also changes person when it shouldn't
|
82
|
+
dupl[:name][:first] = 'Dazen'
|
83
|
+
|
84
|
+
person
|
85
|
+
# => {:name=>{:first=>"Dazen"}}
|
86
|
+
```
|
87
|
+
|
88
|
+
Object#deep_dup fixes this behavior:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
person = { name: { first: 'Gavin' } }
|
92
|
+
dupl = person.deep_dup
|
93
|
+
|
94
|
+
#this also changes person when it shouldn't
|
95
|
+
dupl[:name][:first] = 'Dazen'
|
96
|
+
|
97
|
+
person
|
98
|
+
# => {:name=>{:first=>"Gavin"}}
|
99
|
+
```
|
100
|
+
|
101
|
+
#### Hash
|
102
|
+
##### #delete_blank
|
42
103
|
Deletes all key/value pairs where the value is an empty string/array/hash or nil.
|
43
104
|
|
44
|
-
|
45
|
-
|
105
|
+
```ruby
|
106
|
+
{ name: nil, age: 19, address: "" }.delete_blank
|
107
|
+
# => { age: 19 }
|
108
|
+
```
|
109
|
+
|
110
|
+
##### #deep_delete_blank
|
111
|
+
Recursively deletes all key/value pairs where the value is an empty string/array/hash or nil.
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
{ name: nil, age: 19, address: { street_name: 'Vitosha', street_number: nil }, }.deep_delete_blank
|
115
|
+
# => { age: 19, address: { street_name: 'Vitosha' } }
|
116
|
+
```
|
117
|
+
|
118
|
+
#### Enumerable
|
119
|
+
##### #consecutive_by
|
120
|
+
Groups objects by an attribute that is consecutive
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
[1, 2, 3, 5, 6, 8, 9].consecutive_by(&:identity)
|
124
|
+
# => [[1, 2, 3], [5, 6], [8, 9]]
|
125
|
+
```
|
126
|
+
|
127
|
+
##### #consecutive?
|
128
|
+
Returns true if the objects are consecutive
|
46
129
|
|
47
|
-
|
48
|
-
|
49
|
-
|
130
|
+
```ruby
|
131
|
+
[1, 2, 3, 5, 6, 8, 9].consecutive?
|
132
|
+
# => false
|
133
|
+
```
|
50
134
|
|
51
|
-
|
52
|
-
|
135
|
+
##### #sorted?
|
136
|
+
Returns true if the collection is sorted
|
53
137
|
|
138
|
+
```ruby
|
139
|
+
[1, 2, 3, 3, 5, 6, 8, 9].sorted?
|
140
|
+
# => true
|
141
|
+
```
|
54
142
|
|
55
143
|
## Contributing
|
56
144
|
|
data/Rakefile
CHANGED
@@ -1 +1,19 @@
|
|
1
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
require 'rake'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
7
|
+
task.rspec_opts = ['--color']
|
8
|
+
end
|
9
|
+
|
10
|
+
task default: :spec
|
11
|
+
task test: :spec
|
12
|
+
|
13
|
+
task :console do
|
14
|
+
require 'irb'
|
15
|
+
require 'irb/completion'
|
16
|
+
require 'inactive_support'
|
17
|
+
ARGV.clear
|
18
|
+
IRB.start
|
19
|
+
end
|
data/inactive_support.gemspec
CHANGED
@@ -4,21 +4,21 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'inactive_support/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'inactive_support'
|
8
8
|
spec.version = InactiveSupport::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.description =
|
12
|
-
spec.summary =
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
9
|
+
spec.authors = ['gmitrev']
|
10
|
+
spec.email = ['gvmitrev@gmail.com']
|
11
|
+
spec.description = 'A collection of utilities and extensions'
|
12
|
+
spec.summary = 'Snippets and useful stuff extracted from my projects'
|
13
|
+
spec.homepage = 'https://github.com/gmitrev/inactive_support'
|
14
|
+
spec.license = 'MIT'
|
15
15
|
|
16
|
-
spec.files = `git ls-files`.split(
|
16
|
+
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
-
spec.require_paths = [
|
19
|
+
spec.require_paths = ['lib']
|
20
20
|
|
21
|
-
spec.add_development_dependency
|
22
|
-
spec.add_development_dependency
|
23
|
-
spec.add_development_dependency
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_development_dependency 'rspec', '~> 3.3'
|
24
24
|
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
class Hash
|
3
|
+
# Recursively deletes all key/value pairs where the value is empty string/array/hash or nil.
|
4
|
+
def deep_delete_blank
|
5
|
+
delete_if do |_, v|
|
6
|
+
(v.blank? && v != false) || v.instance_of?(Hash) && v.deep_delete_blank.empty?
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
class Hash
|
2
3
|
# Deletes all key/value pairs where the value is empty string/array/hash or nil.
|
3
4
|
def delete_blank
|
@@ -5,11 +6,4 @@ class Hash
|
|
5
6
|
(v.blank? && v != false)
|
6
7
|
end
|
7
8
|
end
|
8
|
-
|
9
|
-
# Recursively deletes all key/value pairs where the value is empty string/array/hash or nil.
|
10
|
-
def deep_delete_blank
|
11
|
-
delete_if do |_, v|
|
12
|
-
(v.blank? && v != false) || v.instance_of?(Hash) && v.deep_delete_blank.empty?
|
13
|
-
end
|
14
|
-
end
|
15
9
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
class Object
|
3
|
+
# Object#dup is shallow and therefore useless:
|
4
|
+
#
|
5
|
+
# a = { name: { first: 'Georgi' } }
|
6
|
+
# dupl = a.dup
|
7
|
+
#
|
8
|
+
# #this also changes a when it shouldn't
|
9
|
+
# dupl[:name][:first] = 'Dazen'
|
10
|
+
#
|
11
|
+
# a
|
12
|
+
# => :name=>{:first=>"Dazen"}}
|
13
|
+
#
|
14
|
+
# deep_dup fixes this behavior
|
15
|
+
def deep_dup
|
16
|
+
Marshal.load(Marshal.dump(self))
|
17
|
+
end
|
18
|
+
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
class Object
|
2
3
|
# Credit goes to the active_support contributors
|
3
4
|
# https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/object/try.rb
|
@@ -33,11 +34,11 @@ class Object
|
|
33
34
|
end
|
34
35
|
|
35
36
|
class NilClass
|
36
|
-
def try(*
|
37
|
+
def try(*_args)
|
37
38
|
nil
|
38
39
|
end
|
39
40
|
|
40
|
-
def ctry(*
|
41
|
+
def ctry(*_args)
|
41
42
|
nil
|
42
43
|
end
|
43
44
|
end
|
data/lib/inactive_support.rb
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'inactive_support/array'
|
4
|
+
|
5
|
+
describe Array do
|
6
|
+
|
7
|
+
describe '#butfirst' do
|
8
|
+
it 'returns all elements excluding the first one' do
|
9
|
+
expect([].butfirst).to eq []
|
10
|
+
expect([1].butfirst).to eq []
|
11
|
+
expect([1, 2, 3].butfirst).to eq [2, 3]
|
12
|
+
expect([1, 2, 3, 4].butfirst).to eq [2, 3, 4]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#butlast' do
|
17
|
+
it 'returns all elements excluding the last one' do
|
18
|
+
expect([].butlast).to eq []
|
19
|
+
expect([1].butlast).to eq []
|
20
|
+
expect([1, 2, 3].butlast).to eq [1, 2]
|
21
|
+
expect([1, 2, 3, 4].butlast).to eq [1, 2, 3]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/spec/lib/enumerable_spec.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require 'spec_helper'
|
2
3
|
require 'inactive_support/enumerable'
|
3
4
|
|
@@ -12,19 +13,19 @@ describe Enumerable do
|
|
12
13
|
it 'partitions lists by consecutive attribute' do
|
13
14
|
simple_people = [mitko, goshko, krasi, dedo]
|
14
15
|
|
15
|
-
simple_people.consecutive_by(&:age).
|
16
|
+
expect(simple_people.consecutive_by(&:age)).to eq [[mitko, goshko], [krasi], [dedo]]
|
16
17
|
end
|
17
18
|
|
18
19
|
it 'sorts by the given method before partitioning' do
|
19
20
|
simple_people = [goshko, krasi, dedo, mitko]
|
20
21
|
|
21
|
-
simple_people.consecutive_by(&:age).
|
22
|
+
expect(simple_people.consecutive_by(&:age)).to eq [[mitko, goshko], [krasi], [dedo]]
|
22
23
|
end
|
23
24
|
|
24
25
|
it 'works on bare arrays' do
|
25
26
|
ary = [1, 2, 3, 5, 7, 8, 11, 12]
|
26
27
|
|
27
|
-
ary.consecutive_by(&:identity).
|
28
|
+
expect(ary.consecutive_by(&:identity)).to eq [[1, 2, 3], [5], [7, 8], [11, 12]]
|
28
29
|
end
|
29
30
|
|
30
31
|
describe 'with letters somewhere in the number' do
|
@@ -60,16 +61,16 @@ describe Enumerable do
|
|
60
61
|
consecutive_people = [mitko, goshko]
|
61
62
|
non_consecutive_people = [mitko, goshko, krasi]
|
62
63
|
|
63
|
-
consecutive_people.consecutive?(&:age).
|
64
|
+
expect(consecutive_people.consecutive?(&:age)).to be true
|
64
65
|
|
65
|
-
non_consecutive_people.consecutive?(&:age).
|
66
|
+
expect(non_consecutive_people.consecutive?(&:age)).to be false
|
66
67
|
end
|
67
68
|
|
68
69
|
it 'uses identity on the objects if no key is given' do
|
69
|
-
[1, 2, 3, 4].consecutive
|
70
|
-
[1, 2, 3, 5].consecutive
|
71
|
-
[2, 1, 3,
|
72
|
-
[].consecutive
|
70
|
+
expect([1, 2, 3, 4].consecutive?).to be true
|
71
|
+
expect([1, 2, 3, 5].consecutive?).to be false
|
72
|
+
expect([2, 1, 3, 4].consecutive?).to be false
|
73
|
+
expect([].consecutive?).to be false
|
73
74
|
end
|
74
75
|
|
75
76
|
end
|
data/spec/lib/hash_spec.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require 'spec_helper'
|
2
3
|
require 'inactive_support/hash/delete_blank'
|
3
4
|
|
4
5
|
describe Hash do
|
5
6
|
|
6
|
-
describe
|
7
|
+
describe '#delete_blank' do
|
7
8
|
|
8
9
|
it 'deletes nils' do
|
9
10
|
initial = {
|
@@ -15,20 +16,20 @@ describe Hash do
|
|
15
16
|
id: 1
|
16
17
|
}
|
17
18
|
|
18
|
-
initial.delete_blank.
|
19
|
+
expect(initial.delete_blank).to eq expected
|
19
20
|
end
|
20
21
|
|
21
22
|
it 'deletes empty strings' do
|
22
23
|
initial = {
|
23
24
|
id: 1,
|
24
|
-
name:
|
25
|
+
name: ''
|
25
26
|
}
|
26
27
|
|
27
28
|
expected = {
|
28
29
|
id: 1
|
29
30
|
}
|
30
31
|
|
31
|
-
initial.delete_blank.
|
32
|
+
expect(initial.delete_blank).to eq expected
|
32
33
|
end
|
33
34
|
|
34
35
|
it 'preserves false values' do
|
@@ -42,7 +43,7 @@ describe Hash do
|
|
42
43
|
name: false
|
43
44
|
}
|
44
45
|
|
45
|
-
initial.delete_blank.
|
46
|
+
expect(initial.delete_blank).to eq expected
|
46
47
|
end
|
47
48
|
|
48
49
|
it 'deletes empty arrays' do
|
@@ -55,7 +56,7 @@ describe Hash do
|
|
55
56
|
id: 1
|
56
57
|
}
|
57
58
|
|
58
|
-
initial.delete_blank.
|
59
|
+
expect(initial.delete_blank).to eq expected
|
59
60
|
|
60
61
|
end
|
61
62
|
it 'deletes empty hashes' do
|
@@ -68,50 +69,50 @@ describe Hash do
|
|
68
69
|
id: 1
|
69
70
|
}
|
70
71
|
|
71
|
-
initial.delete_blank.
|
72
|
+
expect(initial.delete_blank).to eq expected
|
72
73
|
|
73
74
|
end
|
74
75
|
|
75
76
|
end
|
76
77
|
|
77
|
-
describe
|
78
|
+
describe '#deep_delete_blank' do
|
78
79
|
|
79
80
|
it 'deletes nils' do
|
80
81
|
initial = {
|
81
82
|
id: 1,
|
82
83
|
name: {
|
83
84
|
first: nil,
|
84
|
-
middle:
|
85
|
+
middle: 'Peter'
|
85
86
|
}
|
86
87
|
}
|
87
88
|
|
88
89
|
expected = {
|
89
90
|
id: 1,
|
90
91
|
name: {
|
91
|
-
middle:
|
92
|
+
middle: 'Peter'
|
92
93
|
}
|
93
94
|
}
|
94
95
|
|
95
|
-
initial.deep_delete_blank.
|
96
|
+
expect(initial.deep_delete_blank).to eq expected
|
96
97
|
end
|
97
98
|
|
98
99
|
it 'deletes empty strings' do
|
99
100
|
initial = {
|
100
101
|
id: 1,
|
101
102
|
name: {
|
102
|
-
first:
|
103
|
-
middle:
|
103
|
+
first: '',
|
104
|
+
middle: 'Peter'
|
104
105
|
}
|
105
106
|
}
|
106
107
|
|
107
108
|
expected = {
|
108
109
|
id: 1,
|
109
110
|
name: {
|
110
|
-
middle:
|
111
|
+
middle: 'Peter'
|
111
112
|
}
|
112
113
|
}
|
113
114
|
|
114
|
-
initial.deep_delete_blank.
|
115
|
+
expect(initial.deep_delete_blank).to eq expected
|
115
116
|
end
|
116
117
|
|
117
118
|
it 'preserves false values' do
|
@@ -119,7 +120,7 @@ describe Hash do
|
|
119
120
|
id: 1,
|
120
121
|
name: {
|
121
122
|
first: false,
|
122
|
-
middle:
|
123
|
+
middle: 'Peter'
|
123
124
|
}
|
124
125
|
}
|
125
126
|
|
@@ -127,11 +128,11 @@ describe Hash do
|
|
127
128
|
id: 1,
|
128
129
|
name: {
|
129
130
|
first: false,
|
130
|
-
middle:
|
131
|
+
middle: 'Peter'
|
131
132
|
}
|
132
133
|
}
|
133
134
|
|
134
|
-
initial.deep_delete_blank.
|
135
|
+
expect(initial.deep_delete_blank).to eq expected
|
135
136
|
end
|
136
137
|
|
137
138
|
it 'deletes empty arrays' do
|
@@ -139,18 +140,18 @@ describe Hash do
|
|
139
140
|
id: 1,
|
140
141
|
name: {
|
141
142
|
addresses: [],
|
142
|
-
middle:
|
143
|
+
middle: 'Peter'
|
143
144
|
}
|
144
145
|
}
|
145
146
|
|
146
147
|
expected = {
|
147
148
|
id: 1,
|
148
149
|
name: {
|
149
|
-
middle:
|
150
|
+
middle: 'Peter'
|
150
151
|
}
|
151
152
|
}
|
152
153
|
|
153
|
-
initial.deep_delete_blank.
|
154
|
+
expect(initial.deep_delete_blank).to eq expected
|
154
155
|
|
155
156
|
end
|
156
157
|
it 'deletes empty hashes' do
|
@@ -158,18 +159,18 @@ describe Hash do
|
|
158
159
|
id: 1,
|
159
160
|
name: {
|
160
161
|
children: {},
|
161
|
-
middle:
|
162
|
+
middle: 'Peter'
|
162
163
|
}
|
163
164
|
}
|
164
165
|
|
165
166
|
expected = {
|
166
167
|
id: 1,
|
167
168
|
name: {
|
168
|
-
middle:
|
169
|
+
middle: 'Peter'
|
169
170
|
}
|
170
171
|
}
|
171
172
|
|
172
|
-
initial.deep_delete_blank.
|
173
|
+
expect(initial.deep_delete_blank).to eq expected
|
173
174
|
end
|
174
175
|
|
175
176
|
end
|
data/spec/lib/numeric_spec.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require 'spec_helper'
|
2
3
|
require 'inactive_support/numeric'
|
3
4
|
|
4
5
|
describe Numeric do
|
5
6
|
|
6
|
-
describe
|
7
|
+
describe '#precision' do
|
7
8
|
|
8
9
|
it 'behaves exactly like Float#round when invoked with no arguments', :brute do
|
9
10
|
1_000_000.times do
|
10
11
|
n = rand(1.0..100.0)
|
11
12
|
puts n if n.precision != n.round
|
12
|
-
n.precision.
|
13
|
+
expect(n.precision).to eq n.round
|
13
14
|
end
|
14
15
|
end
|
15
16
|
|
@@ -17,30 +18,30 @@ describe Numeric do
|
|
17
18
|
1_000.times do
|
18
19
|
n = rand(1.0..100.0)
|
19
20
|
puts n if n.precision != n.round
|
20
|
-
n.precision.
|
21
|
+
expect(n.precision).to eq n.round
|
21
22
|
end
|
22
23
|
end
|
23
24
|
|
24
|
-
describe
|
25
|
-
|
26
|
-
it
|
27
|
-
3.904605.precision(0).
|
28
|
-
3.904605.precision(1).
|
29
|
-
3.904605.precision(2).
|
30
|
-
3.904605.precision(3).
|
31
|
-
3.904605.precision(4).
|
25
|
+
describe 'reality checks' do
|
26
|
+
|
27
|
+
it 'case#1' do
|
28
|
+
expect(3.904605.precision(0)).to eq 4
|
29
|
+
expect(3.904605.precision(1)).to eq 3.9
|
30
|
+
expect(3.904605.precision(2)).to eq 3.91
|
31
|
+
expect(3.904605.precision(3)).to eq 3.905
|
32
|
+
expect(3.904605.precision(4)).to eq 3.9046
|
32
33
|
end
|
33
34
|
|
34
|
-
it
|
35
|
-
37.9945.precision(0).
|
36
|
-
37.9945.precision(1).
|
37
|
-
37.9945.precision(2).
|
35
|
+
it 'case#2' do
|
36
|
+
expect(37.9945.precision(0)).to eq 38
|
37
|
+
expect(37.9945.precision(1)).to eq 38
|
38
|
+
expect(37.9945.precision(2)).to eq 38
|
38
39
|
end
|
39
40
|
|
40
|
-
it
|
41
|
-
37.9944.precision(0).
|
42
|
-
37.9944.precision(1).
|
43
|
-
37.9944.precision(2).
|
41
|
+
it 'case#3' do
|
42
|
+
expect(37.9944.precision(0)).to eq 38
|
43
|
+
expect(37.9944.precision(1)).to eq 38
|
44
|
+
expect(37.9944.precision(2)).to eq 37.99
|
44
45
|
end
|
45
46
|
|
46
47
|
end
|
data/spec/lib/object_spec.rb
CHANGED
@@ -1,20 +1,97 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require 'spec_helper'
|
2
3
|
require 'inactive_support/object'
|
3
4
|
|
4
5
|
describe Object do
|
5
6
|
|
6
|
-
describe
|
7
|
-
it
|
8
|
-
|
7
|
+
describe '#ctry' do
|
8
|
+
it 'works with 1 argument' do
|
9
|
+
expect('String'.ctry(:downcase)).to eq 'string'
|
9
10
|
end
|
10
11
|
|
11
|
-
it
|
12
|
-
|
12
|
+
it 'works with multiple arguments' do
|
13
|
+
expect('Nurse I spy gypsies run'.ctry(:downcase, :reverse)).to eq 'nur seispyg yps i esrun'
|
13
14
|
end
|
14
15
|
|
15
16
|
it 'returns nil when called on nil' do
|
16
|
-
nil.ctry(:downcase, :upcase).
|
17
|
+
expect(nil.ctry(:downcase, :upcase)).to eq nil
|
17
18
|
end
|
18
19
|
end
|
19
20
|
|
21
|
+
describe '#blank' do
|
22
|
+
it 'works with arrays' do
|
23
|
+
expect([].blank?).to be true
|
24
|
+
expect([1].blank?).to be false
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'works with hashes' do
|
28
|
+
expect({}.blank?).to be true
|
29
|
+
expect({ name: 'Kip' }.blank?).to be false
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'works with strings' do
|
33
|
+
blank = [
|
34
|
+
'',
|
35
|
+
' ',
|
36
|
+
' ',
|
37
|
+
"\t",
|
38
|
+
"\r",
|
39
|
+
"\n"
|
40
|
+
]
|
41
|
+
|
42
|
+
not_blank = [
|
43
|
+
' a ',
|
44
|
+
'string'
|
45
|
+
]
|
46
|
+
|
47
|
+
blank.each do |string|
|
48
|
+
expect(string.blank?).to be true
|
49
|
+
end
|
50
|
+
|
51
|
+
not_blank.each do |string|
|
52
|
+
expect(string.blank?).to be false
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '#deep_dup' do
|
58
|
+
|
59
|
+
it 'works with hashes' do
|
60
|
+
original = {
|
61
|
+
name: {
|
62
|
+
first: 'Gavin'
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
duplicate = original.deep_dup
|
67
|
+
|
68
|
+
# modify duplicate
|
69
|
+
expect do
|
70
|
+
duplicate[:name][:first] = 'Dazen'
|
71
|
+
end.not_to change { original[:name][:first] }
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'works with arrays' do
|
76
|
+
original = [1, 2, 3]
|
77
|
+
modified = original.deep_dup
|
78
|
+
|
79
|
+
expect do
|
80
|
+
modified.unshift(0)
|
81
|
+
end.not_to change { original }
|
82
|
+
|
83
|
+
expect do
|
84
|
+
modified.push(4)
|
85
|
+
end.not_to change { original }
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'works with strings' do
|
89
|
+
original = "I ain't grouchy, "
|
90
|
+
modified = original.deep_dup
|
91
|
+
|
92
|
+
expect do
|
93
|
+
modified += '“I just have a low threshold for stupidity'
|
94
|
+
end.not_to change { original }
|
95
|
+
end
|
96
|
+
end
|
20
97
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,14 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require 'bundler/setup'
|
2
3
|
Bundler.setup
|
3
4
|
|
4
5
|
require 'inactive_support'
|
5
6
|
|
6
7
|
RSpec.configure do |config|
|
7
|
-
config.treat_symbols_as_metadata_keys_with_true_values = true
|
8
8
|
config.run_all_when_everything_filtered = true
|
9
9
|
config.filter_run :focus
|
10
10
|
config.filter_run_excluding :brute
|
11
11
|
|
12
|
-
|
13
12
|
config.order = 'random'
|
14
13
|
end
|
metadata
CHANGED
@@ -1,64 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: inactive_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.2.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- gmitrev
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2015-
|
11
|
+
date: 2015-09-13 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: bundler
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- - ~>
|
17
|
+
- - "~>"
|
20
18
|
- !ruby/object:Gem::Version
|
21
|
-
version: '1.
|
19
|
+
version: '1.10'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- - ~>
|
24
|
+
- - "~>"
|
28
25
|
- !ruby/object:Gem::Version
|
29
|
-
version: '1.
|
26
|
+
version: '1.10'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - ">="
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - ">="
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rspec
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- - ~>
|
45
|
+
- - "~>"
|
52
46
|
- !ruby/object:Gem::Version
|
53
|
-
version: '
|
47
|
+
version: '3.3'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- - ~>
|
52
|
+
- - "~>"
|
60
53
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
54
|
+
version: '3.3'
|
62
55
|
description: A collection of utilities and extensions
|
63
56
|
email:
|
64
57
|
- gvmitrev@gmail.com
|
@@ -66,62 +59,62 @@ executables: []
|
|
66
59
|
extensions: []
|
67
60
|
extra_rdoc_files: []
|
68
61
|
files:
|
69
|
-
- .gitignore
|
70
|
-
- .rspec
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
71
64
|
- Gemfile
|
72
65
|
- LICENSE.txt
|
73
66
|
- README.md
|
74
67
|
- Rakefile
|
75
68
|
- inactive_support.gemspec
|
76
69
|
- lib/inactive_support.rb
|
70
|
+
- lib/inactive_support/array.rb
|
71
|
+
- lib/inactive_support/array/butfirst.rb
|
72
|
+
- lib/inactive_support/array/butlast.rb
|
77
73
|
- lib/inactive_support/enumerable.rb
|
78
74
|
- lib/inactive_support/enumerable/consecutive_by.rb
|
79
75
|
- lib/inactive_support/hash.rb
|
76
|
+
- lib/inactive_support/hash/deep_delete_blank.rb
|
80
77
|
- lib/inactive_support/hash/delete_blank.rb
|
81
78
|
- lib/inactive_support/numeric.rb
|
82
79
|
- lib/inactive_support/numeric/precision.rb
|
83
80
|
- lib/inactive_support/object.rb
|
84
81
|
- lib/inactive_support/object/blank.rb
|
82
|
+
- lib/inactive_support/object/deep_dup.rb
|
85
83
|
- lib/inactive_support/object/identity.rb
|
86
84
|
- lib/inactive_support/object/try.rb
|
87
85
|
- lib/inactive_support/version.rb
|
86
|
+
- spec/lib/array_spec.rb
|
88
87
|
- spec/lib/enumerable_spec.rb
|
89
88
|
- spec/lib/hash_spec.rb
|
90
89
|
- spec/lib/numeric_spec.rb
|
91
90
|
- spec/lib/object_spec.rb
|
92
91
|
- spec/spec_helper.rb
|
93
|
-
homepage:
|
92
|
+
homepage: https://github.com/gmitrev/inactive_support
|
94
93
|
licenses:
|
95
94
|
- MIT
|
95
|
+
metadata: {}
|
96
96
|
post_install_message:
|
97
97
|
rdoc_options: []
|
98
98
|
require_paths:
|
99
99
|
- lib
|
100
100
|
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
-
none: false
|
102
101
|
requirements:
|
103
|
-
- -
|
102
|
+
- - ">="
|
104
103
|
- !ruby/object:Gem::Version
|
105
104
|
version: '0'
|
106
|
-
segments:
|
107
|
-
- 0
|
108
|
-
hash: -4215888734430273764
|
109
105
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
-
none: false
|
111
106
|
requirements:
|
112
|
-
- -
|
107
|
+
- - ">="
|
113
108
|
- !ruby/object:Gem::Version
|
114
109
|
version: '0'
|
115
|
-
segments:
|
116
|
-
- 0
|
117
|
-
hash: -4215888734430273764
|
118
110
|
requirements: []
|
119
111
|
rubyforge_project:
|
120
|
-
rubygems_version:
|
112
|
+
rubygems_version: 2.4.5
|
121
113
|
signing_key:
|
122
|
-
specification_version:
|
114
|
+
specification_version: 4
|
123
115
|
summary: Snippets and useful stuff extracted from my projects
|
124
116
|
test_files:
|
117
|
+
- spec/lib/array_spec.rb
|
125
118
|
- spec/lib/enumerable_spec.rb
|
126
119
|
- spec/lib/hash_spec.rb
|
127
120
|
- spec/lib/numeric_spec.rb
|