macmillan-utils 1.0.18 → 1.0.19

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: acc95b947ca749c2078f77ca59d7744f65c685c5
4
- data.tar.gz: 23b579c7ff8f10f37d4029f86712f0d33fcaccc2
3
+ metadata.gz: 00268cd5e517f8817ee7515c2bd5c1d94df9e242
4
+ data.tar.gz: f5252d519821c65a1eea390284b5f092ac7c232d
5
5
  SHA512:
6
- metadata.gz: c4cee43c55460edfc8eb646256b95d53ed7fd48fa902cca3f8073cee3c046bdcb50e7a630c75f362eeee870ea1bf22b93c4a26987d1c6d7d568488dcc8a7ed46
7
- data.tar.gz: 60e29f0eae35b3d2a959b31c92de73770da61f56db07e518ddd29eebe2599c7b2b8f1b1c4e6291714cf1a4c76edac3d57510382e1876ef4296e50f9c381bb40f
6
+ metadata.gz: cf346d7f3a5035e0f51bdfe2e06f18a2f9fc4b8329c36f3d30609db4ebb3ece48c2247e9977e5b843d49a33560955d104d7d56a60f9c3dc19ad0d950d96367b0
7
+ data.tar.gz: 09352c5f41b7ccb073e3f51e6f05e137d296531d78317973550fd39c2b1cfa6cd0516eeba73e2df733acd3dff29d5c77b92881082ecf4dca190866bcfd6b51cb
@@ -12,5 +12,6 @@ module Macmillan
12
12
  autoload :StatsdDecorator, 'macmillan/utils/statsd_decorator'
13
13
  autoload :StatsdMiddleware, 'macmillan/utils/statsd_middleware'
14
14
  autoload :StatsdControllerHelper, 'macmillan/utils/statsd_controller_helper'
15
+ autoload :Helper, 'macmillan/utils/helper'
15
16
  end
16
17
  end
@@ -0,0 +1,11 @@
1
+ module Macmillan
2
+ module Utils
3
+ ##
4
+ # Namespace for helpers classes
5
+ #
6
+ module Helper
7
+ autoload :HashKeyHelper, 'macmillan/utils/helper/hash_key_helper'
8
+ autoload :StringConversionHelper, 'macmillan/utils/helper/string_conversion_helper'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,46 @@
1
+ module Macmillan
2
+ module Utils
3
+ module Helper
4
+ module HashKeyHelper
5
+ include StringConversionHelper
6
+
7
+ def convert_keys_to_snakecase_and_symbols(obj)
8
+ case obj
9
+ when Array
10
+ obj.reduce([]) do |res, val|
11
+ res << case val
12
+ when Hash, Array
13
+ convert_keys_to_snakecase_and_symbols(val)
14
+ else
15
+ val
16
+ end
17
+ res
18
+ end
19
+ when Hash
20
+ obj.reduce({}) do |res, (key, val)|
21
+ nkey = snakecase_string(key.to_s).to_sym
22
+ nval = case val
23
+ when Hash, Array
24
+ convert_keys_to_snakecase_and_symbols(val)
25
+ else
26
+ val
27
+ end
28
+ res[nkey] = nval
29
+ res
30
+ end
31
+ else
32
+ obj
33
+ end
34
+ end
35
+
36
+ def convert_key_to_singular(key)
37
+ if key.to_s == 'summaries'
38
+ :summary
39
+ else
40
+ key.to_s.chomp('s').to_sym
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,35 @@
1
+ module Macmillan
2
+ module Utils
3
+ module Helper
4
+ module StringConversionHelper
5
+ module_function
6
+
7
+ # shamelessly ripped out of the 'facets' gem
8
+ def snakecase_string(string)
9
+ string
10
+ .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
11
+ .gsub(/([a-z\d])([A-Z])/,'\1_\2')
12
+ .tr('-', '_')
13
+ .gsub(/\s/, '_')
14
+ .gsub(/__+/, '_')
15
+ .downcase
16
+ end
17
+
18
+ # also shamelessly ripped out of the 'facets' gem
19
+ def upper_camelcase_string(string)
20
+ separators = ['_', '\s']
21
+
22
+ separators.each do |s|
23
+ string = string.gsub(/(?:#{s}+)([a-z])/){ $1.upcase }
24
+ end
25
+
26
+ string.gsub(/(\A|\s)([a-z])/){ $1 + $2.upcase }
27
+ end
28
+
29
+ def camelcase_to_snakecase_symbol(string)
30
+ snakecase_string(string).to_sym
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,124 @@
1
+ require 'spec_helper'
2
+
3
+ describe Macmillan::Utils::Helper::HashKeyHelper do
4
+ include Macmillan::Utils::Helper::HashKeyHelper
5
+
6
+ describe '#convert_keys_to_snakecase_and_symbols' do
7
+ context 'when it is a flat hash' do
8
+ let(:example) do
9
+ { 'aKey' => 'woo', '_debug' => {} }
10
+ end
11
+
12
+ let(:expected) do
13
+ { a_key: 'woo', _debug: {} }
14
+ end
15
+
16
+ it 'converts all the keys to snake_case and symbols' do
17
+ result = convert_keys_to_snakecase_and_symbols(example)
18
+ expect(result).to eq(expected)
19
+ end
20
+ end
21
+
22
+ context 'a hash containing child hashes' do
23
+ let(:example) do
24
+ { 'aKey' => { 'someKey' => 'woo', 'someKey2' => 'woo' } }
25
+ end
26
+
27
+ let(:expected) do
28
+ { a_key: { some_key: 'woo', some_key2: 'woo' } }
29
+ end
30
+
31
+ it 'converts all the keys to snake_case and symbols' do
32
+ result = convert_keys_to_snakecase_and_symbols(example)
33
+ expect(result).to eq(expected)
34
+ end
35
+ end
36
+
37
+ context 'a hash containing arrays of hashes as values' do
38
+ let(:example) do
39
+ {
40
+ 'aKey' => [
41
+ { 'someKey' => 'woo' },
42
+ { 'someKey2' => 'waa' }
43
+ ]
44
+ }
45
+ end
46
+
47
+ let(:expected) do
48
+ {
49
+ a_key: [
50
+ { some_key: 'woo' },
51
+ { some_key2: 'waa' }
52
+ ]
53
+ }
54
+ end
55
+
56
+ it 'converts all the keys to snake_case and symbols' do
57
+ result = convert_keys_to_snakecase_and_symbols(example)
58
+ expect(result).to eq(expected)
59
+ end
60
+ end
61
+
62
+ context 'with a monster' do
63
+ let(:example) do
64
+ {
65
+ 'aKey' => [
66
+ { 'someKey' => 'woo' },
67
+ { 'someKey2' => 'waa' },
68
+ [
69
+ { 'someKey' => 'woo' },
70
+ {
71
+ 'argh' => [
72
+ { 'someKey' => 'woo' }
73
+ ]
74
+ }
75
+ ]
76
+ ]
77
+ }
78
+ end
79
+
80
+ let(:expected) do
81
+ {
82
+ a_key: [
83
+ { some_key: 'woo' },
84
+ { some_key2: 'waa' },
85
+ [
86
+ { some_key: 'woo' },
87
+ {
88
+ argh: [
89
+ { some_key: 'woo' }
90
+ ]
91
+ }
92
+ ]
93
+
94
+ ]
95
+ }
96
+ end
97
+
98
+ it 'converts all the keys to snake_case and symbols' do
99
+ result = convert_keys_to_snakecase_and_symbols(example)
100
+ expect(result).to eq(expected)
101
+ end
102
+ end
103
+ end
104
+
105
+ describe '#convert_key_to_singular' do
106
+ context 'when the key is :summaries' do
107
+ let(:key) { :summaries }
108
+
109
+ it 'converts the key to :summary' do
110
+ expect(convert_key_to_singular(key)).to eq(:summary)
111
+ end
112
+ end
113
+
114
+ context 'when the key is NOT :summaries' do
115
+ let(:articles_key) { :articles }
116
+ let(:primary_article_types_key) { :primary_article_types }
117
+
118
+ it 'converts the key to singular' do
119
+ expect(convert_key_to_singular(articles_key)).to eq(:article)
120
+ expect(convert_key_to_singular(primary_article_types_key)).to eq(:primary_article_type)
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe Macmillan::Utils::Helper::StringConversionHelper do
4
+ include Macmillan::Utils::Helper::StringConversionHelper
5
+
6
+ describe '#snakecase_string' do
7
+ it 'downcases' do
8
+ expect(snakecase_string('URI')).to eq('uri')
9
+ end
10
+
11
+ it 'converts spaces' do
12
+ expect(snakecase_string('MyStuff HisStuff')).to eq('my_stuff_his_stuff')
13
+ end
14
+
15
+ it 'does not alter path dividers' do
16
+ expect(snakecase_string('/my_module/my_class/')).to eq('/my_module/my_class/')
17
+ end
18
+ end
19
+
20
+ describe '#upper_camelcase_string' do
21
+ let(:spacey_string) {'I feel spaced out'}
22
+ let(:underscorey_string) {'i_feel_under_scored'}
23
+
24
+ it 'converts a string containing spaces to upper camelcase' do
25
+ expect(upper_camelcase_string(spacey_string)).to eq('IFeelSpacedOut')
26
+ end
27
+
28
+ it 'converts a string containing underscores to upper camelcase' do
29
+ expect(upper_camelcase_string(underscorey_string)).to eq('IFeelUnderScored')
30
+ end
31
+ end
32
+
33
+ describe '#camelcase_to_snakecase_symbol' do
34
+ let(:camel) {'LookAtThisHumpyOldCamel'}
35
+
36
+ it 'converts a camelcase string to a snakecase symbol equivalent' do
37
+ expect(camelcase_to_snakecase_symbol(camel)).to eq(:look_at_this_humpy_old_camel)
38
+ end
39
+ end
40
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: macmillan-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.18
4
+ version: 1.0.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Macmillan Science and Education (New Publsihing Platforms)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-17 00:00:00.000000000 Z
11
+ date: 2015-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -226,6 +226,9 @@ files:
226
226
  - lib/macmillan/utils/bundler/gem_helper.rb
227
227
  - lib/macmillan/utils/cucumber/cucumber_defaults.rb
228
228
  - lib/macmillan/utils/cucumber/webmock_helper.rb
229
+ - lib/macmillan/utils/helper.rb
230
+ - lib/macmillan/utils/helper/hash_key_helper.rb
231
+ - lib/macmillan/utils/helper/string_conversion_helper.rb
229
232
  - lib/macmillan/utils/logger.rb
230
233
  - lib/macmillan/utils/logger/factory.rb
231
234
  - lib/macmillan/utils/logger/formatter.rb
@@ -251,6 +254,8 @@ files:
251
254
  - lib/macmillan/utils/test_helpers/simplecov_helper.rb
252
255
  - macmillan-utils.gemspec
253
256
  - spec/fixtures/config/application.yml
257
+ - spec/lib/macmillan/utils/helper/hash_key_helper_spec.rb
258
+ - spec/lib/macmillan/utils/helper/string_conversion_helper_spec.rb
254
259
  - spec/lib/macmillan/utils/logger/factory_spec.rb
255
260
  - spec/lib/macmillan/utils/logger/formatter_spec.rb
256
261
  - spec/lib/macmillan/utils/middleware/uuid_spec.rb
@@ -287,6 +292,8 @@ summary: A collection of useful patterns we (Macmillan Science and Education) us
287
292
  in our Ruby applications.
288
293
  test_files:
289
294
  - spec/fixtures/config/application.yml
295
+ - spec/lib/macmillan/utils/helper/hash_key_helper_spec.rb
296
+ - spec/lib/macmillan/utils/helper/string_conversion_helper_spec.rb
290
297
  - spec/lib/macmillan/utils/logger/factory_spec.rb
291
298
  - spec/lib/macmillan/utils/logger/formatter_spec.rb
292
299
  - spec/lib/macmillan/utils/middleware/uuid_spec.rb