more_core_extensions 3.0.0 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -1
- data/lib/more_core_extensions/all.rb +1 -0
- data/lib/more_core_extensions/core_ext/array/deletes.rb +1 -1
- data/lib/more_core_extensions/core_ext/array/math.rb +35 -0
- data/lib/more_core_extensions/core_ext/array.rb +1 -0
- data/lib/more_core_extensions/core_ext/hash/deletes.rb +1 -1
- data/lib/more_core_extensions/core_ext/numeric/math.rb +14 -0
- data/lib/more_core_extensions/core_ext/numeric/rounding.rb +17 -0
- data/lib/more_core_extensions/core_ext/numeric.rb +2 -0
- data/lib/more_core_extensions/core_ext/object.rb +1 -1
- data/lib/more_core_extensions/version.rb +1 -1
- data/spec/core_ext/array/math_spec.rb +16 -0
- data/spec/core_ext/numeric/math_spec.rb +6 -0
- data/spec/core_ext/numeric/rounding_spec.rb +19 -0
- metadata +16 -7
- data/lib/more_core_extensions/core_ext/object/blank.rb +0 -149
- data/spec/core_ext/object/blank_spec.rb +0 -46
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de0bf2437f3829de0a5f82ebd288a6cac59f0a71
|
4
|
+
data.tar.gz: 386ac9e9871d381269e507143b72df08619bfd75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a132740693a2bd42601ef83c2cedb3680a742e7ad87a44c0643953d1f0f10a77c51118609532c184e21e5766404acdc90fcb0222e7b2a123d846c5d69dbec777
|
7
|
+
data.tar.gz: e7c599a6db36661dc88a85666aaac6ac6355441c059e9a54e04b6834f68359a4a970a104e9ba7321b037084527c49fa440db207096caccd303199aecdcd49c1f
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# MoreCoreExtensions
|
2
2
|
|
3
|
+
[![Join the chat at https://gitter.im/ManageIQ/more_core_extensions](https://badges.gitter.im/ManageIQ/more_core_extensions.svg)](https://gitter.im/ManageIQ/more_core_extensions?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
4
|
+
|
3
5
|
MoreCoreExtensions are a set of core extensions beyond those provided by ActiveSupport.
|
4
6
|
|
5
7
|
[![Gem Version](https://badge.fury.io/rb/more_core_extensions.svg)](http://badge.fury.io/rb/more_core_extensions)
|
@@ -14,8 +16,9 @@ MoreCoreExtensions are a set of core extensions beyond those provided by ActiveS
|
|
14
16
|
* delete_nils
|
15
17
|
* delete_blanks
|
16
18
|
* delete_blank_paths
|
17
|
-
* duplicates
|
18
19
|
* delete_path
|
20
|
+
* deviation
|
21
|
+
* duplicates
|
19
22
|
* include_any?
|
20
23
|
* include_none?
|
21
24
|
* include_all?
|
@@ -25,13 +28,16 @@ MoreCoreExtensions are a set of core extensions beyond those provided by ActiveS
|
|
25
28
|
* include_path?
|
26
29
|
* key_path?
|
27
30
|
* member_path?
|
31
|
+
* mean
|
28
32
|
* random_index
|
29
33
|
* random_element
|
34
|
+
* squares
|
30
35
|
* store_path
|
31
36
|
* stretch
|
32
37
|
* stretch!
|
33
38
|
* zip_stretched
|
34
39
|
* tableize
|
40
|
+
* variance
|
35
41
|
* Hash
|
36
42
|
* delete_nils
|
37
43
|
* delete_blanks
|
@@ -44,6 +50,10 @@ MoreCoreExtensions are a set of core extensions beyond those provided by ActiveS
|
|
44
50
|
* key_path?
|
45
51
|
* member_path?
|
46
52
|
* store_path
|
53
|
+
* Numeric
|
54
|
+
* square
|
55
|
+
* round_down
|
56
|
+
* round_up
|
47
57
|
* String
|
48
58
|
* email?
|
49
59
|
* domain_name?
|
@@ -3,5 +3,6 @@ require 'more_core_extensions/version'
|
|
3
3
|
require 'more_core_extensions/core_ext/array'
|
4
4
|
require 'more_core_extensions/core_ext/hash'
|
5
5
|
require 'more_core_extensions/core_ext/module'
|
6
|
+
require 'more_core_extensions/core_ext/numeric'
|
6
7
|
require 'more_core_extensions/core_ext/object'
|
7
8
|
require 'more_core_extensions/core_ext/string'
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'active_support/core_ext/enumerable' # For Array#sum
|
2
|
+
|
3
|
+
module MoreCoreExtensions
|
4
|
+
module ArrayMath
|
5
|
+
#
|
6
|
+
# Returns the mean of an Array of Numerics.
|
7
|
+
#
|
8
|
+
# [1, 2, 3, 4, 5].mean #=> 3.0
|
9
|
+
# [1.0, 2.0, 3.0].mean #=> 2.0
|
10
|
+
def mean
|
11
|
+
sum.to_f / length
|
12
|
+
end
|
13
|
+
|
14
|
+
#
|
15
|
+
# Returns the standard deviation of an Array of Numerics.
|
16
|
+
#
|
17
|
+
# [1, 2, 3, 4, 5].stddev #=> 1.5811388300841898
|
18
|
+
# [1.0, 2.0, 3.0].stddev #=> 1.0
|
19
|
+
def stddev
|
20
|
+
Math.sqrt(variance)
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# Returns the variance of an Array of Numerics.
|
25
|
+
#
|
26
|
+
# [1, 2, 3, 4, 5].variance #=> 2.5
|
27
|
+
# [1.0, 2.0, 3.0].variance #=> 1.0
|
28
|
+
def variance
|
29
|
+
μ = mean
|
30
|
+
reduce(0) { |m, i| m + (i - μ).square } / (length - 1)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Array.send(:include, MoreCoreExtensions::ArrayMath)
|
@@ -2,6 +2,7 @@ require 'more_core_extensions/core_ext/array/deletes'
|
|
2
2
|
require 'more_core_extensions/core_ext/array/duplicates'
|
3
3
|
require 'more_core_extensions/core_ext/array/element_counts'
|
4
4
|
require 'more_core_extensions/core_ext/array/inclusions'
|
5
|
+
require 'more_core_extensions/core_ext/array/math'
|
5
6
|
require 'more_core_extensions/core_ext/array/nested'
|
6
7
|
require 'more_core_extensions/core_ext/array/random'
|
7
8
|
require 'more_core_extensions/core_ext/array/stretch'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module MoreCoreExtensions
|
2
|
+
module NumericRounding
|
3
|
+
def round_up(nearest = 1)
|
4
|
+
return self if nearest == 0
|
5
|
+
return self if (self % nearest) == 0
|
6
|
+
self + nearest - (self % nearest)
|
7
|
+
end
|
8
|
+
|
9
|
+
def round_down(nearest = 1)
|
10
|
+
return self if nearest == 0
|
11
|
+
return self if (self % nearest) == 0
|
12
|
+
self - (self % nearest)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
Numeric.send(:prepend, MoreCoreExtensions::NumericRounding)
|
@@ -1,2 +1,2 @@
|
|
1
|
-
require '
|
1
|
+
require 'active_support/core_ext/object/blank'
|
2
2
|
require 'more_core_extensions/core_ext/object/namespace'
|
@@ -0,0 +1,16 @@
|
|
1
|
+
describe Array do
|
2
|
+
let(:floats) { [1.1, 2.2, 3.3, 4.4, 5.5] }
|
3
|
+
let(:integers) { [1, 2, 3, 4, 5] }
|
4
|
+
|
5
|
+
context "with floats" do
|
6
|
+
it("#stddev") { expect(floats.stddev).to be_within(0.00001).of(1.73925) }
|
7
|
+
it("#mean") { expect(floats.mean).to be_within(0.00001).of(3.30000) }
|
8
|
+
it("#variance") { expect(floats.variance).to be_within(0.00001).of(3.02500) }
|
9
|
+
end
|
10
|
+
|
11
|
+
context "with integers" do
|
12
|
+
it("#stddev") { expect(integers.stddev).to be_within(0.00001).of(1.58113) }
|
13
|
+
it("#mean") { expect(integers.mean).to be_within(0.00001).of(3.00000) }
|
14
|
+
it("#variance") { expect(integers.variance).to be_within(0.00001).of(2.50000) }
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
describe Numeric do
|
2
|
+
context "#round_up" do
|
3
|
+
it("Fixnum") { expect(1.round_up).to eq(1) }
|
4
|
+
it("Float") { expect(1.1.round_up).to eq(2.0) }
|
5
|
+
context "with a 'nearest' param" do
|
6
|
+
it("Fixnum") { expect(1.round_up(10)).to eq(10) }
|
7
|
+
it("Float") { expect(1.1.round_up(0.5)).to eq(1.5) }
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context "#round_down" do
|
12
|
+
it("Fixnum") { expect(1.round_down).to eq(1) }
|
13
|
+
it("Float") { expect(1.1.round_down).to eq(1.0) }
|
14
|
+
context "with a 'nearest' param" do
|
15
|
+
it("Fixnum") { expect(11.round_down(10)).to eq(10) }
|
16
|
+
it("Float") { expect(1.6.round_down(0.5)).to eq(1.5) }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: more_core_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Frey
|
8
|
+
- Brandon Dunne
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2016-
|
12
|
+
date: 2016-08-11 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: bundler
|
@@ -73,7 +74,7 @@ dependencies:
|
|
73
74
|
- - ">="
|
74
75
|
- !ruby/object:Gem::Version
|
75
76
|
version: '0'
|
76
|
-
type: :
|
77
|
+
type: :runtime
|
77
78
|
prerelease: false
|
78
79
|
version_requirements: !ruby/object:Gem::Requirement
|
79
80
|
requirements:
|
@@ -84,6 +85,7 @@ description: MoreCoreExtensions are a set of core extensions beyond those provid
|
|
84
85
|
by ActiveSupport.
|
85
86
|
email:
|
86
87
|
- fryguy9@gmail.com
|
88
|
+
- bdunne@redhat.com
|
87
89
|
executables: []
|
88
90
|
extensions: []
|
89
91
|
extra_rdoc_files: []
|
@@ -98,6 +100,7 @@ files:
|
|
98
100
|
- lib/more_core_extensions/core_ext/array/duplicates.rb
|
99
101
|
- lib/more_core_extensions/core_ext/array/element_counts.rb
|
100
102
|
- lib/more_core_extensions/core_ext/array/inclusions.rb
|
103
|
+
- lib/more_core_extensions/core_ext/array/math.rb
|
101
104
|
- lib/more_core_extensions/core_ext/array/nested.rb
|
102
105
|
- lib/more_core_extensions/core_ext/array/random.rb
|
103
106
|
- lib/more_core_extensions/core_ext/array/stretch.rb
|
@@ -107,8 +110,10 @@ files:
|
|
107
110
|
- lib/more_core_extensions/core_ext/hash/nested.rb
|
108
111
|
- lib/more_core_extensions/core_ext/module.rb
|
109
112
|
- lib/more_core_extensions/core_ext/module/namespace.rb
|
113
|
+
- lib/more_core_extensions/core_ext/numeric.rb
|
114
|
+
- lib/more_core_extensions/core_ext/numeric/math.rb
|
115
|
+
- lib/more_core_extensions/core_ext/numeric/rounding.rb
|
110
116
|
- lib/more_core_extensions/core_ext/object.rb
|
111
|
-
- lib/more_core_extensions/core_ext/object/blank.rb
|
112
117
|
- lib/more_core_extensions/core_ext/object/namespace.rb
|
113
118
|
- lib/more_core_extensions/core_ext/shared/nested.rb
|
114
119
|
- lib/more_core_extensions/core_ext/string.rb
|
@@ -119,13 +124,15 @@ files:
|
|
119
124
|
- spec/core_ext/array/duplicates_spec.rb
|
120
125
|
- spec/core_ext/array/element_counts_spec.rb
|
121
126
|
- spec/core_ext/array/inclusions_spec.rb
|
127
|
+
- spec/core_ext/array/math_spec.rb
|
122
128
|
- spec/core_ext/array/nested_spec.rb
|
123
129
|
- spec/core_ext/array/random_spec.rb
|
124
130
|
- spec/core_ext/array/stretch_spec.rb
|
125
131
|
- spec/core_ext/array/tableize_spec.rb
|
126
132
|
- spec/core_ext/hash/deletes_spec.rb
|
127
133
|
- spec/core_ext/hash/nested_spec.rb
|
128
|
-
- spec/core_ext/
|
134
|
+
- spec/core_ext/numeric/math_spec.rb
|
135
|
+
- spec/core_ext/numeric/rounding_spec.rb
|
129
136
|
- spec/core_ext/object/namespace_spec.rb
|
130
137
|
- spec/core_ext/string/formats_spec.rb
|
131
138
|
- spec/core_ext/string/hex_dump_spec.rb
|
@@ -150,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
150
157
|
version: '0'
|
151
158
|
requirements: []
|
152
159
|
rubyforge_project:
|
153
|
-
rubygems_version: 2.
|
160
|
+
rubygems_version: 2.5.1
|
154
161
|
signing_key:
|
155
162
|
specification_version: 4
|
156
163
|
summary: MoreCoreExtensions are a set of core extensions beyond those provided by
|
@@ -160,13 +167,15 @@ test_files:
|
|
160
167
|
- spec/core_ext/array/duplicates_spec.rb
|
161
168
|
- spec/core_ext/array/element_counts_spec.rb
|
162
169
|
- spec/core_ext/array/inclusions_spec.rb
|
170
|
+
- spec/core_ext/array/math_spec.rb
|
163
171
|
- spec/core_ext/array/nested_spec.rb
|
164
172
|
- spec/core_ext/array/random_spec.rb
|
165
173
|
- spec/core_ext/array/stretch_spec.rb
|
166
174
|
- spec/core_ext/array/tableize_spec.rb
|
167
175
|
- spec/core_ext/hash/deletes_spec.rb
|
168
176
|
- spec/core_ext/hash/nested_spec.rb
|
169
|
-
- spec/core_ext/
|
177
|
+
- spec/core_ext/numeric/math_spec.rb
|
178
|
+
- spec/core_ext/numeric/rounding_spec.rb
|
170
179
|
- spec/core_ext/object/namespace_spec.rb
|
171
180
|
- spec/core_ext/string/formats_spec.rb
|
172
181
|
- spec/core_ext/string/hex_dump_spec.rb
|
@@ -1,149 +0,0 @@
|
|
1
|
-
# Copied from https://github.com/rails/rails/blob/v5.0.0/activesupport/lib/active_support/core_ext/object/blank.rb
|
2
|
-
# With the release of ActiveSupport v5.0.0 a minimum required_ruby_version of '>= 2.2.2' was added.
|
3
|
-
# This code has been copied here so that we can continue to support ruby versions that don't meet that requirement.
|
4
|
-
|
5
|
-
unless Object.respond_to?(:blank?)
|
6
|
-
class Object
|
7
|
-
# An object is blank if it's false, empty, or a whitespace string.
|
8
|
-
# For example, +false+, '', ' ', +nil+, [], and {} are all blank.
|
9
|
-
#
|
10
|
-
# This simplifies
|
11
|
-
#
|
12
|
-
# !address || address.empty?
|
13
|
-
#
|
14
|
-
# to
|
15
|
-
#
|
16
|
-
# address.blank?
|
17
|
-
#
|
18
|
-
# @return [true, false]
|
19
|
-
def blank?
|
20
|
-
respond_to?(:empty?) ? !!empty? : !self
|
21
|
-
end
|
22
|
-
|
23
|
-
# An object is present if it's not blank.
|
24
|
-
#
|
25
|
-
# @return [true, false]
|
26
|
-
def present?
|
27
|
-
!blank?
|
28
|
-
end
|
29
|
-
|
30
|
-
# Returns the receiver if it's present otherwise returns +nil+.
|
31
|
-
# <tt>object.presence</tt> is equivalent to
|
32
|
-
#
|
33
|
-
# object.present? ? object : nil
|
34
|
-
#
|
35
|
-
# For example, something like
|
36
|
-
#
|
37
|
-
# state = params[:state] if params[:state].present?
|
38
|
-
# country = params[:country] if params[:country].present?
|
39
|
-
# region = state || country || 'US'
|
40
|
-
#
|
41
|
-
# becomes
|
42
|
-
#
|
43
|
-
# region = params[:state].presence || params[:country].presence || 'US'
|
44
|
-
#
|
45
|
-
# @return [Object]
|
46
|
-
def presence
|
47
|
-
self if present?
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
class NilClass
|
52
|
-
# +nil+ is blank:
|
53
|
-
#
|
54
|
-
# nil.blank? # => true
|
55
|
-
#
|
56
|
-
# @return [true]
|
57
|
-
def blank?
|
58
|
-
true
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
class FalseClass
|
63
|
-
# +false+ is blank:
|
64
|
-
#
|
65
|
-
# false.blank? # => true
|
66
|
-
#
|
67
|
-
# @return [true]
|
68
|
-
def blank?
|
69
|
-
true
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
class TrueClass
|
74
|
-
# +true+ is not blank:
|
75
|
-
#
|
76
|
-
# true.blank? # => false
|
77
|
-
#
|
78
|
-
# @return [false]
|
79
|
-
def blank?
|
80
|
-
false
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
class Array
|
85
|
-
# An array is blank if it's empty:
|
86
|
-
#
|
87
|
-
# [].blank? # => true
|
88
|
-
# [1,2,3].blank? # => false
|
89
|
-
#
|
90
|
-
# @return [true, false]
|
91
|
-
alias_method :blank?, :empty?
|
92
|
-
end
|
93
|
-
|
94
|
-
class Hash
|
95
|
-
# A hash is blank if it's empty:
|
96
|
-
#
|
97
|
-
# {}.blank? # => true
|
98
|
-
# { key: 'value' }.blank? # => false
|
99
|
-
#
|
100
|
-
# @return [true, false]
|
101
|
-
alias_method :blank?, :empty?
|
102
|
-
end
|
103
|
-
|
104
|
-
class String
|
105
|
-
BLANK_RE = /\A[[:space:]]*\z/
|
106
|
-
|
107
|
-
# A string is blank if it's empty or contains whitespaces only:
|
108
|
-
#
|
109
|
-
# ''.blank? # => true
|
110
|
-
# ' '.blank? # => true
|
111
|
-
# "\t\n\r".blank? # => true
|
112
|
-
# ' blah '.blank? # => false
|
113
|
-
#
|
114
|
-
# Unicode whitespace is supported:
|
115
|
-
#
|
116
|
-
# "\u00a0".blank? # => true
|
117
|
-
#
|
118
|
-
# @return [true, false]
|
119
|
-
def blank?
|
120
|
-
# The regexp that matches blank strings is expensive. For the case of empty
|
121
|
-
# strings we can speed up this method (~3.5x) with an empty? call. The
|
122
|
-
# penalty for the rest of strings is marginal.
|
123
|
-
empty? || BLANK_RE === self
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
class Numeric #:nodoc:
|
128
|
-
# No number is blank:
|
129
|
-
#
|
130
|
-
# 1.blank? # => false
|
131
|
-
# 0.blank? # => false
|
132
|
-
#
|
133
|
-
# @return [false]
|
134
|
-
def blank?
|
135
|
-
false
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
class Time #:nodoc:
|
140
|
-
# No Time is blank:
|
141
|
-
#
|
142
|
-
# Time.now.blank? # => false
|
143
|
-
#
|
144
|
-
# @return [false]
|
145
|
-
def blank?
|
146
|
-
false
|
147
|
-
end
|
148
|
-
end
|
149
|
-
end
|
@@ -1,46 +0,0 @@
|
|
1
|
-
describe "blank" do
|
2
|
-
class EmptyTrue
|
3
|
-
def empty?
|
4
|
-
0
|
5
|
-
end
|
6
|
-
end
|
7
|
-
|
8
|
-
class EmptyFalse
|
9
|
-
def empty?
|
10
|
-
nil
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
BLANK = [EmptyTrue.new, nil, false, '', ' ', " \n\t \r ", ' ', "\u00a0", [], {}].freeze
|
15
|
-
PRESENT = [EmptyFalse.new, Object.new, true, 0, 1, 'a', [nil], {nil => 0}].freeze
|
16
|
-
|
17
|
-
describe "#blank?" do
|
18
|
-
context "true" do
|
19
|
-
BLANK.each { |v| it(v.inspect) { expect(v).to be_blank } }
|
20
|
-
end
|
21
|
-
|
22
|
-
context "false" do
|
23
|
-
PRESENT.each { |v| it(v.inspect) { expect(v).to_not be_blank } }
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
describe "#present?" do
|
28
|
-
context "true" do
|
29
|
-
PRESENT.each { |v| it(v.inspect) { expect(v).to be_present } }
|
30
|
-
end
|
31
|
-
|
32
|
-
context "false" do
|
33
|
-
BLANK.each { |v| it(v.inspect) { expect(v).to_not be_present } }
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
describe "#presence" do
|
38
|
-
context "object" do
|
39
|
-
PRESENT.each { |v| it(v.inspect) { expect(v.presence).to eq(v) } }
|
40
|
-
end
|
41
|
-
|
42
|
-
context "nil" do
|
43
|
-
BLANK.each { |v| it(v.inspect) { expect(v.presence).to be_nil } }
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|