arstotzka 1.3.0 → 1.3.1
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 +4 -1
- data/.gitignore +1 -0
- data/Dockerfile +1 -1
- data/README.md +1 -1
- data/Rakefile +1 -0
- data/arstotzka.gemspec +1 -0
- data/config/rubycritc.rb +12 -0
- data/config/yardstick.yml +8 -6
- data/lib/arstotzka.rb +1 -0
- data/lib/arstotzka/class_methods.rb +36 -4
- data/lib/arstotzka/crawler.rb +13 -5
- data/lib/arstotzka/exception.rb +28 -0
- data/lib/arstotzka/fetcher.rb +30 -22
- data/lib/arstotzka/hash_reader.rb +80 -0
- data/lib/arstotzka/method_builder.rb +1 -1
- data/lib/arstotzka/options.rb +13 -0
- data/lib/arstotzka/version.rb +1 -1
- data/lib/arstotzka/wrapper.rb +4 -4
- data/scripts/rubycritic.sh +10 -0
- data/spec/integration/yard/arstotzka/hash_reader_spec.rb +41 -0
- data/spec/integration/yard/arstotzka/method_builder_spec.rb +4 -4
- data/spec/lib/arstotzka/hash_reader_spec.rb +43 -0
- data/spec/lib/arstotzka_spec.rb +70 -0
- data/spec/support/models/arstotzka/dummy.rb +6 -1
- metadata +23 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a4b941b1b173ae1f57c979ab7b58771a675dc297184cecb936c4e3e5854de5c
|
4
|
+
data.tar.gz: 4dcc0ad6c185c423aa7cb9376ebc47f392a9fb7eccb5753f0448817a1337dc10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55402898468c8a5f593abfba401cf36a76e08771b4b98c6d1240af8224f54d141e13f6c19033befee4a9244289bee3b2aebf0f682a874090162c7fb529aaf63b
|
7
|
+
data.tar.gz: 064e5a70a830ea70ff8d1dc544f36f81ebd88c8ce4df77efad625a1e4d9d67d8b5245b5dc8f851c9fc7ccd8dff5ef7eb9a83ad702556a494ed7084fdae08515f
|
data/.circleci/config.yml
CHANGED
@@ -2,7 +2,7 @@ version: 2
|
|
2
2
|
jobs:
|
3
3
|
build:
|
4
4
|
docker:
|
5
|
-
- image: darthjee/circleci_ruby_gems:0.0.
|
5
|
+
- image: darthjee/circleci_ruby_gems:0.0.2
|
6
6
|
steps:
|
7
7
|
- checkout
|
8
8
|
- run:
|
@@ -26,3 +26,6 @@ jobs:
|
|
26
26
|
- run:
|
27
27
|
name: Check version documentation
|
28
28
|
command: scripts/check_readme.sh
|
29
|
+
- run:
|
30
|
+
name: Rubycritcs check
|
31
|
+
command: scripts/rubycritic.sh
|
data/.gitignore
CHANGED
data/Dockerfile
CHANGED
data/README.md
CHANGED
data/Rakefile
CHANGED
data/arstotzka.gemspec
CHANGED
@@ -28,6 +28,7 @@ Gem::Specification.new do |gem|
|
|
28
28
|
gem.add_development_dependency 'rspec', '>= 3.8'
|
29
29
|
gem.add_development_dependency 'rubocop', '0.58.1'
|
30
30
|
gem.add_development_dependency 'rubocop-rspec', '1.30.0'
|
31
|
+
gem.add_development_dependency 'rubycritic', '>= 4.0.2'
|
31
32
|
gem.add_development_dependency 'simplecov', '~> 0.16.x'
|
32
33
|
gem.add_development_dependency 'yard', '>= 0.9.18'
|
33
34
|
gem.add_development_dependency 'yardstick', '>= 0.9.9'
|
data/config/rubycritc.rb
ADDED
data/config/yardstick.yml
CHANGED
@@ -19,28 +19,30 @@ rules:
|
|
19
19
|
ReturnTag:
|
20
20
|
enabled: true
|
21
21
|
exclude:
|
22
|
-
- Arstotzka::Crawler#
|
22
|
+
- Arstotzka::Crawler#block
|
23
23
|
- Arstotzka::Crawler#options
|
24
|
-
- Arstotzka::
|
24
|
+
- Arstotzka::Exception::FetcherBuilderNotFound#attribute
|
25
|
+
- Arstotzka::Exception::FetcherBuilderNotFound#klass
|
25
26
|
- Arstotzka::Fetcher#options
|
26
27
|
- Arstotzka::FetcherBuilder#options
|
28
|
+
- Arstotzka::HashReader#options
|
27
29
|
- Arstotzka::MethodBuilder#attr_names
|
28
30
|
- Arstotzka::MethodBuilder#options
|
29
|
-
- Arstotzka::Options#keys
|
30
31
|
- Arstotzka::Reader#keys
|
31
32
|
- Arstotzka::Reader#options
|
32
33
|
- Arstotzka::Wrapper#options
|
33
34
|
Summary::Presence:
|
34
35
|
enabled: true
|
35
36
|
exclude:
|
36
|
-
- Arstotzka::Crawler#
|
37
|
+
- Arstotzka::Crawler#block
|
37
38
|
- Arstotzka::Crawler#options
|
38
|
-
- Arstotzka::
|
39
|
+
- Arstotzka::Exception::FetcherBuilderNotFound#attribute
|
40
|
+
- Arstotzka::Exception::FetcherBuilderNotFound#klass
|
39
41
|
- Arstotzka::Fetcher#options
|
40
42
|
- Arstotzka::FetcherBuilder#options
|
43
|
+
- Arstotzka::HashReader#options
|
41
44
|
- Arstotzka::MethodBuilder#attr_names
|
42
45
|
- Arstotzka::MethodBuilder#options
|
43
|
-
- Arstotzka::Options#keys
|
44
46
|
- Arstotzka::Reader#keys
|
45
47
|
- Arstotzka::Reader#options
|
46
48
|
- Arstotzka::Wrapper#options
|
data/lib/arstotzka.rb
CHANGED
@@ -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 :HashReader, 'arstotzka/hash_reader'
|
175
176
|
autoload :MethodBuilder, 'arstotzka/method_builder'
|
176
177
|
autoload :Options, 'arstotzka/options'
|
177
178
|
autoload :Reader, 'arstotzka/reader'
|
@@ -12,8 +12,8 @@ module Arstotzka
|
|
12
12
|
# @param options [Arstotzka::Options] fetcher options
|
13
13
|
#
|
14
14
|
# @return [Artotzka::FetcherBuilder]
|
15
|
-
def add_fetcher(attribute, options)
|
16
|
-
fetcher_builders[attribute
|
15
|
+
def add_fetcher(attribute, options = {})
|
16
|
+
fetcher_builders[attribute] = FetcherBuilder.new(options.merge(key: attribute))
|
17
17
|
end
|
18
18
|
|
19
19
|
# @api private
|
@@ -27,7 +27,39 @@ module Arstotzka
|
|
27
27
|
#
|
28
28
|
# @return [Arstotzka::Fetcher]
|
29
29
|
def fetcher_for(attribute, instance)
|
30
|
-
|
30
|
+
return builder_for(attribute).build(instance) if fetcher_for?(attribute)
|
31
|
+
|
32
|
+
raise Exception::FetcherBuilderNotFound.new(attribute, self)
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
# @api private
|
38
|
+
#
|
39
|
+
# Checks if class can build a fetcher for attribute
|
40
|
+
#
|
41
|
+
# @param attribute [::Symbol]
|
42
|
+
#
|
43
|
+
# @return [TrueClass,FalseClass]
|
44
|
+
def fetcher_for?(attribute)
|
45
|
+
return true if fetcher_builders.key?(attribute)
|
46
|
+
return unless superclass.include?(Arstotzka)
|
47
|
+
|
48
|
+
superclass.fetcher_for?(attribute)
|
49
|
+
end
|
50
|
+
|
51
|
+
# @api private
|
52
|
+
#
|
53
|
+
# Returns fetcher builder for an attribute
|
54
|
+
#
|
55
|
+
# @param attribute [::Symbol]
|
56
|
+
#
|
57
|
+
# @return [Arstotzka::FetcherBuilder]
|
58
|
+
def builder_for(attribute)
|
59
|
+
builder = fetcher_builders[attribute]
|
60
|
+
return superclass.builder_for(attribute) unless builder
|
61
|
+
|
62
|
+
builder
|
31
63
|
end
|
32
64
|
|
33
65
|
private
|
@@ -69,7 +101,7 @@ module Arstotzka
|
|
69
101
|
# ActiveSupport::Concern
|
70
102
|
def expose(*attr_names, **options_hash)
|
71
103
|
options = Options.new(options_hash.symbolize_keys)
|
72
|
-
MethodBuilder.new(attr_names, self, options).build
|
104
|
+
MethodBuilder.new(attr_names.map(&:to_sym), self, options).build
|
73
105
|
end
|
74
106
|
|
75
107
|
# @private
|
data/lib/arstotzka/crawler.rb
CHANGED
@@ -36,8 +36,7 @@ module Arstotzka
|
|
36
36
|
# @param options [Arstotzka::Options] options of initialization object
|
37
37
|
def initialize(options = {}, &block)
|
38
38
|
self.options = options
|
39
|
-
|
40
|
-
@post_process = block || proc { |value| value }
|
39
|
+
@block = block
|
41
40
|
end
|
42
41
|
|
43
42
|
# Crawls into the hash looking for all keys in the given keys
|
@@ -94,7 +93,7 @@ module Arstotzka
|
|
94
93
|
private
|
95
94
|
|
96
95
|
# @private
|
97
|
-
attr_reader :
|
96
|
+
attr_reader :block, :options
|
98
97
|
delegate :compact, :default, to: :options
|
99
98
|
|
100
99
|
# Fetch the value from hash by crawling the keys
|
@@ -150,6 +149,15 @@ module Arstotzka
|
|
150
149
|
post_process.call(value)
|
151
150
|
end
|
152
151
|
|
152
|
+
# @private
|
153
|
+
#
|
154
|
+
# Return post processor responsible for processing final result
|
155
|
+
#
|
156
|
+
# @return [Proc]
|
157
|
+
def post_process
|
158
|
+
@post_process ||= block || proc { |value| value }
|
159
|
+
end
|
160
|
+
|
153
161
|
# @private
|
154
162
|
#
|
155
163
|
# Iterate over array applying #crawl over each element
|
@@ -160,8 +168,8 @@ module Arstotzka
|
|
160
168
|
# @return [Array] the new array with the individual values returned
|
161
169
|
# @see #crawl
|
162
170
|
def crawl_array(array, index)
|
163
|
-
array.map { |
|
164
|
-
|
171
|
+
array.map { |element| value(element, index) }.tap do |new_array|
|
172
|
+
new_array.compact! if compact
|
165
173
|
end
|
166
174
|
end
|
167
175
|
end
|
data/lib/arstotzka/exception.rb
CHANGED
@@ -1,8 +1,36 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Arstotzka
|
4
|
+
# @api private
|
5
|
+
#
|
6
|
+
# Arstotzka exceptions
|
4
7
|
module Exception
|
5
8
|
# Exception raised when a key in the hash is not found
|
6
9
|
class KeyNotFound < StandardError; end
|
10
|
+
|
11
|
+
# Exception raised when configuration for FetcherBuilder is not found
|
12
|
+
class FetcherBuilderNotFound < StandardError
|
13
|
+
# Returns a new instance of FetcherBuilderNotFound
|
14
|
+
#
|
15
|
+
# @param attribute [Symbol] attribute's name
|
16
|
+
# @param klass [Class] Class where the fetcher was being accessed
|
17
|
+
#
|
18
|
+
# @return [FetcherBuilderNotFound]
|
19
|
+
def initialize(attribute, klass)
|
20
|
+
@attribute = attribute
|
21
|
+
@klass = klass
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns message specifying which attemp created the failure
|
25
|
+
#
|
26
|
+
# @return [String]
|
27
|
+
def message
|
28
|
+
"FetcherBuild not found for #{attribute} on #{klass}"
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
attr_reader :attribute, :klass
|
34
|
+
end
|
7
35
|
end
|
8
36
|
end
|
data/lib/arstotzka/fetcher.rb
CHANGED
@@ -11,10 +11,12 @@ module Arstotzka
|
|
11
11
|
# Creates an instance of Artotzka::Fetcher
|
12
12
|
#
|
13
13
|
# @overload iniitalize(options_hash = {})
|
14
|
-
# @param options_hash [Hash] options for {Crawler}, {Wrapper}
|
14
|
+
# @param options_hash [Hash] options for {Crawler}, {Wrapper},
|
15
|
+
# {Reader} and {HashReader}
|
15
16
|
#
|
16
17
|
# @overload iniitalize(options)
|
17
|
-
# @param options [Arstotzka::Options] options for {Crawler}, {Wrapper}
|
18
|
+
# @param options [Arstotzka::Options] options for {Crawler}, {Wrapper},
|
19
|
+
# {Reader} and {HashReader}
|
18
20
|
def initialize(options_hash = {})
|
19
21
|
self.options = options_hash
|
20
22
|
end
|
@@ -81,35 +83,32 @@ module Arstotzka
|
|
81
83
|
# # ]
|
82
84
|
def fetch
|
83
85
|
value = crawler.value(hash)
|
84
|
-
value.flatten! if flatten && value.
|
86
|
+
value.flatten! if flatten && value.is_a?(Array)
|
85
87
|
value = instance.send(after, value) if after
|
86
88
|
value
|
87
89
|
end
|
88
90
|
|
91
|
+
# Checks if other equals self
|
92
|
+
#
|
93
|
+
# @param other [Object]
|
94
|
+
#
|
95
|
+
# @return [TrueClass,FalseClass]
|
96
|
+
def ==(other)
|
97
|
+
return false unless other.class == self.class
|
98
|
+
options == other.options &&
|
99
|
+
instance == other.instance
|
100
|
+
end
|
101
|
+
|
102
|
+
protected
|
103
|
+
|
104
|
+
attr_reader :options
|
105
|
+
|
89
106
|
private
|
90
107
|
|
91
108
|
# @private
|
92
|
-
attr_reader :instance, :options
|
93
109
|
delegate :instance, :after, :flatten, to: :options
|
94
110
|
delegate :wrap, to: :wrapper
|
95
|
-
|
96
|
-
# @private
|
97
|
-
#
|
98
|
-
# Retrieves the hash to be crawled from the instance
|
99
|
-
#
|
100
|
-
# @return [Hash]
|
101
|
-
# rubocop:disable Metrics/AbcSize
|
102
|
-
def hash
|
103
|
-
@hash ||= case options.json.to_s
|
104
|
-
when /^@@.*/
|
105
|
-
instance.class.class_variable_get(options.json)
|
106
|
-
when /^@.*/
|
107
|
-
then instance.instance_variable_get(options.json)
|
108
|
-
else
|
109
|
-
instance.send(options.json.to_s)
|
110
|
-
end
|
111
|
-
end
|
112
|
-
# rubocop:enable Metrics/AbcSize
|
111
|
+
delegate :hash, to: :hash_reader
|
113
112
|
|
114
113
|
# @private
|
115
114
|
#
|
@@ -134,5 +133,14 @@ module Arstotzka
|
|
134
133
|
def wrapper
|
135
134
|
@wrapper ||= Wrapper.new(options.merge(instance: instance))
|
136
135
|
end
|
136
|
+
|
137
|
+
# @api private
|
138
|
+
#
|
139
|
+
# Reader responsible for fetching hash from instance
|
140
|
+
#
|
141
|
+
# @return [Arstotzka::HashReader]
|
142
|
+
def hash_reader
|
143
|
+
@hash_reader ||= HashReader.new(options)
|
144
|
+
end
|
137
145
|
end
|
138
146
|
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Arstotzka
|
4
|
+
# @api private
|
5
|
+
#
|
6
|
+
# Class responsible for reading json / hash from instance
|
7
|
+
class HashReader
|
8
|
+
include Base
|
9
|
+
|
10
|
+
# Returns a new instance of HashReader
|
11
|
+
#
|
12
|
+
# @overload iniitalize(options_hash = {})
|
13
|
+
# @param options_hash [Hash]
|
14
|
+
#
|
15
|
+
# @overload iniitalize(options)
|
16
|
+
# @param options [Arstotzka::Options]
|
17
|
+
def initialize(options_hash = {})
|
18
|
+
self.options = options_hash
|
19
|
+
end
|
20
|
+
|
21
|
+
# Retrieves the hash to be crawled from the instance
|
22
|
+
#
|
23
|
+
# @return [Hash]
|
24
|
+
#
|
25
|
+
# @example Simple Usage
|
26
|
+
# class Dummy
|
27
|
+
# def initialize(json = {})
|
28
|
+
# @json = json
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# private
|
32
|
+
#
|
33
|
+
# attr_reader :json
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# hash = { key: 'value' }
|
37
|
+
# instance = Dummy.new(hash)
|
38
|
+
#
|
39
|
+
# reader = Arstotzka::HashReader.new(
|
40
|
+
# instance: instance
|
41
|
+
# )
|
42
|
+
#
|
43
|
+
# reader.hash # returns { key: 'value' }
|
44
|
+
#
|
45
|
+
# @example When fetching from class variable
|
46
|
+
# class ClassVariable
|
47
|
+
# def self.json=(json)
|
48
|
+
# @@json = json
|
49
|
+
# end
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# hash = { key: 'value' }
|
53
|
+
# ClassVariable.json = hash
|
54
|
+
#
|
55
|
+
# instance = ClassVariable.new
|
56
|
+
#
|
57
|
+
# reader = Arstotzka::HashReader.new(
|
58
|
+
# instance: instance, json: :@@json
|
59
|
+
# )
|
60
|
+
#
|
61
|
+
# reader.hash # returns { key: 'value' }
|
62
|
+
def hash
|
63
|
+
@hash ||= case json.to_s
|
64
|
+
when /^@@.*/
|
65
|
+
instance.class.class_variable_get(json)
|
66
|
+
when /^@.*/
|
67
|
+
then instance.instance_variable_get(json)
|
68
|
+
else
|
69
|
+
instance.send(json)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
attr_reader :options
|
76
|
+
|
77
|
+
delegate :instance, to: :options
|
78
|
+
delegate :json, to: :options
|
79
|
+
end
|
80
|
+
end
|
@@ -41,7 +41,7 @@ module Arstotzka
|
|
41
41
|
include Base
|
42
42
|
# Returns new instance of Arstotzka::MethodBuilder
|
43
43
|
#
|
44
|
-
# @param attr_names [Array] list of attributes to be fetched from the hash/json
|
44
|
+
# @param attr_names [Array<Symbol>] list of attributes to be fetched from the hash/json
|
45
45
|
# @param klass [Class] class to receive the methods
|
46
46
|
# (using {https://www.rubydoc.info/gems/sinclair Sinclair})
|
47
47
|
#
|
data/lib/arstotzka/options.rb
CHANGED
@@ -316,11 +316,24 @@ module Arstotzka
|
|
316
316
|
self.class.new(to_h.merge(options))
|
317
317
|
end
|
318
318
|
|
319
|
+
# Retuns all keys used when fetching
|
320
|
+
#
|
321
|
+
# @return [Array<String>]
|
319
322
|
def keys
|
320
323
|
return full_path.split('.') if full_path
|
321
324
|
return [key.to_s] unless path&.present?
|
322
325
|
|
323
326
|
[path, key].compact.join('.').split('.')
|
324
327
|
end
|
328
|
+
|
329
|
+
# Checks if another instance equals self
|
330
|
+
#
|
331
|
+
# @param other [Object]
|
332
|
+
#
|
333
|
+
# @return [TrueClass,FalseClass]
|
334
|
+
def ==(other)
|
335
|
+
return false unless other.class == self.class
|
336
|
+
to_h == other.to_h
|
337
|
+
end
|
325
338
|
end
|
326
339
|
end
|
data/lib/arstotzka/version.rb
CHANGED
data/lib/arstotzka/wrapper.rb
CHANGED
@@ -66,7 +66,7 @@ module Arstotzka
|
|
66
66
|
|
67
67
|
# @private
|
68
68
|
attr_reader :options
|
69
|
-
delegate :klass, :type, to: :options
|
69
|
+
delegate :klass, :type, :after_each, to: :options
|
70
70
|
|
71
71
|
# @private
|
72
72
|
#
|
@@ -90,7 +90,7 @@ module Arstotzka
|
|
90
90
|
#
|
91
91
|
# @return [Arra]
|
92
92
|
def wrap_array(array)
|
93
|
-
array.map { |
|
93
|
+
array.map { |element| wrap element }
|
94
94
|
end
|
95
95
|
|
96
96
|
# @private
|
@@ -132,9 +132,9 @@ module Arstotzka
|
|
132
132
|
#
|
133
133
|
# @return [Object] result of method call
|
134
134
|
def after(value)
|
135
|
-
return value unless
|
135
|
+
return value unless after_each
|
136
136
|
|
137
|
-
options.instance.send(
|
137
|
+
options.instance.send(after_each, value)
|
138
138
|
end
|
139
139
|
end
|
140
140
|
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
|
3
|
+
DIFF_LIST=$(git diff --name-only $CIRCLE_SHA1 $(git merge-base $CIRCLE_SHA1 origin/master) | grep "^lib/")
|
4
|
+
|
5
|
+
if [ ! -z "$DIFF_LIST" ]; then
|
6
|
+
mkdir -p tmp/rubycritic/compare
|
7
|
+
bundle exec rubycritic --format console --branch origin/master -t 0 $DIFF_LIST
|
8
|
+
else
|
9
|
+
echo "No changes detected. Skipping rubycritic..."
|
10
|
+
fi
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Arstotzka::HashReader do
|
6
|
+
subject(:reader) { described_class.new(options) }
|
7
|
+
|
8
|
+
describe '#hash' do
|
9
|
+
let(:klass) { Arstotzka::Fetcher::Dummy }
|
10
|
+
let(:json) { { key: 'value' } }
|
11
|
+
let(:instance) { klass.new(json) }
|
12
|
+
let(:options) { { instance: instance } }
|
13
|
+
|
14
|
+
context 'when no extra options are given' do
|
15
|
+
it 'reads hash from instance json method' do
|
16
|
+
expect(reader.hash).to eq(json)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when fetching from instance variable' do
|
21
|
+
let(:klass) { Arstotzka::Fetcher::NoAccessor }
|
22
|
+
let(:options) { { instance: instance, json: :@hash } }
|
23
|
+
|
24
|
+
it 'reads hash from instance variable' do
|
25
|
+
expect(reader.hash).to eq(json)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'when fetching from class variable' do
|
30
|
+
let(:klass) { Arstotzka::Fetcher::ClassVariable }
|
31
|
+
let(:options) { { instance: instance, json: :@@json } }
|
32
|
+
let(:instance) { klass.new }
|
33
|
+
|
34
|
+
before { klass.json = json }
|
35
|
+
|
36
|
+
it 'reads hash from class variable' do
|
37
|
+
expect(reader.hash).to eq(json)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -31,8 +31,8 @@ describe Arstotzka::MethodBuilder do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
describe '#age' do
|
34
|
-
let(:klass)
|
35
|
-
let(:attributes) { [
|
34
|
+
let(:klass) { Class.new(MyModel) }
|
35
|
+
let(:attributes) { %i[age cars] }
|
36
36
|
let(:options_hash) { { type: :integer } }
|
37
37
|
|
38
38
|
before do
|
@@ -49,8 +49,8 @@ describe Arstotzka::MethodBuilder do
|
|
49
49
|
end
|
50
50
|
|
51
51
|
describe '#cars' do
|
52
|
-
let(:klass)
|
53
|
-
let(:attributes) { [
|
52
|
+
let(:klass) { Class.new(MyModel) }
|
53
|
+
let(:attributes) { %i[age cars] }
|
54
54
|
let(:options_hash) { { type: :integer } }
|
55
55
|
|
56
56
|
before do
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Arstotzka::HashReader do
|
6
|
+
subject(:reader) { described_class.new(options) }
|
7
|
+
|
8
|
+
describe '#hash' do
|
9
|
+
let(:klass) { Arstotzka::Fetcher::Dummy }
|
10
|
+
let(:json) { { key: SecureRandom.hex } }
|
11
|
+
let(:instance) { klass.new(json) }
|
12
|
+
let(:options) { { instance: instance } }
|
13
|
+
|
14
|
+
context 'when no extra options are given' do
|
15
|
+
it 'reads hash from instance json method' do
|
16
|
+
expect(reader.hash).to eq(json)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when fetching from instance variable' do
|
21
|
+
let(:klass) { Arstotzka::Fetcher::NoAccessor }
|
22
|
+
let(:options) { { instance: instance, json: :@hash } }
|
23
|
+
|
24
|
+
it 'reads hash from instance variable' do
|
25
|
+
expect(reader.hash).to eq(json)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'when fetching from class variable' do
|
30
|
+
let(:klass) { Arstotzka::Fetcher::ClassVariable }
|
31
|
+
let(:options) { { instance: instance, json: :@@json } }
|
32
|
+
let(:instance) { klass.new }
|
33
|
+
|
34
|
+
before do
|
35
|
+
klass.json = json
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'reads hash from class variable' do
|
39
|
+
expect(reader.hash).to eq(json)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/spec/lib/arstotzka_spec.rb
CHANGED
@@ -8,6 +8,67 @@ describe Arstotzka do
|
|
8
8
|
let(:json) { load_json_fixture_file('arstotzka.json') }
|
9
9
|
let(:value) { dummy.public_send(attribute) }
|
10
10
|
|
11
|
+
describe '.add_fether' do
|
12
|
+
let(:klass) { Class.new(Arstotzka::Dummy) }
|
13
|
+
|
14
|
+
it do
|
15
|
+
expect { klass.add_fetcher(:new_attribute) }
|
16
|
+
.to change { klass.send(:fetcher_builders).keys }
|
17
|
+
.to([:new_attribute])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '.fetcher_for' do
|
22
|
+
let(:klass) { Arstotzka::Dummy }
|
23
|
+
let(:instance) { klass.new({}) }
|
24
|
+
let(:expected) do
|
25
|
+
Arstotzka::Fetcher.new(
|
26
|
+
key: :name,
|
27
|
+
path: 'user',
|
28
|
+
instance: instance
|
29
|
+
)
|
30
|
+
end
|
31
|
+
|
32
|
+
it do
|
33
|
+
expect(klass.fetcher_for(:name, instance))
|
34
|
+
.to be_a(Arstotzka::Fetcher)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'returns correct fetcher' do
|
38
|
+
expect(klass.fetcher_for(:name, instance))
|
39
|
+
.to eq(expected)
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'when fetcher was never added' do
|
43
|
+
it do
|
44
|
+
expect { klass.fetcher_for(:new_attribute, instance) }
|
45
|
+
.to raise_error(
|
46
|
+
Arstotzka::Exception::FetcherBuilderNotFound,
|
47
|
+
"FetcherBuild not found for new_attribute on #{klass}"
|
48
|
+
)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'when dealing with subclass' do
|
53
|
+
let(:klass) { Class.new(Arstotzka::Dummy) }
|
54
|
+
|
55
|
+
it do
|
56
|
+
expect { klass.fetcher_for(:name, instance) }
|
57
|
+
.not_to raise_error
|
58
|
+
end
|
59
|
+
|
60
|
+
context 'when fetcher was never added' do
|
61
|
+
it do
|
62
|
+
expect { klass.fetcher_for(:new_attribute, instance) }
|
63
|
+
.to raise_error(
|
64
|
+
Arstotzka::Exception::FetcherBuilderNotFound,
|
65
|
+
"FetcherBuild not found for new_attribute on #{klass}"
|
66
|
+
)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
11
72
|
context 'when parser is configured with no options' do
|
12
73
|
let(:attribute) { :id }
|
13
74
|
|
@@ -130,4 +191,13 @@ describe Arstotzka do
|
|
130
191
|
expect(value).to be_a(Float)
|
131
192
|
end
|
132
193
|
end
|
194
|
+
|
195
|
+
context 'when class is a child' do
|
196
|
+
let(:klass) { Class.new(Arstotzka::Dummy) }
|
197
|
+
let(:attribute) { :name }
|
198
|
+
|
199
|
+
it 'retrieves attribute from base json' do
|
200
|
+
expect(value).to eq(json['user']['name'])
|
201
|
+
end
|
202
|
+
end
|
133
203
|
end
|
@@ -5,7 +5,7 @@ module Arstotzka
|
|
5
5
|
include Arstotzka
|
6
6
|
attr_reader :json
|
7
7
|
|
8
|
-
expose
|
8
|
+
expose 'id'
|
9
9
|
expose :name, path: 'user'
|
10
10
|
expose :father_name, full_path: 'father.name'
|
11
11
|
expose :age, cached: true
|
@@ -23,5 +23,10 @@ module Arstotzka
|
|
23
23
|
g.publisher == 'sega'
|
24
24
|
end
|
25
25
|
end
|
26
|
+
|
27
|
+
def ==(other)
|
28
|
+
return false unless other.class == self.class
|
29
|
+
json == other.json
|
30
|
+
end
|
26
31
|
end
|
27
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arstotzka
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Darthjee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-05-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -122,6 +122,20 @@ dependencies:
|
|
122
122
|
- - '='
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: 1.30.0
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rubycritic
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 4.0.2
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 4.0.2
|
125
139
|
- !ruby/object:Gem::Dependency
|
126
140
|
name: simplecov
|
127
141
|
requirement: !ruby/object:Gem::Requirement
|
@@ -199,6 +213,7 @@ files:
|
|
199
213
|
- Rakefile
|
200
214
|
- arstotzka.gemspec
|
201
215
|
- arstotzka.jpg
|
216
|
+
- config/rubycritc.rb
|
202
217
|
- config/yardstick.rb
|
203
218
|
- config/yardstick.yml
|
204
219
|
- docker-compose.yml
|
@@ -209,6 +224,7 @@ files:
|
|
209
224
|
- lib/arstotzka/exception.rb
|
210
225
|
- lib/arstotzka/fetcher.rb
|
211
226
|
- lib/arstotzka/fetcher_builder.rb
|
227
|
+
- lib/arstotzka/hash_reader.rb
|
212
228
|
- lib/arstotzka/method_builder.rb
|
213
229
|
- lib/arstotzka/options.rb
|
214
230
|
- lib/arstotzka/reader.rb
|
@@ -216,6 +232,7 @@ files:
|
|
216
232
|
- lib/arstotzka/version.rb
|
217
233
|
- lib/arstotzka/wrapper.rb
|
218
234
|
- scripts/check_readme.sh
|
235
|
+
- scripts/rubycritic.sh
|
219
236
|
- spec/fixtures/accounts.json
|
220
237
|
- spec/fixtures/accounts_missing.json
|
221
238
|
- spec/fixtures/arstotzka.json
|
@@ -227,6 +244,7 @@ files:
|
|
227
244
|
- spec/integration/yard/arstotzka/crawler_spec.rb
|
228
245
|
- spec/integration/yard/arstotzka/fetcher_builder_spec.rb
|
229
246
|
- spec/integration/yard/arstotzka/fetcher_spec.rb
|
247
|
+
- spec/integration/yard/arstotzka/hash_reader_spec.rb
|
230
248
|
- spec/integration/yard/arstotzka/method_builder_spec.rb
|
231
249
|
- spec/integration/yard/arstotzka/options_spec.rb
|
232
250
|
- spec/integration/yard/arstotzka/reader_spec.rb
|
@@ -236,6 +254,7 @@ files:
|
|
236
254
|
- spec/lib/arstotzka/crawler_spec.rb
|
237
255
|
- spec/lib/arstotzka/fetcher_builder_spec.rb
|
238
256
|
- spec/lib/arstotzka/fetcher_spec.rb
|
257
|
+
- spec/lib/arstotzka/hash_reader_spec.rb
|
239
258
|
- spec/lib/arstotzka/method_builder_spec.rb
|
240
259
|
- spec/lib/arstotzka/options_spec.rb
|
241
260
|
- spec/lib/arstotzka/reader_spec.rb
|
@@ -310,6 +329,7 @@ test_files:
|
|
310
329
|
- spec/integration/yard/arstotzka/crawler_spec.rb
|
311
330
|
- spec/integration/yard/arstotzka/fetcher_builder_spec.rb
|
312
331
|
- spec/integration/yard/arstotzka/fetcher_spec.rb
|
332
|
+
- spec/integration/yard/arstotzka/hash_reader_spec.rb
|
313
333
|
- spec/integration/yard/arstotzka/method_builder_spec.rb
|
314
334
|
- spec/integration/yard/arstotzka/options_spec.rb
|
315
335
|
- spec/integration/yard/arstotzka/reader_spec.rb
|
@@ -319,6 +339,7 @@ test_files:
|
|
319
339
|
- spec/lib/arstotzka/crawler_spec.rb
|
320
340
|
- spec/lib/arstotzka/fetcher_builder_spec.rb
|
321
341
|
- spec/lib/arstotzka/fetcher_spec.rb
|
342
|
+
- spec/lib/arstotzka/hash_reader_spec.rb
|
322
343
|
- spec/lib/arstotzka/method_builder_spec.rb
|
323
344
|
- spec/lib/arstotzka/options_spec.rb
|
324
345
|
- spec/lib/arstotzka/reader_spec.rb
|