ruby_core_extensions 0.0.1 → 0.1.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/.rubocop.yml +21 -0
- data/CHANGELOG.md +22 -0
- data/Rakefile +1 -1
- data/gemfiles/rails3.gemfile +1 -1
- data/gemfiles/rails4.gemfile +1 -1
- data/lib/ruby_core_extensions/array.rb +1 -10
- data/lib/ruby_core_extensions/class.rb +0 -3
- data/lib/ruby_core_extensions/compact/array.rb +1 -2
- data/lib/ruby_core_extensions/compact/hash.rb +3 -4
- data/lib/ruby_core_extensions/enumerable.rb +0 -3
- data/lib/ruby_core_extensions/file.rb +0 -1
- data/lib/ruby_core_extensions/hash.rb +4 -6
- data/lib/ruby_core_extensions/kernel.rb +0 -2
- data/lib/ruby_core_extensions/numeric.rb +0 -2
- data/lib/ruby_core_extensions/object.rb +5 -7
- data/lib/ruby_core_extensions/recursive/array.rb +42 -44
- data/lib/ruby_core_extensions/recursive/hash.rb +14 -17
- data/lib/ruby_core_extensions/recursive/object.rb +8 -4
- data/lib/ruby_core_extensions/recursive.rb +0 -5
- data/lib/ruby_core_extensions/string.rb +8 -9
- data/lib/ruby_core_extensions/version.rb +1 -1
- data/ruby_core_extensions.gemspec +3 -1
- data/spec/array_spec.rb +33 -12
- data/spec/compact_spec.rb +13 -13
- data/spec/enumerable_spec.rb +13 -9
- data/spec/filename_spec.rb +0 -1
- data/spec/hash_spec.rb +41 -30
- data/spec/object_spec.rb +4 -5
- data/spec/string_spec.rb +3 -1
- data/spec/support/coverage.rb +4 -29
- metadata +20 -9
- data/lib/ruby_core_extensions/recursive/big_decimal.rb +0 -5
- data/lib/ruby_core_extensions/recursive/date.rb +0 -8
- data/lib/ruby_core_extensions/recursive/date_time.rb +0 -8
- data/lib/ruby_core_extensions/recursive/fixnum.rb +0 -7
- data/lib/ruby_core_extensions/recursive/time.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 702cb94d4f6195649a0c4ed456219c865a4883db
|
4
|
+
data.tar.gz: b03377cd78457c6949efc0278ae875d3a243074d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24b92150e349a329f51e9f88b7f5e6ff491f583e2e26d93b394605debc86274171e1a7f339b1cd27dde2e197987ee668f14cba88c194c5c77c532948ea7a624a
|
7
|
+
data.tar.gz: c22a0acd4cea5b4cc86afbd035203ee7a87c4742ccc6b8c68cc810f2d848fcb25bb320d6ab01c7cdb09138b98efa1284507a215f160b23398550a532d73ae6d5
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
inherit_gem:
|
2
|
+
rubocop-rails:
|
3
|
+
- config/rails.yml
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
TargetRubyVersion: 2.4
|
7
|
+
|
8
|
+
Style/FrozenStringLiteralComment:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
Style/StringLiterals:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Layout/IndentationWidth:
|
15
|
+
IgnoredPatterns: ['^\s*private$']
|
16
|
+
|
17
|
+
Style/BracesAroundHashParameters:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Metrics/LineLength:
|
21
|
+
Max: 100
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Change Log
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
This project adheres to [Semantic Versioning](http://semver.org/).
|
4
|
+
This changelog adheres to [Keep a CHANGELOG](http://keepachangelog.com/).
|
5
|
+
|
6
|
+
## Unreleased
|
7
|
+
|
8
|
+
## 0.1.0
|
9
|
+
|
10
|
+
### Added
|
11
|
+
- [TT-4020] Implemented Rubocop
|
12
|
+
|
13
|
+
### Removed
|
14
|
+
- Remove no longer supported Array methods #to_param/#show_name
|
15
|
+
|
16
|
+
### Changed
|
17
|
+
- stringify_values_recursively now just works on to_s for all objects
|
18
|
+
|
19
|
+
## 0.0.1
|
20
|
+
|
21
|
+
### Added
|
22
|
+
- [TT-1392] Changelog file
|
data/Rakefile
CHANGED
data/gemfiles/rails3.gemfile
CHANGED
data/gemfiles/rails4.gemfile
CHANGED
@@ -1,13 +1,4 @@
|
|
1
1
|
class Array
|
2
|
-
|
3
|
-
def to_param
|
4
|
-
self.collect { |element| element.respond_to?(:to_param) ? element.to_param : element }
|
5
|
-
end
|
6
|
-
|
7
|
-
def show_name
|
8
|
-
first.titleize
|
9
|
-
end
|
10
|
-
|
11
2
|
# Key should be unique, or latest element with that key will override previous ones.
|
12
3
|
def hash_by(key = nil, method = nil, &block)
|
13
4
|
self.inject({}) do |h, element|
|
@@ -31,7 +22,7 @@ class Array
|
|
31
22
|
end
|
32
23
|
|
33
24
|
def intersects?(other)
|
34
|
-
self.any?{|i| other.include?(i)}
|
25
|
+
self.any? { |i| other.include?(i) }
|
35
26
|
end
|
36
27
|
|
37
28
|
# Same effect as Array.wrap(object).first
|
@@ -6,7 +6,7 @@ class Hash
|
|
6
6
|
|
7
7
|
# Remove nil values - !desctructively!
|
8
8
|
def compact!
|
9
|
-
delete_if{|k,v| v.nil?}
|
9
|
+
delete_if { |k, v| v.nil? }
|
10
10
|
end
|
11
11
|
|
12
12
|
def compact_blank
|
@@ -14,11 +14,11 @@ class Hash
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def compact_blank!
|
17
|
-
delete_if{|k,v| v.blank?}
|
17
|
+
delete_if { |k, v| v.blank? }
|
18
18
|
end
|
19
19
|
|
20
20
|
def recursive_compact_blank!
|
21
|
-
delete_if do |k,v|
|
21
|
+
delete_if do |k, v|
|
22
22
|
if v.is_a?(Hash)
|
23
23
|
v.recursive_compact_blank!
|
24
24
|
v.recursive_blank?
|
@@ -31,4 +31,3 @@ class Hash
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
34
|
-
|
@@ -1,7 +1,6 @@
|
|
1
1
|
class Hash
|
2
|
-
|
3
2
|
unless self.method_defined?(:extract!)
|
4
|
-
#Imported from Rails 3
|
3
|
+
# Imported from Rails 3
|
5
4
|
def extract!(*keys)
|
6
5
|
result = {}
|
7
6
|
keys.each { |key| result[key] = delete(key) }
|
@@ -12,23 +11,22 @@ class Hash
|
|
12
11
|
|
13
12
|
def map_key_value(key_method, value_method = nil)
|
14
13
|
value_method ||= key_method
|
15
|
-
each.with_object({}) do |(k,v), new_hash|
|
14
|
+
each.with_object({}) do |(k, v), new_hash|
|
16
15
|
new_hash[k.send(key_method)] = v.send(value_method)
|
17
16
|
end
|
18
17
|
end
|
19
18
|
|
20
19
|
|
21
20
|
def map_key(method)
|
22
|
-
each.with_object({}) do |(k,v), new_hash|
|
21
|
+
each.with_object({}) do |(k, v), new_hash|
|
23
22
|
new_hash[k.send(method)] = v
|
24
23
|
end
|
25
24
|
end
|
26
25
|
|
27
26
|
|
28
27
|
def map_value(method)
|
29
|
-
each.with_object({}) do |(k,v), new_hash|
|
28
|
+
each.with_object({}) do |(k, v), new_hash|
|
30
29
|
new_hash[k] = v.send(method)
|
31
30
|
end
|
32
31
|
end
|
33
|
-
|
34
32
|
end
|
@@ -3,16 +3,16 @@ class Object
|
|
3
3
|
def sounds_like?(other)
|
4
4
|
self.phonetic_code == other.phonetic_code
|
5
5
|
end
|
6
|
-
|
6
|
+
|
7
7
|
# Convert this object into a string, then convert that to phonetic code
|
8
8
|
def phonetic_code
|
9
9
|
self.to_s.phonetic_code
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
def to_long_s
|
13
13
|
to_s
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
def virtual_belongs_to(*associations)
|
17
17
|
options = associations.extract_options!
|
18
18
|
|
@@ -62,11 +62,9 @@ class Object
|
|
62
62
|
end
|
63
63
|
EVAL
|
64
64
|
end
|
65
|
-
|
66
|
-
|
65
|
+
|
66
|
+
|
67
67
|
def to_bool
|
68
68
|
self.to_s.to_bool
|
69
69
|
end
|
70
|
-
|
71
70
|
end
|
72
|
-
|
@@ -1,45 +1,43 @@
|
|
1
|
-
class Array
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
end
|
1
|
+
class Array
|
2
|
+
def convert
|
3
|
+
self
|
4
|
+
end
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
end
|
6
|
+
def convert_keys_recursively(&converter)
|
7
|
+
map { |v| v.convert_keys_recursively(&converter) }
|
8
|
+
end
|
9
|
+
|
10
|
+
def convert_values_recursively(&converter)
|
11
|
+
map { |v| v.convert_values_recursively(&converter) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def symbolize_keys_recursively
|
15
|
+
map(&:symbolize_keys_recursively)
|
16
|
+
end
|
17
|
+
|
18
|
+
def stringify_values_recursively
|
19
|
+
map(&:stringify_values_recursively)
|
20
|
+
end
|
21
|
+
|
22
|
+
def make_indifferent_access_recursively
|
23
|
+
map(&:make_indifferent_access_recursively)
|
24
|
+
end
|
25
|
+
|
26
|
+
def recursive_blank?
|
27
|
+
each do |v|
|
28
|
+
if v.respond_to?(:recursive_blank?)
|
29
|
+
return false unless v.recursive_blank?
|
30
|
+
else
|
31
|
+
return false unless v.blank?
|
32
|
+
end
|
33
|
+
end
|
34
|
+
true
|
35
|
+
end
|
36
|
+
|
37
|
+
def recursively(&block)
|
38
|
+
each do |element|
|
39
|
+
block.call(element)
|
40
|
+
element.recursively(&block) if element.respond_to?(:recursively)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
class Hash
|
2
|
-
|
3
2
|
def recursive_blank?
|
4
3
|
each do |k, v|
|
5
4
|
if v.respond_to?(:recursive_blank?)
|
@@ -10,18 +9,18 @@ class Hash
|
|
10
9
|
end
|
11
10
|
true
|
12
11
|
end
|
13
|
-
|
12
|
+
|
14
13
|
def convert
|
15
14
|
self
|
16
15
|
end
|
17
|
-
|
16
|
+
|
18
17
|
def convert_keys(&converter)
|
19
18
|
inject({}) do |hash, (key, value)|
|
20
19
|
hash[converter.call(key)] = value
|
21
20
|
hash
|
22
21
|
end
|
23
22
|
end
|
24
|
-
|
23
|
+
|
25
24
|
def convert_values(*keys, &converter)
|
26
25
|
inject(clone) do |hash, (key, value)|
|
27
26
|
hash[key] = value.convert(&converter) if keys.blank? || keys.include?(key)
|
@@ -33,54 +32,52 @@ class Hash
|
|
33
32
|
Hash[map do |key, value|
|
34
33
|
k = converter.call(key)
|
35
34
|
v = value.convert_keys_recursively(&converter)
|
36
|
-
[k,v]
|
35
|
+
[k, v]
|
37
36
|
end]
|
38
37
|
end
|
39
|
-
|
38
|
+
|
40
39
|
def convert_values_recursively(&converter)
|
41
40
|
inject({}) do |hash, (key, value)|
|
42
41
|
hash[key] = value.convert_values_recursively(&converter)
|
43
42
|
hash
|
44
43
|
end
|
45
44
|
end
|
46
|
-
|
45
|
+
|
47
46
|
def symbolize_keys_recursively
|
48
47
|
Hash[map do |key, value|
|
49
48
|
k = key.is_a?(String) ? key.to_sym : key
|
50
49
|
v = value.symbolize_keys_recursively
|
51
|
-
[k,v]
|
50
|
+
[k, v]
|
52
51
|
end]
|
53
52
|
end
|
54
|
-
|
53
|
+
|
55
54
|
def stringify_values_recursively
|
56
55
|
inject({}) do |options, (key, value)|
|
57
56
|
options[key] = value.stringify_values_recursively
|
58
57
|
options
|
59
58
|
end
|
60
59
|
end
|
61
|
-
|
60
|
+
|
62
61
|
def make_indifferent_access_recursively
|
63
62
|
HashWithIndifferentAccess.new(inject({}) do |options, (key, value)|
|
64
63
|
options[key] = value.make_indifferent_access_recursively
|
65
64
|
options
|
66
65
|
end)
|
67
66
|
end
|
68
|
-
|
67
|
+
|
69
68
|
def deep_dup
|
70
69
|
duplicate = self.dup
|
71
|
-
duplicate.each_pair do |k,v|
|
70
|
+
duplicate.each_pair do |k, v|
|
72
71
|
tv = duplicate[k]
|
73
72
|
duplicate[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_dup : v
|
74
73
|
end
|
75
74
|
duplicate
|
76
75
|
end
|
77
|
-
|
76
|
+
|
78
77
|
def recursively(&block)
|
79
|
-
each do |key,value|
|
80
|
-
block.call(key,value)
|
78
|
+
each do |key, value|
|
79
|
+
block.call(key, value)
|
81
80
|
value.recursively(&block) if value.respond_to?(:recursively)
|
82
81
|
end
|
83
82
|
end
|
84
|
-
|
85
83
|
end
|
86
|
-
|
@@ -2,16 +2,20 @@ class Object
|
|
2
2
|
def convert(&converter)
|
3
3
|
converter.call(self)
|
4
4
|
end
|
5
|
-
|
5
|
+
|
6
6
|
def return_self
|
7
7
|
self
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
|
+
def return_to_s
|
11
|
+
to_s
|
12
|
+
end
|
13
|
+
|
10
14
|
alias_method :convert_values_recursively, :convert
|
11
15
|
alias_method :convert_recursively, :convert
|
12
|
-
|
16
|
+
|
13
17
|
alias_method :convert_keys_recursively, :return_self
|
14
18
|
alias_method :symbolize_keys_recursively, :return_self
|
15
|
-
alias_method :stringify_values_recursively, :
|
19
|
+
alias_method :stringify_values_recursively, :return_to_s
|
16
20
|
alias_method :make_indifferent_access_recursively, :return_self
|
17
21
|
end
|
@@ -1,8 +1,3 @@
|
|
1
1
|
require 'ruby_core_extensions/recursive/array'
|
2
|
-
require 'ruby_core_extensions/recursive/big_decimal'
|
3
|
-
require 'ruby_core_extensions/recursive/date'
|
4
|
-
require 'ruby_core_extensions/recursive/date_time'
|
5
|
-
require 'ruby_core_extensions/recursive/fixnum'
|
6
2
|
require 'ruby_core_extensions/recursive/hash'
|
7
3
|
require 'ruby_core_extensions/recursive/object'
|
8
|
-
require 'ruby_core_extensions/recursive/time'
|
@@ -1,8 +1,8 @@
|
|
1
1
|
class String
|
2
2
|
def proper_underscore
|
3
|
-
self.titleize.gsub(" ","").underscore
|
3
|
+
self.titleize.gsub(" ", "").underscore
|
4
4
|
end
|
5
|
-
|
5
|
+
|
6
6
|
# Generate a phonetic code - which is the same for similar sounding names
|
7
7
|
def phonetic_code
|
8
8
|
# Currently using 'metaphone' which is more accurate than soundex
|
@@ -11,7 +11,7 @@ class String
|
|
11
11
|
|
12
12
|
# Solr requires numbers and letters to be separated
|
13
13
|
def separate_numbers_and_letters
|
14
|
-
gsub(/[a-z][0-9]|[0-9][a-z]/i){ |s| s[0].chr + ' ' + s[1].chr }
|
14
|
+
gsub(/[a-z][0-9]|[0-9][a-z]/i) { |s| s[0].chr + ' ' + s[1].chr }
|
15
15
|
end
|
16
16
|
|
17
17
|
# convert newlines to breaks
|
@@ -42,13 +42,13 @@ class String
|
|
42
42
|
words = split(' ')
|
43
43
|
|
44
44
|
# Return first letter of <limit> words
|
45
|
-
return words.first(limit).map{|w| w.chars.first}.join if (words.size * 2 - 1) >= limit
|
45
|
+
return words.first(limit).map { |w| w.chars.first }.join if (words.size * 2 - 1) >= limit
|
46
46
|
|
47
47
|
spaces = words.size - 1
|
48
48
|
word_char_min = (limit - spaces) / words.size
|
49
49
|
word_char_max = word_char_min + 1
|
50
50
|
|
51
|
-
words = words.map{|word| word[0..(word_char_max - 1)]}
|
51
|
+
words = words.map { |word| word[0..(word_char_max - 1)] }
|
52
52
|
|
53
53
|
words.reverse.each.with_index do |word, index|
|
54
54
|
letters_to_remove = words.join(' ').size - limit
|
@@ -60,19 +60,18 @@ class String
|
|
60
60
|
end
|
61
61
|
|
62
62
|
# Replace word
|
63
|
-
words[words.size -
|
63
|
+
words[words.size - index - 1] = word[0..(letters_to_keep - 1)]
|
64
64
|
|
65
65
|
break if last_case
|
66
66
|
end
|
67
67
|
|
68
68
|
words.join(' ')
|
69
69
|
end
|
70
|
-
|
71
|
-
|
70
|
+
|
71
|
+
|
72
72
|
def to_bool
|
73
73
|
return true if self == true || self =~ (/(true|t|yes|y|1)$/i)
|
74
74
|
return false if self == false || self.blank? || self =~ (/(false|f|no|n|0)$/i)
|
75
75
|
raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
|
76
76
|
end
|
77
77
|
end
|
78
|
-
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# coding: utf-8
|
2
|
+
|
2
3
|
lib = File.expand_path('../lib', __FILE__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
5
|
require 'ruby_core_extensions/version'
|
@@ -25,8 +26,9 @@ Gem::Specification.new do |spec|
|
|
25
26
|
spec.add_development_dependency "bundler", "~> 1.3"
|
26
27
|
spec.add_development_dependency "rake"
|
27
28
|
spec.add_development_dependency 'rspec'
|
28
|
-
spec.add_development_dependency '
|
29
|
+
spec.add_development_dependency 'coverage-kit'
|
29
30
|
spec.add_development_dependency 'simplecov-rcov'
|
30
31
|
spec.add_development_dependency 'coveralls'
|
31
32
|
spec.add_development_dependency 'travis'
|
33
|
+
spec.add_development_dependency 'rubocop-rails'
|
32
34
|
end
|
data/spec/array_spec.rb
CHANGED
@@ -1,10 +1,21 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Array do
|
4
|
-
|
5
4
|
it "should allow converting all values to strings recursively" do
|
6
|
-
|
7
|
-
|
5
|
+
to_s_class = Class.new do
|
6
|
+
def to_s
|
7
|
+
'Myself'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
stringify_values_recursively_class = Class.new do
|
11
|
+
def stringify_values_recursively
|
12
|
+
'Special'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
now = Time.now
|
16
|
+
array = [1, 2, now, [3, 4], to_s_class.new, stringify_values_recursively_class.new]
|
17
|
+
output = ['1', '2', now.to_s, %w[3 4], 'Myself', 'Special']
|
18
|
+
expect(array.stringify_values_recursively).to eq output
|
8
19
|
end
|
9
20
|
|
10
21
|
it "should allow removing all blank values" do
|
@@ -14,30 +25,40 @@ describe Array do
|
|
14
25
|
end
|
15
26
|
|
16
27
|
it "should allow removing all blank values recursively" do
|
17
|
-
a = [1, 2, [" Kan", {}], nil, {:
|
28
|
+
a = [1, 2, [" Kan", {}], nil, { a: "", b: {} }, ["garoos", " "]]
|
18
29
|
a.recursive_compact_blank!
|
19
30
|
expect(a.join).to eq "12 Kangaroos"
|
20
31
|
end
|
21
32
|
|
22
33
|
it "should allow verifying if all elements are blank recursively" do
|
23
|
-
expect(['',nil,[nil,['']]]).to be_recursive_blank
|
24
|
-
expect(['',nil,[nil,['',1]]]).to_not be_recursive_blank
|
34
|
+
expect(['', nil, [nil, ['']]]).to be_recursive_blank
|
35
|
+
expect(['', nil, [nil, ['', 1]]]).to_not be_recursive_blank
|
25
36
|
end
|
26
37
|
|
27
38
|
it "should allow converting to hash given a key" do
|
28
|
-
expect([1,2,3].hash_by(:ordinalize)).to eq({'1st' => 1, "2nd" => 2, "3rd" => 3})
|
29
|
-
expect([1,2,3].hash_by(:ordinalize, :to_s)).to eq(
|
30
|
-
|
31
|
-
|
39
|
+
expect([1, 2, 3].hash_by(:ordinalize)).to eq({ '1st' => 1, "2nd" => 2, "3rd" => 3 })
|
40
|
+
expect([1, 2, 3].hash_by(:ordinalize, :to_s)).to eq(
|
41
|
+
{ '1st' => '1', "2nd" => '2', "3rd" => '3' }
|
42
|
+
)
|
43
|
+
expect([1, 2, 3].hash_by(:ordinalize) { |v| v + 1 }).to eq(
|
44
|
+
{ '1st' => 2, "2nd" => 3, "3rd" => 4 }
|
45
|
+
)
|
46
|
+
expect([1, 2, 3].hash_by { |k| k * 2 }).to eq({ 2 => 1, 4 => 2, 6 => 3 })
|
47
|
+
end
|
48
|
+
|
49
|
+
it "#hash_by_id" do
|
50
|
+
one = double(id: 1, name: 'One')
|
51
|
+
two = double(id: 2, name: 'Two')
|
52
|
+
expect([one, two].hash_by_id).to eq(1 => one, 2 => two)
|
32
53
|
end
|
33
54
|
|
34
55
|
it "should allow executing blocks recursively" do
|
35
|
-
array = [1,[2,3],[4,[5,6],7,[8]],9,[[10]]]
|
56
|
+
array = [1, [2, 3], [4, [5, 6], 7, [8]], 9, [[10]]]
|
36
57
|
result = []
|
37
58
|
array.recursively do |e|
|
38
59
|
result << e unless e.is_a?(Array)
|
39
60
|
end
|
40
|
-
expect(result).to eq [1,2,3,4,5,6,7,8,9,10]
|
61
|
+
expect(result).to eq [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
41
62
|
end
|
42
63
|
|
43
64
|
it '#intersects?' do
|
data/spec/compact_spec.rb
CHANGED
@@ -6,11 +6,11 @@ describe Array, 'compact' do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
it 'should recursive compact blank' do
|
9
|
-
a = [1, nil, {:
|
9
|
+
a = [1, nil, { a: nil, b: 2 }]
|
10
10
|
a.recursive_compact_blank!
|
11
|
-
expect(a).to eq [1, {:
|
11
|
+
expect(a).to eq [1, { b: 2 }]
|
12
12
|
|
13
|
-
b = [1, nil, {:
|
13
|
+
b = [1, nil, { a: nil }]
|
14
14
|
b.recursive_compact_blank!
|
15
15
|
expect(b).to eq [1]
|
16
16
|
end
|
@@ -18,32 +18,32 @@ end
|
|
18
18
|
|
19
19
|
describe Hash, 'compact' do
|
20
20
|
it 'should compact' do
|
21
|
-
expect({:
|
21
|
+
expect({ a: 1, b: nil }.compact).to eq({ a: 1 })
|
22
22
|
end
|
23
23
|
|
24
24
|
it 'should compact!' do
|
25
|
-
a = {:
|
25
|
+
a = { a: 1, b: nil }
|
26
26
|
a.compact!
|
27
|
-
expect(a).to eq({:
|
27
|
+
expect(a).to eq({ a: 1 })
|
28
28
|
end
|
29
29
|
|
30
30
|
it 'should compact blank' do
|
31
|
-
expect({:
|
31
|
+
expect({ a: 1, b: '' }.compact_blank).to eq({ a: 1 })
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'should compact blank!' do
|
35
|
-
a = {:
|
35
|
+
a = { a: 1, b: '' }
|
36
36
|
a.compact_blank!
|
37
|
-
expect(a).to eq({:
|
37
|
+
expect(a).to eq({ a: 1 })
|
38
38
|
end
|
39
39
|
|
40
40
|
it 'should recursive compact blank!' do
|
41
|
-
a = {:
|
41
|
+
a = { a: 1, b: { c: 1, d: '' } }
|
42
42
|
a.recursive_compact_blank!
|
43
|
-
expect(a).to eq({:
|
43
|
+
expect(a).to eq({ a: 1, b: { c: 1 } })
|
44
44
|
|
45
|
-
a = {:
|
45
|
+
a = { a: 1, b: { c: [], d: '' } }
|
46
46
|
a.recursive_compact_blank!
|
47
|
-
expect(a).to eq({:
|
47
|
+
expect(a).to eq({ a: 1 })
|
48
48
|
end
|
49
49
|
end
|
data/spec/enumerable_spec.rb
CHANGED
@@ -1,24 +1,28 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Enumerable do
|
4
|
-
|
5
|
-
it '
|
6
|
-
expect([1,2,3].map_methods(:to_s
|
4
|
+
|
5
|
+
it 'allow mapping elements to hashes with method names of the returned values for each method' do
|
6
|
+
expect([1, 2, 3].map_methods(:to_s, :to_f)).to eq(
|
7
|
+
[{ to_s: '1', to_f: 1.0 }, { to_s: '2', to_f: 2.0 }, { to_s: '3', to_f: 3.0 }]
|
8
|
+
)
|
7
9
|
end
|
8
10
|
|
9
11
|
|
10
12
|
context 'when detecting and returning the block value' do
|
11
|
-
it { expect([1,2,3].detect_and_return { |number| number.even? && number * number }).to eq 4 }
|
12
|
-
it { expect([1,3,5].detect_and_return { |number|
|
13
|
+
it { expect([1, 2, 3].detect_and_return { |number| number.even? && number * number }).to eq 4 }
|
14
|
+
it { expect([1, 3, 5].detect_and_return { |number|
|
15
|
+
number.even? && number * number }).to be nil
|
16
|
+
}
|
13
17
|
end
|
14
18
|
|
15
19
|
|
16
20
|
it 'should allow selecting by attribute' do
|
17
|
-
one = double(:
|
18
|
-
two = double(:
|
19
|
-
thr = double(:
|
21
|
+
one = double(name: 'one', type: 'odd')
|
22
|
+
two = double(name: 'two', type: 'even')
|
23
|
+
thr = double(name: 'thr', type: 'odd')
|
20
24
|
expect([one, two, thr].select_by_attr(:type, 'odd')).to eq [one, thr]
|
21
25
|
expect([one, two, thr].select_by_attr(:type, 'even')).to eq [two]
|
22
26
|
end
|
23
|
-
|
27
|
+
|
24
28
|
end
|
data/spec/filename_spec.rb
CHANGED
data/spec/hash_spec.rb
CHANGED
@@ -3,14 +3,21 @@ require 'spec_helper'
|
|
3
3
|
describe Hash do
|
4
4
|
|
5
5
|
before do
|
6
|
-
@sub_array1 = [3, BigDecimal('4'), Date.new(2000, 1, 1),
|
7
|
-
|
8
|
-
@
|
9
|
-
|
6
|
+
@sub_array1 = [3, BigDecimal('4'), Date.new(2000, 1, 1),
|
7
|
+
DateTime.new(2000, 1, 1, 0, 0, 0), { f: 5 }]
|
8
|
+
@sub_array2 = [3, BigDecimal('4'), Date.new(2000, 1, 1),
|
9
|
+
DateTime.new(2000, 1, 1, 0, 0, 0), { 'f' => 5 }]
|
10
|
+
@hash1 = { a: 1, b: { c: 2 }, d: 'test', e: @sub_array1 }
|
11
|
+
@hash2 = { 'a' => 1, 'b' => { 'c' => 2 }, :d => 'test', 'e' => @sub_array2 }
|
10
12
|
end
|
11
13
|
|
12
14
|
it "should allow converting all values to strings recursively" do
|
13
|
-
expect(@hash1.stringify_values_recursively).to eq(
|
15
|
+
expect(@hash1.stringify_values_recursively).to eq(
|
16
|
+
{
|
17
|
+
a: "1", b: { c: "2" }, d: "test",
|
18
|
+
e: ["3", '4.0', "2000-01-01", '2000-01-01T00:00:00+00:00', { f: "5" }]
|
19
|
+
}
|
20
|
+
)
|
14
21
|
end
|
15
22
|
|
16
23
|
it "should allow converting all keys to symbols recursively" do
|
@@ -18,29 +25,33 @@ describe Hash do
|
|
18
25
|
end
|
19
26
|
|
20
27
|
it "should allow converting keys" do
|
21
|
-
expect(@hash1.convert_keys(&:to_s)).to eq(
|
28
|
+
expect(@hash1.convert_keys(&:to_s)).to eq(
|
29
|
+
{ "a" => 1, "b" => { c: 2 }, "d" => "test", "e" => @sub_array1 }
|
30
|
+
)
|
22
31
|
end
|
23
32
|
|
24
33
|
it "should allow converting values" do
|
25
|
-
expect(@hash1.convert_values(&:to_s)).to eq({:
|
34
|
+
expect(@hash1.convert_values(&:to_s)).to eq({ a: "1", b: { c: 2 }, d: "test", e: @sub_array1 })
|
26
35
|
end
|
27
|
-
|
36
|
+
|
28
37
|
it "should allow converting values only for specific keys" do
|
29
|
-
expect(@hash1.convert_values(:d, :e, &:to_s)).to eq(
|
38
|
+
expect(@hash1.convert_values(:d, :e, &:to_s)).to eq(
|
39
|
+
{ a: 1, b: { c: 2 }, d: "test", e: @sub_array1 }
|
40
|
+
)
|
30
41
|
end
|
31
42
|
|
32
43
|
it "should allow making indifferent access recursively" do
|
33
44
|
expect(@hash1.make_indifferent_access_recursively['b']['c']).to eq 2
|
34
45
|
expect(@hash1.make_indifferent_access_recursively['e'][4]['f']).to eq 5
|
35
46
|
end
|
36
|
-
|
47
|
+
|
37
48
|
it "should allow executing blocks recursively" do
|
38
|
-
hash = {:
|
49
|
+
hash = { a: 1, b: { a: 2 }, c: { a: 3, b: 4, c: { a: 5 } } }
|
39
50
|
result = []
|
40
|
-
hash.recursively do |k,v|
|
51
|
+
hash.recursively do |k, v|
|
41
52
|
result << v unless v.is_a?(Hash)
|
42
|
-
end
|
43
|
-
expect(result.sort).to eq [1,2,3,4,5] # Ruby 1.8.7 doesn't order hash keys
|
53
|
+
end
|
54
|
+
expect(result.sort).to eq [1, 2, 3, 4, 5] # Ruby 1.8.7 doesn't order hash keys
|
44
55
|
end
|
45
56
|
|
46
57
|
end
|
@@ -49,36 +60,36 @@ end
|
|
49
60
|
describe Hash do
|
50
61
|
|
51
62
|
it 'should allow removing all nil values and return a new hash' do
|
52
|
-
expect({:
|
63
|
+
expect({ a: 1, b: nil }.compact).to eq({ a: 1 })
|
53
64
|
end
|
54
65
|
|
55
66
|
it 'should allow removing all nil values' do
|
56
|
-
a = {:
|
67
|
+
a = { a: 1, b: nil }
|
57
68
|
a.compact!
|
58
|
-
expect(a).to eq({:
|
69
|
+
expect(a).to eq({ a: 1 })
|
59
70
|
end
|
60
71
|
|
61
72
|
it 'should allow removing all nil values and return a new hash' do
|
62
|
-
expect({:
|
73
|
+
expect({ a: 1, b: '' }.compact_blank).to eq({ a: 1 })
|
63
74
|
end
|
64
75
|
|
65
76
|
it 'should allow removing all blank values' do
|
66
|
-
a = {:
|
77
|
+
a = { a: 1, b: '' }
|
67
78
|
a.compact_blank!
|
68
|
-
expect(a).to eq({:
|
79
|
+
expect(a).to eq({ a: 1 })
|
69
80
|
end
|
70
81
|
|
71
82
|
it 'should allow removing all blank values recursively' do
|
72
|
-
a = {:
|
83
|
+
a = { a: 1, b: { c: 1, d: '', e: [] } }
|
73
84
|
a.recursive_compact_blank!
|
74
|
-
expect(a).to eq({:
|
85
|
+
expect(a).to eq({ a: 1, b: { c: 1 } })
|
75
86
|
end
|
76
87
|
|
77
88
|
it 'should allow extracting subsets' do
|
78
|
-
a = {:
|
89
|
+
a = { a: 1, b: 2, c: 3 }
|
79
90
|
b = a.extract!(:a, :c)
|
80
|
-
expect(b).to eq({:
|
81
|
-
expect(a).to eq({:
|
91
|
+
expect(b).to eq({ a: 1, c: 3 })
|
92
|
+
expect(a).to eq({ b: 2 })
|
82
93
|
end
|
83
94
|
|
84
95
|
end
|
@@ -86,22 +97,22 @@ end
|
|
86
97
|
|
87
98
|
describe Hash, '#map_key_value' do
|
88
99
|
|
89
|
-
subject { {'1' => '2', 3 => 4} }
|
100
|
+
subject { { '1' => '2', 3 => 4 } }
|
90
101
|
|
91
102
|
it 'should map key' do
|
92
|
-
expect(subject.map_key(:to_i)).to eq({1 => '2', 3 => 4})
|
103
|
+
expect(subject.map_key(:to_i)).to eq({ 1 => '2', 3 => 4 })
|
93
104
|
end
|
94
105
|
|
95
106
|
it 'should map value' do
|
96
|
-
expect(subject.map_value(:to_i)).to eq({'1' => 2, 3 => 4})
|
107
|
+
expect(subject.map_value(:to_i)).to eq({ '1' => 2, 3 => 4 })
|
97
108
|
end
|
98
109
|
|
99
110
|
it 'should map key and value' do
|
100
|
-
expect(subject.map_key_value(:to_i, :to_i)).to eq({1 => 2, 3 => 4})
|
111
|
+
expect(subject.map_key_value(:to_i, :to_i)).to eq({ 1 => 2, 3 => 4 })
|
101
112
|
end
|
102
113
|
|
103
114
|
it 'should map key and value if value not specified' do
|
104
|
-
expect(subject.map_key_value(:to_i)).to eq({1 => 2, 3 => 4})
|
115
|
+
expect(subject.map_key_value(:to_i)).to eq({ 1 => 2, 3 => 4 })
|
105
116
|
end
|
106
117
|
|
107
118
|
end
|
data/spec/object_spec.rb
CHANGED
@@ -26,14 +26,13 @@ describe Object do
|
|
26
26
|
class ReadyError < StandardError; end
|
27
27
|
|
28
28
|
class BooleanizeTest
|
29
|
-
|
30
29
|
attr_accessor :ready
|
31
30
|
|
32
31
|
def verify!
|
33
|
-
fail ArgumentError, "Ready should be a boolean" unless ready.is_a?(TrueClass) ||
|
32
|
+
fail ArgumentError, "Ready should be a boolean" unless ready.is_a?(TrueClass) ||
|
33
|
+
ready.is_a?(FalseClass)
|
34
34
|
fail ReadyError, "Not ready" unless ready
|
35
35
|
end
|
36
|
-
|
37
36
|
end
|
38
37
|
|
39
38
|
|
@@ -42,8 +41,8 @@ describe Object do
|
|
42
41
|
end
|
43
42
|
|
44
43
|
|
45
|
-
it "
|
46
|
-
expect { @object.booleanize(:verify!, :
|
44
|
+
it "allows defining methods that return boolean depending on the execution of another method" do
|
45
|
+
expect { @object.booleanize(:verify!, rescue: ReadyError) }.to_not raise_error
|
47
46
|
expect { @object.verify? }.to raise_error(ArgumentError, 'Ready should be a boolean')
|
48
47
|
@object.ready = false
|
49
48
|
expect { @object.verify? }.to_not raise_error
|
data/spec/string_spec.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
describe String do
|
2
2
|
|
3
3
|
it "should convert to underscore replacing spaces with underscores" do
|
4
|
-
expect("CamelCase UPPERCASE to be_Converted".proper_underscore).to eq
|
4
|
+
expect("CamelCase UPPERCASE to be_Converted".proper_underscore).to eq(
|
5
|
+
"camel_case_uppercase_to_be_converted"
|
6
|
+
)
|
5
7
|
end
|
6
8
|
|
7
9
|
it 'should separate numbers and letters' do
|
data/spec/support/coverage.rb
CHANGED
@@ -1,29 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
require 'simplecov-rcov'
|
6
|
-
require 'coveralls'
|
7
|
-
Coveralls.wear!
|
8
|
-
|
9
|
-
SimpleCov.formatters = [
|
10
|
-
SimpleCov::Formatter::RcovFormatter,
|
11
|
-
Coveralls::SimpleCov::Formatter
|
12
|
-
]
|
13
|
-
SimpleCov.start do
|
14
|
-
add_filter '/vendor/'
|
15
|
-
add_filter '/spec/'
|
16
|
-
add_filter '/lib/error_handling/'
|
17
|
-
add_group 'lib', 'lib'
|
18
|
-
end
|
19
|
-
SimpleCov.at_exit do
|
20
|
-
SimpleCov.result.format!
|
21
|
-
percent = SimpleCov.result.covered_percent
|
22
|
-
unless percent >= MINIMUM_COVERAGE
|
23
|
-
puts '*' * 80
|
24
|
-
puts "Coverage must be above #{MINIMUM_COVERAGE}%. It is #{format('%.2f', percent)}%"
|
25
|
-
puts '*' * 80
|
26
|
-
Kernel.exit(1)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
1
|
+
require 'simplecov-rcov'
|
2
|
+
require 'coveralls'
|
3
|
+
require 'coverage/kit'
|
4
|
+
Coverage::Kit.setup(minimum_coverage: 87.2)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_core_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Noack
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2018-05-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -82,7 +82,7 @@ dependencies:
|
|
82
82
|
- !ruby/object:Gem::Version
|
83
83
|
version: '0'
|
84
84
|
- !ruby/object:Gem::Dependency
|
85
|
-
name:
|
85
|
+
name: coverage-kit
|
86
86
|
requirement: !ruby/object:Gem::Requirement
|
87
87
|
requirements:
|
88
88
|
- - ">="
|
@@ -137,6 +137,20 @@ dependencies:
|
|
137
137
|
- - ">="
|
138
138
|
- !ruby/object:Gem::Version
|
139
139
|
version: '0'
|
140
|
+
- !ruby/object:Gem::Dependency
|
141
|
+
name: rubocop-rails
|
142
|
+
requirement: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
type: :development
|
148
|
+
prerelease: false
|
149
|
+
version_requirements: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - ">="
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0'
|
140
154
|
description: These are extensions from core ruby classes.
|
141
155
|
email: development@travellink.com.au
|
142
156
|
executables: []
|
@@ -145,7 +159,9 @@ extra_rdoc_files: []
|
|
145
159
|
files:
|
146
160
|
- ".gitignore"
|
147
161
|
- ".rspec"
|
162
|
+
- ".rubocop.yml"
|
148
163
|
- ".travis.yml"
|
164
|
+
- CHANGELOG.md
|
149
165
|
- Gemfile
|
150
166
|
- LICENSE
|
151
167
|
- README.md
|
@@ -166,13 +182,8 @@ files:
|
|
166
182
|
- lib/ruby_core_extensions/object.rb
|
167
183
|
- lib/ruby_core_extensions/recursive.rb
|
168
184
|
- lib/ruby_core_extensions/recursive/array.rb
|
169
|
-
- lib/ruby_core_extensions/recursive/big_decimal.rb
|
170
|
-
- lib/ruby_core_extensions/recursive/date.rb
|
171
|
-
- lib/ruby_core_extensions/recursive/date_time.rb
|
172
|
-
- lib/ruby_core_extensions/recursive/fixnum.rb
|
173
185
|
- lib/ruby_core_extensions/recursive/hash.rb
|
174
186
|
- lib/ruby_core_extensions/recursive/object.rb
|
175
|
-
- lib/ruby_core_extensions/recursive/time.rb
|
176
187
|
- lib/ruby_core_extensions/string.rb
|
177
188
|
- lib/ruby_core_extensions/version.rb
|
178
189
|
- ruby_core_extensions.gemspec
|
@@ -207,7 +218,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
207
218
|
version: '0'
|
208
219
|
requirements: []
|
209
220
|
rubyforge_project:
|
210
|
-
rubygems_version: 2.
|
221
|
+
rubygems_version: 2.6.14.1
|
211
222
|
signing_key:
|
212
223
|
specification_version: 4
|
213
224
|
summary: Set of extensions to core ruby libraries used by TravelLink Technology.
|