arstotzka 1.3.1 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a4b941b1b173ae1f57c979ab7b58771a675dc297184cecb936c4e3e5854de5c
4
- data.tar.gz: 4dcc0ad6c185c423aa7cb9376ebc47f392a9fb7eccb5753f0448817a1337dc10
3
+ metadata.gz: fc52a24a605aeff67f5a310bf8e6bc7dbe300374ba03678ef4abe68f05ae4f4e
4
+ data.tar.gz: 37ec2565487d9b560d1da8571b12dc6d562a888366e8d8ecb368089ddbf2cb33
5
5
  SHA512:
6
- metadata.gz: 55402898468c8a5f593abfba401cf36a76e08771b4b98c6d1240af8224f54d141e13f6c19033befee4a9244289bee3b2aebf0f682a874090162c7fb529aaf63b
7
- data.tar.gz: 064e5a70a830ea70ff8d1dc544f36f81ebd88c8ce4df77efad625a1e4d9d67d8b5245b5dc8f851c9fc7ccd8dff5ef7eb9a83ad702556a494ed7084fdae08515f
6
+ metadata.gz: 995a859fdd57fc6a32cff22aec0ae542b22148de6ab5a16497e62acec94249e7c56e61e39b8ca336ed2383ebae8ec0efee3e8f8d4fd18366795a66cf88ce6baf
7
+ data.tar.gz: 55376d536beeb947697540cbdd3a8909b17e85d86beb5cde27d0535975ce59380cf49503d3eabbf3e7af7ae51b6eb77d8c1fce6c3dadc3f8c2b2212b65a1ee36
data/README.md CHANGED
@@ -19,7 +19,7 @@ JSON keys)
19
19
 
20
20
  Yard Documentation
21
21
  -------------------
22
- https://www.rubydoc.info/gems/arstotzka/1.3.1
22
+ https://www.rubydoc.info/gems/arstotzka/1.3.2
23
23
 
24
24
  Instalation
25
25
  ---------------
@@ -25,6 +25,9 @@ rules:
25
25
  - Arstotzka::Exception::FetcherBuilderNotFound#klass
26
26
  - Arstotzka::Fetcher#options
27
27
  - Arstotzka::FetcherBuilder#options
28
+ - Arstotzka::KeyReader#base_key
29
+ - Arstotzka::KeyReader#hash
30
+ - Arstotzka::KeyReader#options
28
31
  - Arstotzka::HashReader#options
29
32
  - Arstotzka::MethodBuilder#attr_names
30
33
  - Arstotzka::MethodBuilder#options
@@ -40,6 +43,9 @@ rules:
40
43
  - Arstotzka::Exception::FetcherBuilderNotFound#klass
41
44
  - Arstotzka::Fetcher#options
42
45
  - Arstotzka::FetcherBuilder#options
46
+ - Arstotzka::KeyReader#base_key
47
+ - Arstotzka::KeyReader#hash
48
+ - Arstotzka::KeyReader#options
43
49
  - Arstotzka::HashReader#options
44
50
  - Arstotzka::MethodBuilder#attr_names
45
51
  - Arstotzka::MethodBuilder#options
@@ -172,6 +172,7 @@ module Arstotzka
172
172
  autoload :Exception, 'arstotzka/exception'
173
173
  autoload :Fetcher, 'arstotzka/fetcher'
174
174
  autoload :FetcherBuilder, 'arstotzka/fetcher_builder'
175
+ autoload :KeyReader, 'arstotzka/key_reader'
175
176
  autoload :HashReader, 'arstotzka/hash_reader'
176
177
  autoload :MethodBuilder, 'arstotzka/method_builder'
177
178
  autoload :Options, 'arstotzka/options'
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Arstotzka
4
+ # @api private
5
+ #
6
+ # Class responsible for reading values from a hash
7
+ class KeyReader
8
+ include Base
9
+
10
+ # Creates a new instance of Reader
11
+ #
12
+ # @param [Hash] hash Hash where the key will be found
13
+ # @param [String] base_key The key to be checked
14
+ # (before case change)
15
+ def initialize(hash, base_key, options_hash = {})
16
+ self.options = options_hash
17
+
18
+ @hash = hash
19
+ @base_key = base_key
20
+ end
21
+
22
+ # Reads value from hash key
23
+ #
24
+ # @raise Arstotzka::Exception::KeyNotFound
25
+ #
26
+ # @return [Object]
27
+ #
28
+ # @example Simple usage
29
+ # hash = { theKey: 'value' }
30
+ # key = { 'the_key' }
31
+ # reader = Arstotzka::KeyReader.new(hash, key)
32
+ #
33
+ # reader.read # returns 'value'
34
+ #
35
+ # @example
36
+ # hash = { 'the_key' => 'value' }
37
+ # key = 'TheKey'
38
+ # reader = Arstotzka::KeyReader.new(hash, key)
39
+ #
40
+ # reader.read # returns 'value'
41
+ #
42
+ def read
43
+ raise Exception::KeyNotFound unless key?
44
+
45
+ hash.key?(key) ? hash[key] : hash[key.to_sym]
46
+ end
47
+
48
+ private
49
+
50
+ attr_reader :hash, :base_key, :options
51
+
52
+ # @private
53
+ #
54
+ # Checks if hash contains or not the key
55
+ #
56
+ # The check first happens using String key and,
57
+ # in case of not found, searches as symbol
58
+ #
59
+ # @return [Boolean]
60
+ #
61
+ # @see #check_key!
62
+ def key?
63
+ return unless hash
64
+ hash.key?(key) || hash.key?(key.to_sym)
65
+ end
66
+
67
+ # @private
68
+ #
69
+ # Transforms the key to have the correct case
70
+ #
71
+ # the possible cases (instance attribute) are
72
+ # - lower_camel: for cammel case with first letter lowercase
73
+ # - upper_camel: for cammel case with first letter uppercase
74
+ # - snake: for snake case
75
+ #
76
+ # @param [String] key the key to be transformed
77
+ #
78
+ # @return [String] the string transformed
79
+ def key
80
+ @key ||= case options.case
81
+ when :lower_camel
82
+ base_key.camelize(:lower)
83
+ when :upper_camel
84
+ base_key.camelize(:upper)
85
+ when :snake
86
+ base_key.underscore
87
+ end
88
+ end
89
+ end
90
+ end
@@ -23,7 +23,7 @@ module Arstotzka
23
23
  def initialize(options_hash = {})
24
24
  self.options = options_hash
25
25
 
26
- @keys = options.keys.map(&method(:change_case))
26
+ @keys = options.keys
27
27
  end
28
28
 
29
29
  # Reads the value of one key in the hash
@@ -63,9 +63,7 @@ module Arstotzka
63
63
  def read(hash, index)
64
64
  key = keys[index]
65
65
 
66
- check_key!(hash, key)
67
-
68
- hash.key?(key) ? hash[key] : hash[key.to_sym]
66
+ KeyReader.new(hash, key, options).read
69
67
  end
70
68
 
71
69
  # @private
@@ -84,61 +82,5 @@ module Arstotzka
84
82
 
85
83
  # @private
86
84
  attr_reader :keys, :options
87
-
88
- # @private
89
- #
90
- # Checks if a hash contains or not the key
91
- #
92
- # if the key is not found, an execption is raised
93
- #
94
- # @raise Arstotzka::Exception::KeyNotFound
95
- #
96
- # @return [NilClass]
97
- #
98
- # @see #key?
99
- def check_key!(hash, key)
100
- return if key?(hash, key)
101
- raise Exception::KeyNotFound
102
- end
103
-
104
- # @private
105
- #
106
- # Checks if a hash contains or not the key
107
- #
108
- # The check first happens using String key and,
109
- # in case of not found, searches as symbol
110
- #
111
- # @param [Hash] hash Hash where the key will be found
112
- # @param [String] key The key to be checked
113
- #
114
- # @return [Boolean]
115
- #
116
- # @see #check_key!
117
- def key?(hash, key)
118
- hash&.key?(key) || hash&.key?(key.to_sym)
119
- end
120
-
121
- # @private
122
- #
123
- # Transforms the key to have the correct case
124
- #
125
- # the possible cases (instance attribute) are
126
- # - lower_camel: for cammel case with first letter lowercase
127
- # - upper_camel: for cammel case with first letter uppercase
128
- # - snake: for snake case
129
- #
130
- # @param [String] key the key to be transformed
131
- #
132
- # @return [String] the string transformed
133
- def change_case(key)
134
- case options.case
135
- when :lower_camel
136
- key.camelize(:lower)
137
- when :upper_camel
138
- key.camelize(:upper)
139
- when :snake
140
- key.underscore
141
- end
142
- end
143
85
  end
144
86
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Arstotzka
4
- VERSION = '1.3.1'
4
+ VERSION = '1.3.2'
5
5
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Arstotzka::KeyReader do
6
+ describe 'yard' do
7
+ describe '#read' do
8
+ describe 'Simple Usage' do
9
+ subject(:reader) { described_class.new hash, key }
10
+
11
+ let(:key) { 'the_key' }
12
+ let(:hash) { { theKey: 'value' } }
13
+
14
+ it 'reads the value' do
15
+ expect(reader.read).to eq('value')
16
+ end
17
+ end
18
+
19
+ describe 'Specifying snakecase' do
20
+ subject(:reader) { described_class.new hash, key, case: :snake }
21
+
22
+ let(:key) { 'TheKey' }
23
+ let(:hash) { { 'the_key' => 'value' } }
24
+
25
+ it 'reads the value' do
26
+ expect(reader.read).to eq('value')
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Arstotzka::KeyReader do
6
+ subject(:reader) { described_class.new hash, key, options }
7
+
8
+ let(:key) { 'key' }
9
+
10
+ describe '#read' do
11
+ context 'when no options are given' do
12
+ let(:options) { {} }
13
+
14
+ context 'when hash keys are symbols' do
15
+ let(:hash) { { key: 'value' } }
16
+
17
+ it 'reads the value' do
18
+ expect(reader.read).to eq('value')
19
+ end
20
+ end
21
+
22
+ context 'when hash keys are strings' do
23
+ let(:hash) { { 'key' => 'value' } }
24
+
25
+ it 'reads the value' do
26
+ expect(reader.read).to eq('value')
27
+ end
28
+ end
29
+
30
+ context 'when key is snake_case and hash keys are camelcase' do
31
+ let(:key) { 'the_key' }
32
+ let(:hash) { { theKey: 'value' } }
33
+
34
+ it 'reads the value' do
35
+ expect(reader.read).to eq('value')
36
+ end
37
+ end
38
+ end
39
+
40
+ context 'when case options are given' do
41
+ context 'when key is camelcase and hash keys are snake_case and case option is snake' do
42
+ let(:options) { { case: :snake } }
43
+ let(:key) { 'theKey' }
44
+ let(:hash) { { the_key: 'value' } }
45
+
46
+ it 'reads the value' do
47
+ expect(reader.read).to eq('value')
48
+ end
49
+ end
50
+
51
+ context 'when key is snakecase and hash keys are lower camelcase and case option is lower' do
52
+ let(:options) { { case: :lower_camel } }
53
+ let(:key) { 'the_key' }
54
+ let(:hash) { { theKey: 'value' } }
55
+
56
+ it 'reads the value' do
57
+ expect(reader.read).to eq('value')
58
+ end
59
+ end
60
+
61
+ context 'when key is snakecase and hash keys are upper camelcase and case option is upper' do
62
+ let(:options) { { case: :upper_camel } }
63
+ let(:key) { 'the_key' }
64
+ let(:hash) { { TheKey: 'value' } }
65
+
66
+ it 'reads the value' do
67
+ expect(reader.read).to eq('value')
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arstotzka
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darthjee
@@ -225,6 +225,7 @@ files:
225
225
  - lib/arstotzka/fetcher.rb
226
226
  - lib/arstotzka/fetcher_builder.rb
227
227
  - lib/arstotzka/hash_reader.rb
228
+ - lib/arstotzka/key_reader.rb
228
229
  - lib/arstotzka/method_builder.rb
229
230
  - lib/arstotzka/options.rb
230
231
  - lib/arstotzka/reader.rb
@@ -245,6 +246,7 @@ files:
245
246
  - spec/integration/yard/arstotzka/fetcher_builder_spec.rb
246
247
  - spec/integration/yard/arstotzka/fetcher_spec.rb
247
248
  - spec/integration/yard/arstotzka/hash_reader_spec.rb
249
+ - spec/integration/yard/arstotzka/key_reader_spec.rb
248
250
  - spec/integration/yard/arstotzka/method_builder_spec.rb
249
251
  - spec/integration/yard/arstotzka/options_spec.rb
250
252
  - spec/integration/yard/arstotzka/reader_spec.rb
@@ -255,6 +257,7 @@ files:
255
257
  - spec/lib/arstotzka/fetcher_builder_spec.rb
256
258
  - spec/lib/arstotzka/fetcher_spec.rb
257
259
  - spec/lib/arstotzka/hash_reader_spec.rb
260
+ - spec/lib/arstotzka/key_reader_spec.rb
258
261
  - spec/lib/arstotzka/method_builder_spec.rb
259
262
  - spec/lib/arstotzka/options_spec.rb
260
263
  - spec/lib/arstotzka/reader_spec.rb
@@ -330,6 +333,7 @@ test_files:
330
333
  - spec/integration/yard/arstotzka/fetcher_builder_spec.rb
331
334
  - spec/integration/yard/arstotzka/fetcher_spec.rb
332
335
  - spec/integration/yard/arstotzka/hash_reader_spec.rb
336
+ - spec/integration/yard/arstotzka/key_reader_spec.rb
333
337
  - spec/integration/yard/arstotzka/method_builder_spec.rb
334
338
  - spec/integration/yard/arstotzka/options_spec.rb
335
339
  - spec/integration/yard/arstotzka/reader_spec.rb
@@ -340,6 +344,7 @@ test_files:
340
344
  - spec/lib/arstotzka/fetcher_builder_spec.rb
341
345
  - spec/lib/arstotzka/fetcher_spec.rb
342
346
  - spec/lib/arstotzka/hash_reader_spec.rb
347
+ - spec/lib/arstotzka/key_reader_spec.rb
343
348
  - spec/lib/arstotzka/method_builder_spec.rb
344
349
  - spec/lib/arstotzka/options_spec.rb
345
350
  - spec/lib/arstotzka/reader_spec.rb