dm_ruby_extensions 1.0.8 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflow/ci.yml +41 -0
- data/.gitignore +0 -3
- data/.rubocop.yml +26 -0
- data/.ruby-version +1 -0
- data/Gemfile +5 -1
- data/README.md +3 -1
- data/Rakefile +6 -4
- data/bin/console +18 -0
- data/dm_ruby_extensions.gemspec +15 -12
- data/lib/dm_ruby_extensions/extend_array.rb +9 -8
- data/lib/dm_ruby_extensions/extend_date.rb +7 -8
- data/lib/dm_ruby_extensions/extend_datetime.rb +6 -7
- data/lib/dm_ruby_extensions/extend_false.rb +2 -3
- data/lib/dm_ruby_extensions/extend_hash.rb +36 -28
- data/lib/dm_ruby_extensions/extend_integer.rb +6 -5
- data/lib/dm_ruby_extensions/extend_nil.rb +11 -11
- data/lib/dm_ruby_extensions/extend_numeric.rb +5 -5
- data/lib/dm_ruby_extensions/extend_string.rb +68 -65
- data/lib/dm_ruby_extensions/extend_time.rb +4 -5
- data/lib/dm_ruby_extensions/extend_true.rb +2 -3
- data/lib/dm_ruby_extensions/version.rb +3 -1
- data/lib/dm_ruby_extensions.rb +15 -12
- data/spec/extensions/array_spec.rb +13 -12
- data/spec/extensions/date_spec.rb +8 -7
- data/spec/extensions/datetime_spec.rb +8 -7
- data/spec/extensions/false_spec.rb +3 -4
- data/spec/extensions/hash_spec.rb +20 -17
- data/spec/extensions/integer_spec.rb +3 -3
- data/spec/extensions/nil_spec.rb +7 -7
- data/spec/extensions/numeric_spec.rb +3 -3
- data/spec/extensions/string_spec.rb +50 -39
- data/spec/extensions/time_spec.rb +4 -3
- data/spec/extensions/true_spec.rb +3 -4
- data/spec/spec_helper.rb +45 -44
- metadata +14 -11
@@ -1,16 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# If the string is empty, then return a default string
|
2
4
|
#------------------------------------------------------------------------------
|
3
|
-
class String
|
4
|
-
|
5
|
+
class String # :nodoc:
|
5
6
|
# If the string is empty, return a default value, otherwise the string
|
6
7
|
#------------------------------------------------------------------------------
|
7
8
|
def to_s_default(default_str = 'n/a')
|
8
|
-
|
9
|
+
empty? || strip.empty? ? default_str : to_s
|
9
10
|
end
|
10
|
-
|
11
|
+
|
11
12
|
#------------------------------------------------------------------------------
|
12
13
|
def as_boolean
|
13
|
-
|
14
|
+
self == 'true' || self == 'yes' || self == '1' ? true : false
|
14
15
|
end
|
15
16
|
|
16
17
|
# given a css type of size (like a width), make it into a valid css value
|
@@ -18,49 +19,47 @@ class String #:nodoc:
|
|
18
19
|
def as_css_size
|
19
20
|
size = self
|
20
21
|
size += 'px' unless size.blank? || size.end_with?('px', '%', 'em') || size == 'auto' || size == 'inherit'
|
21
|
-
|
22
|
+
size
|
22
23
|
end
|
23
24
|
|
24
25
|
# Adds SQL wildcard cahracters to begin/end of string for use in LIKE statements
|
25
26
|
#------------------------------------------------------------------------------
|
26
27
|
def sql_wildcard
|
27
|
-
"%#{self}%"
|
28
|
+
"%#{self}%"
|
28
29
|
end
|
29
|
-
|
30
|
+
|
30
31
|
# Replace non-alphanumbic character
|
31
32
|
#------------------------------------------------------------------------------
|
32
33
|
def replace_non_alphanumeric(replacement = '')
|
33
|
-
|
34
|
+
gsub(/[^\w.-]/, replacement)
|
34
35
|
end
|
35
|
-
|
36
|
+
|
36
37
|
# Santize the string
|
37
38
|
# Note: File.basename doesn't work right with Windows paths on Unix
|
38
39
|
#------------------------------------------------------------------------------
|
39
40
|
def sanitize_filename
|
40
|
-
name =
|
41
|
+
name = strip
|
41
42
|
#--- get only the filename, not the whole path
|
42
|
-
name.gsub!
|
43
|
-
|
43
|
+
name.gsub!(%r{^.*(\\|/)}, '')
|
44
|
+
|
44
45
|
#--- Finally, replace all non alphanumeric, underscore or periods with underscore
|
45
|
-
name.gsub!
|
46
|
-
|
46
|
+
name.gsub!(/[^\w.-]/, '_')
|
47
|
+
name
|
47
48
|
end
|
48
49
|
|
49
|
-
# if a relative url path is given, then expand it by prepending the supplied
|
50
|
+
# if a relative url path is given, then expand it by prepending the supplied
|
50
51
|
# path.
|
51
52
|
#------------------------------------------------------------------------------
|
52
53
|
def expand_url(path = '')
|
53
|
-
if
|
54
|
-
|
55
|
-
|
56
|
-
return path.end_with?('/') ? "#{path}#{self}" : "#{path}/#{self}"
|
57
|
-
end
|
54
|
+
return self if blank? || absolute_url?
|
55
|
+
|
56
|
+
path.end_with?('/') ? "#{path}#{self}" : "#{path}/#{self}"
|
58
57
|
end
|
59
58
|
|
60
59
|
# Test if a url is absolute
|
61
60
|
#------------------------------------------------------------------------------
|
62
61
|
def absolute_url?
|
63
|
-
|
62
|
+
include?('://') || start_with?('/') ? true : false
|
64
63
|
end
|
65
64
|
|
66
65
|
#------------------------------------------------------------------------------
|
@@ -69,33 +68,37 @@ class String #:nodoc:
|
|
69
68
|
# Used for better capitalizing titles and sentences
|
70
69
|
#------------------------------------------------------------------------------
|
71
70
|
def smart_titlecase
|
72
|
-
small_words = %w
|
73
|
-
|
74
|
-
x = split(
|
75
|
-
#
|
71
|
+
small_words = %w[a an and as at but by en for if in of on or the to v v. via vs vs. von]
|
72
|
+
|
73
|
+
x = split(' ').map do |word|
|
74
|
+
# NOTE: word could contain non-word characters!
|
76
75
|
# downcase all small_words, capitalize the rest
|
77
|
-
small_words.include?(word.gsub(/\W/,
|
76
|
+
small_words.include?(word.gsub(/\W/, '').downcase) ? word.downcase! : word.smart_capitalize!
|
78
77
|
word
|
79
78
|
end
|
80
79
|
# capitalize first and last words
|
81
80
|
x.first.smart_capitalize!
|
82
81
|
x.last.smart_capitalize!
|
83
82
|
# small words after colons are capitalized
|
84
|
-
x.join(
|
83
|
+
x.join(' ').gsub(/:\s?(\W*#{small_words.join('|')}\W*)\s/) { ": #{::Regexp.last_match(1).smart_capitalize} " }
|
85
84
|
end
|
86
|
-
|
85
|
+
|
87
86
|
#------------------------------------------------------------------------------
|
88
87
|
def smart_capitalize
|
88
|
+
result = dup
|
89
|
+
|
89
90
|
# ignore any leading crazy characters and capitalize the first real character
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
91
|
+
return result unless self =~ /^['"(\[']*([a-z])/
|
92
|
+
|
93
|
+
i = index(::Regexp.last_match(1))
|
94
|
+
x = result[i, result.length]
|
95
|
+
|
96
|
+
# word with capitals and periods mid-word are left alone
|
97
|
+
result[i, 1] = result[i, 1].upcase unless x =~ (/[A-Z]/) || x =~ (/\.\w+/)
|
98
|
+
|
99
|
+
result
|
97
100
|
end
|
98
|
-
|
101
|
+
|
99
102
|
#------------------------------------------------------------------------------
|
100
103
|
def smart_capitalize!
|
101
104
|
replace(smart_capitalize)
|
@@ -107,38 +110,40 @@ class String #:nodoc:
|
|
107
110
|
# From http://stackoverflow.com/questions/1293573/rails-smart-text-truncation
|
108
111
|
#------------------------------------------------------------------------------
|
109
112
|
def smart_truncate(opts = {})
|
110
|
-
opts = {:
|
113
|
+
opts = { words: 12 }.merge(opts)
|
111
114
|
if opts[:sentences]
|
112
|
-
|
115
|
+
result = split(/\.(\s|$)+/).reject { |s| s.strip.empty? }[0, opts[:sentences]]
|
116
|
+
return "#{result.map(&:strip).join('. ')}..."
|
113
117
|
end
|
114
|
-
|
118
|
+
|
119
|
+
a = split(/\s/) # or /[ ]+/ to only split on spaces
|
115
120
|
n = opts[:words]
|
116
|
-
a[0...n].join(' ') + (a.size > n ? '...' : '')
|
121
|
+
a[0...n].join(' ') + (a.size > n ? '...' : '')
|
117
122
|
end
|
118
123
|
#------------------------------------------------------------------------------
|
119
|
-
|
124
|
+
|
120
125
|
# http://github.com/tenderlove/namecase
|
121
126
|
# NameCase is a Ruby implementation of Lingua::EN::NameCase, a library for
|
122
127
|
# converting strings/names to be properly cased. This is good for converting
|
123
128
|
# denormalized data to human friendly data.
|
124
129
|
#------------------------------------------------------------------------------
|
125
130
|
def name_case(options = {})
|
126
|
-
options = { :
|
131
|
+
options = { lazy: true, irish: true }.merge options
|
127
132
|
|
128
133
|
# Skip if string is mixed case
|
129
134
|
if options[:lazy]
|
130
|
-
first_letter_lower = self[0] ==
|
131
|
-
all_lower_or_upper =
|
135
|
+
first_letter_lower = self[0] == downcase[0]
|
136
|
+
all_lower_or_upper = downcase == self || upcase == self
|
132
137
|
|
133
138
|
return self unless first_letter_lower || all_lower_or_upper
|
134
139
|
end
|
135
140
|
|
136
141
|
localstring = downcase
|
137
|
-
localstring.gsub!(/\b\w
|
138
|
-
localstring.gsub!(
|
142
|
+
localstring.gsub!(/\b\w/, &:upcase)
|
143
|
+
localstring.gsub!(/'\w\b/, &:downcase) # Lowercase 's
|
139
144
|
|
140
145
|
if options[:irish]
|
141
|
-
if localstring =~ /\bMac[A-Za-z]{2,}[^aciozj]\b/
|
146
|
+
if localstring =~ (/\bMac[A-Za-z]{2,}[^aciozj]\b/) || localstring =~ (/\bMc/)
|
142
147
|
match = localstring.match(/\b(Ma?c)([A-Za-z]+)/)
|
143
148
|
localstring.gsub!(/\bMa?c[A-Za-z]+/) { match[1] + match[2].capitalize }
|
144
149
|
|
@@ -157,37 +162,35 @@ class String #:nodoc:
|
|
157
162
|
localstring.gsub!(/\bMacKmin/, 'Mackmin')
|
158
163
|
localstring.gsub!(/\bMacQuarie/, 'Macquarie')
|
159
164
|
end
|
160
|
-
localstring.gsub!('Macmurdo','MacMurdo')
|
165
|
+
localstring.gsub!('Macmurdo', 'MacMurdo')
|
161
166
|
end
|
162
167
|
|
163
168
|
# Fixes for "son (daughter) of" etc
|
164
169
|
localstring.gsub!(/\bAl(?=\s+\w)/, 'al') # al Arabic or forename Al.
|
165
170
|
localstring.gsub!(/\bAp\b/, 'ap') # ap Welsh.
|
166
|
-
localstring.gsub!(/\bBen(?=\s+\w)/,'ben') # ben Hebrew or forename Ben.
|
167
|
-
localstring.gsub!(/\bDell([ae])\b/,'dell\1')
|
168
|
-
localstring.gsub!(/\bD([aeiou])\b/,'d\1')
|
169
|
-
localstring.gsub!(/\bD([ao]s)\b/,'d\1')
|
170
|
-
localstring.gsub!(/\bDe([lr])\b/,'de\1')
|
171
|
-
localstring.gsub!(/\bEl\b/,'el') # el Greek or El Spanish.
|
172
|
-
localstring.gsub!(/\bLa\b/,'la') # la French or La Spanish.
|
173
|
-
localstring.gsub!(/\bL([eo])\b/,'l\1')
|
174
|
-
localstring.gsub!(/\bVan(?=\s+\w)/,'van')
|
175
|
-
localstring.gsub!(/\bVon\b/,'von')
|
171
|
+
localstring.gsub!(/\bBen(?=\s+\w)/, 'ben') # ben Hebrew or forename Ben.
|
172
|
+
localstring.gsub!(/\bDell([ae])\b/, 'dell\1') # della and delle Italian.
|
173
|
+
localstring.gsub!(/\bD([aeiou])\b/, 'd\1') # da, de, di Italian; du French; do Brasil
|
174
|
+
localstring.gsub!(/\bD([ao]s)\b/, 'd\1') # das, dos Brasileiros
|
175
|
+
localstring.gsub!(/\bDe([lr])\b/, 'de\1') # del Italian; der Dutch/Flemish.
|
176
|
+
localstring.gsub!(/\bEl\b/, 'el') # el Greek or El Spanish.
|
177
|
+
localstring.gsub!(/\bLa\b/, 'la') # la French or La Spanish.
|
178
|
+
localstring.gsub!(/\bL([eo])\b/, 'l\1') # lo Italian; le French.
|
179
|
+
localstring.gsub!(/\bVan(?=\s+\w)/, 'van') # van German or forename Van.
|
180
|
+
localstring.gsub!(/\bVon\b/, 'von') # von Dutch/Flemish
|
176
181
|
|
177
182
|
# Fix roman numeral names
|
178
183
|
localstring.gsub!(
|
179
184
|
/ \b ( (?: [Xx]{1,3} | [Xx][Ll] | [Ll][Xx]{0,3} )?
|
180
|
-
(?: [Ii]{1,3} | [Ii][VvXx] | [Vv][Ii]{0,3} )? ) \b /x
|
181
|
-
)
|
185
|
+
(?: [Ii]{1,3} | [Ii][VvXx] | [Vv][Ii]{0,3} )? ) \b /x, &:upcase
|
186
|
+
)
|
182
187
|
|
183
188
|
localstring
|
184
189
|
end
|
185
|
-
|
190
|
+
|
186
191
|
# Modifies _str_ in place and properly namecases the string.
|
187
192
|
#------------------------------------------------------------------------------
|
188
193
|
def name_case!
|
189
|
-
|
194
|
+
gsub!(self, name_case)
|
190
195
|
end
|
191
|
-
|
192
196
|
end
|
193
|
-
|
@@ -1,11 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
#------------------------------------------------------------------------------
|
2
4
|
class Time
|
3
|
-
|
4
5
|
#------------------------------------------------------------------------------
|
5
6
|
def localize(options = {})
|
6
|
-
options = {:
|
7
|
-
I18n.localize(self, options)
|
7
|
+
options = { format: options } if options.instance_of?(String)
|
8
|
+
I18n.localize(self, **options)
|
8
9
|
end
|
9
|
-
|
10
10
|
end
|
11
|
-
|
data/lib/dm_ruby_extensions.rb
CHANGED
@@ -1,18 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_support'
|
2
4
|
require 'active_support/core_ext'
|
3
5
|
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
10
|
-
require
|
11
|
-
require
|
12
|
-
require
|
13
|
-
require
|
14
|
-
require
|
15
|
-
require
|
6
|
+
require 'dm_ruby_extensions/version'
|
7
|
+
require 'dm_ruby_extensions/extend_array'
|
8
|
+
require 'dm_ruby_extensions/extend_date'
|
9
|
+
require 'dm_ruby_extensions/extend_datetime'
|
10
|
+
require 'dm_ruby_extensions/extend_hash'
|
11
|
+
require 'dm_ruby_extensions/extend_nil'
|
12
|
+
require 'dm_ruby_extensions/extend_string'
|
13
|
+
require 'dm_ruby_extensions/extend_time'
|
14
|
+
require 'dm_ruby_extensions/extend_integer'
|
15
|
+
require 'dm_ruby_extensions/extend_numeric'
|
16
|
+
require 'dm_ruby_extensions/extend_true'
|
17
|
+
require 'dm_ruby_extensions/extend_false'
|
16
18
|
|
19
|
+
# DmRubyExtensions
|
17
20
|
module DmRubyExtensions
|
18
21
|
end
|
@@ -1,39 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'dm_ruby_extensions'
|
3
5
|
|
4
6
|
describe Array do
|
5
|
-
|
6
7
|
describe 'closest_max' do
|
7
8
|
#------------------------------------------------------------------------------
|
8
9
|
it 'return the value that is closest to the value in the array, rounded down' do
|
9
|
-
expect([0,5,7,8,11,16].closest_max(6)).to eq 5
|
10
|
-
expect([0,5,7,8,11,16].closest_max(7)).to eq 7
|
10
|
+
expect([0, 5, 7, 8, 11, 16].closest_max(6)).to eq 5
|
11
|
+
expect([0, 5, 7, 8, 11, 16].closest_max(7)).to eq 7
|
11
12
|
end
|
12
13
|
end
|
13
14
|
|
14
15
|
describe 'extract_options!' do
|
15
16
|
#------------------------------------------------------------------------------
|
16
17
|
it "Removes and returns the last element in the array if it's a hash, otherwise returns a blank hash" do
|
17
|
-
args = [1, 2, {:
|
18
|
-
expect(args.extract_options!).to eq
|
18
|
+
args = [1, 2, { a: :b }]
|
19
|
+
expect(args.extract_options!).to eq({ a: :b })
|
19
20
|
expect(args).to eq [1, 2]
|
20
|
-
expect(args.extract_options!).to eq
|
21
|
+
expect(args.extract_options!).to eq({})
|
21
22
|
end
|
22
23
|
end
|
23
|
-
|
24
|
+
|
24
25
|
describe 'xss_aware_join' do
|
25
26
|
it 'safely join safe and unsafe strings' do
|
26
|
-
safe =
|
27
|
-
unsafe =
|
28
|
-
expect([safe, unsafe].xss_aware_join).to eq
|
27
|
+
safe = '<p>Safe</p>'.html_safe
|
28
|
+
unsafe = '<script></script>'
|
29
|
+
expect([safe, unsafe].xss_aware_join).to eq '<p>Safe</p><script></script>'
|
29
30
|
expect([safe, unsafe].xss_aware_join.class).to eq ActiveSupport::SafeBuffer
|
30
31
|
end
|
31
32
|
end
|
32
|
-
|
33
|
+
|
33
34
|
describe 'css_join' do
|
34
35
|
it 'given an array of css classes/styles, join them into one string' do
|
35
36
|
classes = ['panel', '', 'panel-body', nil, 'red']
|
36
37
|
expect(classes.css_join(' ')).to eq 'panel panel-body red'
|
37
38
|
end
|
38
39
|
end
|
39
|
-
end
|
40
|
+
end
|
@@ -1,14 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'dm_ruby_extensions'
|
3
5
|
|
4
6
|
describe Date do
|
5
|
-
|
6
7
|
describe 'to_age' do
|
7
|
-
it 'return number of years since now'
|
8
|
-
expect(Date.new(
|
8
|
+
it 'return number of years since now' do
|
9
|
+
expect(Date.new(Time.now.year - 50, Time.now.month, Time.now.day).to_age).to eq 50
|
9
10
|
end
|
10
11
|
end
|
11
|
-
|
12
|
+
|
12
13
|
describe 'localize' do
|
13
14
|
it 'localize the date based on the format' do
|
14
15
|
I18n.enforce_available_locales = false
|
@@ -19,11 +20,11 @@ describe Date do
|
|
19
20
|
expect(date.localize('%b %Y')).to eq 'Nov 2012'
|
20
21
|
end
|
21
22
|
end
|
22
|
-
|
23
|
+
|
23
24
|
describe 'to_index' do
|
24
25
|
it 'creates unique sortable index for a date' do
|
25
26
|
date = Date.new(2012, 11, 23)
|
26
|
-
expect(date.to_index).to eq
|
27
|
+
expect(date.to_index).to eq 12_328
|
27
28
|
end
|
28
29
|
end
|
29
|
-
end
|
30
|
+
end
|
@@ -1,14 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'dm_ruby_extensions'
|
3
5
|
|
4
6
|
describe DateTime do
|
5
|
-
|
6
7
|
describe 'to_age' do
|
7
|
-
it 'return number of years since now'
|
8
|
-
expect(DateTime.new(
|
8
|
+
it 'return number of years since now' do
|
9
|
+
expect(DateTime.new(Time.now.year - 50, Time.now.month, Time.now.day).to_age).to eq 50
|
9
10
|
end
|
10
11
|
end
|
11
|
-
|
12
|
+
|
12
13
|
describe 'localize' do
|
13
14
|
it 'localize the date based on the format' do
|
14
15
|
I18n.enforce_available_locales = false
|
@@ -19,11 +20,11 @@ describe DateTime do
|
|
19
20
|
expect(date.localize('%b %Y')).to eq 'Nov 2012'
|
20
21
|
end
|
21
22
|
end
|
22
|
-
|
23
|
+
|
23
24
|
describe 'to_index' do
|
24
25
|
it 'creates unique sortable index for a date' do
|
25
26
|
date = Date.new(2012, 11, 23)
|
26
|
-
expect(date.to_index).to eq
|
27
|
+
expect(date.to_index).to eq 12_328
|
27
28
|
end
|
28
29
|
end
|
29
|
-
end
|
30
|
+
end
|
@@ -1,12 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'dm_ruby_extensions'
|
3
5
|
|
4
6
|
describe Hash do
|
5
|
-
|
6
7
|
describe 'convert_date' do
|
7
8
|
#------------------------------------------------------------------------------
|
8
9
|
it 'converts a rails date hash' do
|
9
|
-
datehash = {'start_date(1i)' => 2012, 'start_date(2i)' => 11, 'start_date(3i)' => 23}
|
10
|
+
datehash = { 'start_date(1i)' => 2012, 'start_date(2i)' => 11, 'start_date(3i)' => 23 }
|
10
11
|
expect(datehash.convert_date(:start_date)).to eq Date.new(2012, 11, 23)
|
11
12
|
expect(datehash.convert_date(:end_date)).to eq nil
|
12
13
|
end
|
@@ -15,7 +16,8 @@ describe Hash do
|
|
15
16
|
describe 'convert_datetime' do
|
16
17
|
#------------------------------------------------------------------------------
|
17
18
|
it 'converts a rails datetime hash' do
|
18
|
-
datehash = {'start_date(1i)' => 2012, 'start_date(2i)' => 11, 'start_date(3i)' => 23, 'start_date(4i)' => 8,
|
19
|
+
datehash = { 'start_date(1i)' => 2012, 'start_date(2i)' => 11, 'start_date(3i)' => 23, 'start_date(4i)' => 8,
|
20
|
+
'start_date(5i)' => 35 }
|
19
21
|
expect(datehash.convert_datetime(:start_date)).to eq Time.local(2012, 11, 23, 8, 35)
|
20
22
|
expect(datehash.convert_datetime(:end_date)).to eq nil
|
21
23
|
end
|
@@ -24,32 +26,33 @@ describe Hash do
|
|
24
26
|
describe 'url_query_string' do
|
25
27
|
#------------------------------------------------------------------------------
|
26
28
|
it 'converts hash of parameters to a query string' do
|
27
|
-
params = {tag: 'shoe', as_admin: '1'}
|
28
|
-
expect(params.url_query_string).to eq '/?tag=shoe
|
29
|
-
expect(params.url_query_string(false)).to eq '?tag=shoe
|
29
|
+
params = { tag: 'shoe', as_admin: '1' }
|
30
|
+
expect(params.url_query_string).to eq '/?as_admin=1&tag=shoe'
|
31
|
+
expect(params.url_query_string(false)).to eq '?as_admin=1&tag=shoe'
|
30
32
|
end
|
31
33
|
end
|
32
34
|
|
33
35
|
describe 'rekey' do
|
34
36
|
#------------------------------------------------------------------------------
|
35
37
|
it 'if a key map is given, then the first key is changed to the second key.' do
|
36
|
-
foo = { :
|
37
|
-
expect(foo.rekey(:
|
38
|
-
expect(foo.rekey(:
|
39
|
-
expect(foo.rekey('foo'=>'bar')).to eq({ :
|
38
|
+
foo = { a: 1, b: 2 }
|
39
|
+
expect(foo.rekey(a: 'a')).to eq({ 'a' => 1, :b => 2 })
|
40
|
+
expect(foo.rekey(b: :x)).to eq({ a: 1, x: 2 })
|
41
|
+
expect(foo.rekey('foo' => 'bar')).to eq({ a: 1, b: 2 })
|
40
42
|
end
|
41
|
-
|
43
|
+
|
42
44
|
#------------------------------------------------------------------------------
|
45
|
+
# rubocop:disable Style/SymbolProc
|
43
46
|
it 'converts all keys in the Hash accroding to the given block procedure' do
|
44
|
-
foo = { :
|
45
|
-
expect(foo.rekey{ |k| k.to_s }).to eq({
|
47
|
+
foo = { name: 'Gavin', wife: :Lisa }
|
48
|
+
expect(foo.rekey { |k| k.to_s }).to eq({ 'name' => 'Gavin', 'wife' => :Lisa })
|
46
49
|
end
|
47
|
-
|
50
|
+
# rubocop:enable Style/SymbolProc
|
51
|
+
|
48
52
|
#------------------------------------------------------------------------------
|
49
53
|
it 'converts all keys to symbols' do
|
50
54
|
foo = { 'name' => 'Gavin', 'wife' => :Lisa }
|
51
|
-
expect(foo.rekey).to eq({ :
|
55
|
+
expect(foo.rekey).to eq({ name: 'Gavin', wife: :Lisa })
|
52
56
|
end
|
53
57
|
end
|
54
|
-
|
55
|
-
end
|
58
|
+
end
|
@@ -1,8 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'dm_ruby_extensions'
|
3
5
|
|
4
6
|
describe Integer do
|
5
|
-
|
6
7
|
describe 'factorial' do
|
7
8
|
#------------------------------------------------------------------------------
|
8
9
|
it 'return the factorial of the number' do
|
@@ -16,5 +17,4 @@ describe Integer do
|
|
16
17
|
specify { expect(0.as_boolean).to be_falsey }
|
17
18
|
specify { expect(20.as_boolean).to be_truthy }
|
18
19
|
end
|
19
|
-
|
20
|
-
end
|
20
|
+
end
|
data/spec/extensions/nil_spec.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'dm_ruby_extensions'
|
3
5
|
|
4
6
|
describe NilClass do
|
5
|
-
|
6
7
|
describe 'to_s_default' do
|
7
8
|
it 'returns default string' do
|
8
9
|
expect(nil.to_s_default).to eq 'n/a'
|
@@ -13,9 +14,9 @@ describe NilClass do
|
|
13
14
|
describe 'as_boolean' do
|
14
15
|
specify { expect(nil.as_boolean).to eq false }
|
15
16
|
end
|
16
|
-
|
17
|
+
|
17
18
|
describe 'to_age' do
|
18
|
-
specify { expect(nil.to_age).to eq 0}
|
19
|
+
specify { expect(nil.to_age).to eq 0 }
|
19
20
|
end
|
20
21
|
|
21
22
|
describe 'sql_wildcard' do
|
@@ -25,10 +26,9 @@ describe NilClass do
|
|
25
26
|
describe 'utc' do
|
26
27
|
specify { expect(nil.utc).to eq '' }
|
27
28
|
end
|
28
|
-
|
29
|
+
|
29
30
|
describe '+(y)' do
|
30
31
|
specify { expect(nil + 12).to eq 12 }
|
31
|
-
specify { expect(
|
32
|
+
specify { expect('foo').to eq 'foo' }
|
32
33
|
end
|
33
|
-
|
34
|
-
end
|
34
|
+
end
|
@@ -1,8 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'dm_ruby_extensions'
|
3
5
|
|
4
6
|
describe Numeric do
|
5
|
-
|
6
7
|
describe 'percent_of' do
|
7
8
|
it 'returns percentage of number' do
|
8
9
|
expect(25.percent_of(100)).to eq 25
|
@@ -10,5 +11,4 @@ describe Numeric do
|
|
10
11
|
expect(25.percent_of(80, 2)).to eq 31.25
|
11
12
|
end
|
12
13
|
end
|
13
|
-
|
14
|
-
end
|
14
|
+
end
|