arstotzka 1.3.2 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +1 -1
- data/Dockerfile +23 -3
- data/README.md +47 -2
- data/arstotzka.gemspec +1 -1
- data/config/yardstick.yml +5 -0
- data/lib/arstotzka.rb +5 -0
- data/lib/arstotzka/base.rb +1 -1
- data/lib/arstotzka/class_methods.rb +1 -2
- data/lib/arstotzka/config.rb +70 -0
- data/lib/arstotzka/fetcher.rb +22 -7
- data/lib/arstotzka/fetcher/cache.rb +154 -0
- data/lib/arstotzka/fetcher_builder.rb +13 -15
- data/lib/arstotzka/key_reader.rb +0 -2
- data/lib/arstotzka/method_builder.rb +8 -27
- data/lib/arstotzka/options.rb +3 -21
- data/lib/arstotzka/version.rb +1 -1
- data/scripts/rubycritic.sh +1 -1
- data/spec/integration/readme/arstotzka/config_spec.rb +52 -0
- data/spec/integration/yard/arstotzka/config_spec.rb +40 -0
- data/spec/integration/yard/arstotzka/fetcher/cache_spec.rb +91 -0
- data/spec/integration/yard/arstotzka/fetcher_builder_spec.rb +11 -11
- data/spec/integration/yard/arstotzka/method_builder_spec.rb +9 -10
- data/spec/lib/arstotzka/config_spec.rb +225 -0
- data/spec/lib/arstotzka/fetcher/cache_spec.rb +154 -0
- data/spec/lib/arstotzka/fetcher_spec.rb +41 -1
- data/spec/lib/arstotzka/method_builder_spec.rb +1 -1
- data/spec/lib/arstotzka/options_spec.rb +1 -1
- data/spec/lib/arstotzka_spec.rb +163 -0
- data/spec/spec_helper.rb +1 -1
- data/spec/support/models/restaurant.rb +11 -0
- metadata +18 -4
@@ -7,7 +7,6 @@ describe Arstotzka::MethodBuilder do
|
|
7
7
|
subject(:builder) { described_class.new(attributes, klass, options) }
|
8
8
|
|
9
9
|
let!(:instance) { klass.new(hash) }
|
10
|
-
let(:options) { Arstotzka::Options.new(options_hash) }
|
11
10
|
let(:hash) do
|
12
11
|
{
|
13
12
|
'name' => { first: 'John', last: 'Williams' },
|
@@ -17,9 +16,9 @@ describe Arstotzka::MethodBuilder do
|
|
17
16
|
end
|
18
17
|
|
19
18
|
describe '#first_name' do
|
20
|
-
let(:klass)
|
21
|
-
let(:attributes)
|
22
|
-
let(:
|
19
|
+
let(:klass) { Class.new(MyModel) }
|
20
|
+
let(:attributes) { [:first_name] }
|
21
|
+
let(:options) { { full_path: 'name.first' } }
|
23
22
|
|
24
23
|
before do
|
25
24
|
builder.build
|
@@ -31,9 +30,9 @@ describe Arstotzka::MethodBuilder do
|
|
31
30
|
end
|
32
31
|
|
33
32
|
describe '#age' do
|
34
|
-
let(:klass)
|
35
|
-
let(:attributes)
|
36
|
-
let(:
|
33
|
+
let(:klass) { Class.new(MyModel) }
|
34
|
+
let(:attributes) { %i[age cars] }
|
35
|
+
let(:options) { { type: :integer } }
|
37
36
|
|
38
37
|
before do
|
39
38
|
builder.build
|
@@ -49,9 +48,9 @@ describe Arstotzka::MethodBuilder do
|
|
49
48
|
end
|
50
49
|
|
51
50
|
describe '#cars' do
|
52
|
-
let(:klass)
|
53
|
-
let(:attributes)
|
54
|
-
let(:
|
51
|
+
let(:klass) { Class.new(MyModel) }
|
52
|
+
let(:attributes) { %i[age cars] }
|
53
|
+
let(:options) { { type: :integer } }
|
55
54
|
|
56
55
|
before do
|
57
56
|
builder.build
|
@@ -0,0 +1,225 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Arstotzka::Config do
|
6
|
+
subject(:config) { Arstotzka.config }
|
7
|
+
|
8
|
+
after do
|
9
|
+
Arstotzka.reset_config
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#options' do
|
13
|
+
it do
|
14
|
+
expect(config.options).to be_a(Arstotzka::Options)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'generates options with default values' do
|
18
|
+
expect(config.options.to_h)
|
19
|
+
.to eq(described_class::DEFAULT_CONFIGS)
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'when configuring it' do
|
23
|
+
let(:expected_options_hash) do
|
24
|
+
{
|
25
|
+
after: :method,
|
26
|
+
after_each: :each_method,
|
27
|
+
cached: true,
|
28
|
+
case: :snake,
|
29
|
+
compact: true,
|
30
|
+
default: 10,
|
31
|
+
flatten: true,
|
32
|
+
json: :@hash,
|
33
|
+
klass: Account,
|
34
|
+
type: :string
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
before do
|
39
|
+
Arstotzka.configure do |c|
|
40
|
+
after :method
|
41
|
+
after_each :each_method
|
42
|
+
cached true
|
43
|
+
c.case :snake
|
44
|
+
compact true
|
45
|
+
default 10
|
46
|
+
flatten true
|
47
|
+
json :@hash
|
48
|
+
klass Account
|
49
|
+
type :string
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'generates options with given values' do
|
54
|
+
expect(config.options.to_h)
|
55
|
+
.to eq(expected_options_hash)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'when passing a hash' do
|
60
|
+
let(:options_hash) do
|
61
|
+
expected_options_hash
|
62
|
+
end
|
63
|
+
|
64
|
+
let(:expected_options_hash) do
|
65
|
+
{
|
66
|
+
after: :method,
|
67
|
+
after_each: :each_method,
|
68
|
+
cached: true,
|
69
|
+
case: :snake,
|
70
|
+
compact: true,
|
71
|
+
default: 10,
|
72
|
+
flatten: true,
|
73
|
+
json: :@hash,
|
74
|
+
klass: Account,
|
75
|
+
type: :string
|
76
|
+
}
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'generates options with given values' do
|
80
|
+
expect(config.options(options_hash).to_h)
|
81
|
+
.to eq(expected_options_hash)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe '#after' do
|
87
|
+
it do
|
88
|
+
expect(config.after).to be_falsey
|
89
|
+
end
|
90
|
+
|
91
|
+
context 'when configuring value' do
|
92
|
+
before { Arstotzka.configure { after :method } }
|
93
|
+
|
94
|
+
it 'returns configured value' do
|
95
|
+
expect(config.after).to eq(:method)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe '#after_each' do
|
101
|
+
it do
|
102
|
+
expect(config.after).to be_falsey
|
103
|
+
end
|
104
|
+
|
105
|
+
context 'when configuring value' do
|
106
|
+
before { Arstotzka.configure { after_each :each_method } }
|
107
|
+
|
108
|
+
it 'returns configured value' do
|
109
|
+
expect(config.after_each).to eq(:each_method)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe '#cached' do
|
115
|
+
it do
|
116
|
+
expect(config.cached).to be_falsey
|
117
|
+
end
|
118
|
+
|
119
|
+
context 'when configuring value' do
|
120
|
+
before { Arstotzka.configure { cached true } }
|
121
|
+
|
122
|
+
it 'returns configured value' do
|
123
|
+
expect(config.cached).to be_truthy
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe '#case' do
|
129
|
+
it 'returns default case' do
|
130
|
+
expect(config.case).to eq(:lower_camel)
|
131
|
+
end
|
132
|
+
|
133
|
+
context 'when configuring value' do
|
134
|
+
before { Arstotzka.configure { |c| c.case :snake } }
|
135
|
+
|
136
|
+
it 'returns configured value' do
|
137
|
+
expect(config.case).to eq(:snake)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
describe '#compact' do
|
143
|
+
it do
|
144
|
+
expect(config.compact).to be_falsey
|
145
|
+
end
|
146
|
+
|
147
|
+
context 'when configuring value' do
|
148
|
+
before { Arstotzka.configure { compact true } }
|
149
|
+
|
150
|
+
it 'returns configured value' do
|
151
|
+
expect(config.case).to be_truthy
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
describe '#default' do
|
157
|
+
it do
|
158
|
+
expect(config.default).to be_nil
|
159
|
+
end
|
160
|
+
|
161
|
+
context 'when configuring value' do
|
162
|
+
before { Arstotzka.configure { default 10 } }
|
163
|
+
|
164
|
+
it 'returns configured value' do
|
165
|
+
expect(config.default).to eq(10)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
describe '#flatten' do
|
171
|
+
it do
|
172
|
+
expect(config.flatten).to be_falsey
|
173
|
+
end
|
174
|
+
|
175
|
+
context 'when configuring value' do
|
176
|
+
before { Arstotzka.configure { flatten true } }
|
177
|
+
|
178
|
+
it 'returns configured value' do
|
179
|
+
expect(config.flatten).to be_truthy
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
describe '#json' do
|
185
|
+
it 'returns default json option' do
|
186
|
+
expect(config.json).to eq(:json)
|
187
|
+
end
|
188
|
+
|
189
|
+
context 'when configuring value' do
|
190
|
+
before { Arstotzka.configure { json :@hash } }
|
191
|
+
|
192
|
+
it 'returns configured value' do
|
193
|
+
expect(config.json).to eq(:@hash)
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
describe '#klass' do
|
199
|
+
it do
|
200
|
+
expect(config.klass).to be_nil
|
201
|
+
end
|
202
|
+
|
203
|
+
context 'when configuring value' do
|
204
|
+
before { Arstotzka.configure { klass Account } }
|
205
|
+
|
206
|
+
it 'returns configured value' do
|
207
|
+
expect(config.klass).to eq(Account)
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
describe '#type' do
|
213
|
+
it 'returns none' do
|
214
|
+
expect(config.type).to eq(:none)
|
215
|
+
end
|
216
|
+
|
217
|
+
context 'when configuring value' do
|
218
|
+
before { Arstotzka.configure { type :string } }
|
219
|
+
|
220
|
+
it 'returns configured value' do
|
221
|
+
expect(config.type).to eq(:string)
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
@@ -0,0 +1,154 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Arstotzka::Fetcher::Cache do
|
6
|
+
subject(:cache) { described_class.new(options) { 100 } }
|
7
|
+
|
8
|
+
let(:instance) { Object.new }
|
9
|
+
let(:value) { cache.fetch }
|
10
|
+
|
11
|
+
let(:options) do
|
12
|
+
Arstotzka.config.options(
|
13
|
+
options_hash.merge(instance: instance, key: :the_value)
|
14
|
+
)
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'when option cached is false' do
|
18
|
+
let(:options_hash) { { cached: false } }
|
19
|
+
|
20
|
+
context 'when a variable has not been set' do
|
21
|
+
it 'returns result of the block' do
|
22
|
+
expect(value).to eq(100)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'does not set the variable' do
|
26
|
+
expect { value }
|
27
|
+
.not_to(change { instance.instance_variable_get(:@the_value) })
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'when a variable has been set as nil' do
|
32
|
+
before do
|
33
|
+
instance.instance_variable_set('@the_value', nil)
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'returns result of the block' do
|
37
|
+
expect(value).to eq(100)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'does not set the variable' do
|
41
|
+
expect { value }
|
42
|
+
.not_to(change { instance.instance_variable_get(:@the_value) })
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'when a variable has been set with value' do
|
47
|
+
before do
|
48
|
+
instance.instance_variable_set('@the_value', :old_value)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'returns result of the block' do
|
52
|
+
expect(value).to eq(100)
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'does not set the variable' do
|
56
|
+
expect { value }
|
57
|
+
.not_to(change { instance.instance_variable_get(:@the_value) })
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'when option cached is true' do
|
63
|
+
let(:options_hash) { { cached: true } }
|
64
|
+
|
65
|
+
context 'when a variable has not been set' do
|
66
|
+
it 'returns result of the block' do
|
67
|
+
expect(value).to eq(100)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'does sets the variable' do
|
71
|
+
expect { value }
|
72
|
+
.to change { instance.instance_variable_get(:@the_value) }
|
73
|
+
.from(nil).to(100)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context 'when a variable has been set as nil' do
|
78
|
+
before do
|
79
|
+
instance.instance_variable_set('@the_value', nil)
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'returns result of the block' do
|
83
|
+
expect(value).to eq(100)
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'does sets the variable' do
|
87
|
+
expect { value }
|
88
|
+
.to change { instance.instance_variable_get(:@the_value) }
|
89
|
+
.from(nil).to(100)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context 'when a variable has been set with value' do
|
94
|
+
before do
|
95
|
+
instance.instance_variable_set('@the_value', :old_value)
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'returns the cached value' do
|
99
|
+
expect(value).to eq(:old_value)
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'does not update the variable' do
|
103
|
+
expect { value }
|
104
|
+
.not_to(change { instance.instance_variable_get(:@the_value) })
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context 'when option cached is :full' do
|
110
|
+
let(:options_hash) { { cached: :full } }
|
111
|
+
|
112
|
+
context 'when a variable has not been set' do
|
113
|
+
it 'returns result of the block' do
|
114
|
+
expect(value).to eq(100)
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'does sets the variable' do
|
118
|
+
expect { value }
|
119
|
+
.to change { instance.instance_variable_get(:@the_value) }
|
120
|
+
.from(nil).to(100)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
context 'when a variable has been set as nil' do
|
125
|
+
before do
|
126
|
+
instance.instance_variable_set('@the_value', nil)
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'returns nil' do
|
130
|
+
expect(value).to be_nil
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'does not update the variable' do
|
134
|
+
expect { value }
|
135
|
+
.not_to(change { instance.instance_variable_get(:@the_value) })
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context 'when a variable has been set with value' do
|
140
|
+
before do
|
141
|
+
instance.instance_variable_set('@the_value', :old_value)
|
142
|
+
end
|
143
|
+
|
144
|
+
it 'returns the cached value' do
|
145
|
+
expect(value).to eq(:old_value)
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'does not update the variable' do
|
149
|
+
expect { value }
|
150
|
+
.not_to(change { instance.instance_variable_get(:@the_value) })
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
@@ -7,7 +7,7 @@ describe Arstotzka::Fetcher do
|
|
7
7
|
described_class.new options
|
8
8
|
end
|
9
9
|
|
10
|
-
let(:options) { Arstotzka
|
10
|
+
let(:options) { Arstotzka.config.options(options_hash.merge(instance: instance)) }
|
11
11
|
let(:instance) { Arstotzka::Fetcher::Dummy.new(json) }
|
12
12
|
let(:json) { load_json_fixture_file('arstotzka.json') }
|
13
13
|
let(:value) { fetcher.fetch }
|
@@ -177,4 +177,44 @@ describe Arstotzka::Fetcher do
|
|
177
177
|
end
|
178
178
|
end
|
179
179
|
end
|
180
|
+
|
181
|
+
describe 'cached option' do
|
182
|
+
let(:key) { 'id' }
|
183
|
+
|
184
|
+
context 'with cached true' do
|
185
|
+
let(:options_hash) { { key: key, cached: true } }
|
186
|
+
|
187
|
+
it 'retrieves attribute from cache' do
|
188
|
+
expect { json['id'] = Random.rand(1000) }
|
189
|
+
.not_to change(fetcher, :fetch)
|
190
|
+
end
|
191
|
+
|
192
|
+
context 'when initial value is nil' do
|
193
|
+
let(:json) { { id: nil } }
|
194
|
+
|
195
|
+
it 'does not cache nil values' do
|
196
|
+
expect { json['id'] = Random.rand(1000) }
|
197
|
+
.to change(fetcher, :fetch)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
context 'with cached full' do
|
203
|
+
let(:options_hash) { { key: key, cached: :full } }
|
204
|
+
|
205
|
+
it 'retrieves attribute from cache' do
|
206
|
+
expect { json['id'] = Random.rand(1000) }
|
207
|
+
.not_to change(fetcher, :fetch)
|
208
|
+
end
|
209
|
+
|
210
|
+
context 'when initial value is nil' do
|
211
|
+
let(:json) { { id: nil } }
|
212
|
+
|
213
|
+
it 'caches nil values' do
|
214
|
+
expect { json['id'] = Random.rand(1000) }
|
215
|
+
.not_to change(fetcher, :fetch)
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
180
220
|
end
|