arstotzka 1.5.0 → 1.6.0

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: 3821d878e7f40912c328b3bfb6529d718264f6e3f6067087ac28e91e4e7e30b8
4
- data.tar.gz: 2f027fe2670701cd6724e117acfccc576fac5ab15080cf3d9ed583009dcdf34c
3
+ metadata.gz: 33a840eef86f6e4e433a4ecfc5a1744b29c6c2e5a632d9b700e907d45ac2b9cf
4
+ data.tar.gz: cd4ca82455ddd2b49209becdb97380658852214c70f10de0bb13063cdb37c8b1
5
5
  SHA512:
6
- metadata.gz: 2df223a18047011324b177a1abee1f965badf8b898237f7c07964cf703425241b7f40636ed29e21c5a13b2a90e0cf584c0654067eb75091017015ea2557c8fbb
7
- data.tar.gz: 19efad2d61ceac43e634a2d27aedd3243b923824dfb8cdab7e5507b6ccea3e70f08a078a71affd7d70af37a5fb901820bcc6e7726db2e0919bd657b4e091fb46
6
+ metadata.gz: 0dc3f7d36565b7cd3853a44d53cedc140e7ffcddfe59815650cced1172b0654e5136e04cbc4105b353282f591ed2d331fcbc8018c0e1994b93167eac231716de
7
+ data.tar.gz: 3495dc73ad34c2d2a78d581212ee993c12e061a9ba58b39f015bcf515ba6b849aff4ce9076cb91ec938cfad485f443a7281f26b2db37c2a3b12616f08be4cec8
@@ -18,7 +18,7 @@ workflows:
18
18
  jobs:
19
19
  test:
20
20
  docker:
21
- - image: darthjee/circleci_ruby_gems:0.5.3
21
+ - image: darthjee/circleci_ruby_gems:0.5.4
22
22
  environment:
23
23
  PROJECT: arstotzka
24
24
  steps:
@@ -52,7 +52,7 @@ jobs:
52
52
  command: check_specs
53
53
  build-and-release:
54
54
  docker:
55
- - image: darthjee/circleci_ruby_gems:0.5.3
55
+ - image: darthjee/circleci_ruby_gems:0.5.4
56
56
  environment:
57
57
  PROJECT: arstotzka
58
58
  steps:
data/Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- FROM darthjee/rails_gems:0.5.3 as base
1
+ FROM darthjee/ruby_gems:0.5.4 as base
2
2
  FROM darthjee/scripts:0.1.8 as scripts
3
3
 
4
4
  ######################################
data/README.md CHANGED
@@ -21,7 +21,7 @@ JSON keys)
21
21
 
22
22
  Yard Documentation
23
23
  -------------------
24
- [https://www.rubydoc.info/gems/arstotzka/1.5.0](https://www.rubydoc.info/gems/arstotzka/1.5.0)
24
+ [https://www.rubydoc.info/gems/arstotzka/1.6.0](https://www.rubydoc.info/gems/arstotzka/1.6.0)
25
25
 
26
26
  Instalation
27
27
  ---------------
@@ -114,6 +114,7 @@ Options
114
114
  - json: Method that contains the hash to be parsed (`:json` by default)
115
115
  - path: Path where to find the sub hash that contains the key (empty by default)
116
116
  - type: Type that the value must be cast into ([TypeCast](#typecast))
117
+ - before: method to be ran on result before any wrapping
117
118
 
118
119
  ## TypeCast
119
120
  The type casting, when the option `type` is passed, is done through the `Arstotzka::TypeCast` which can
@@ -19,7 +19,7 @@ Gem::Specification.new do |gem|
19
19
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
20
  gem.require_paths = ['lib']
21
21
 
22
- gem.add_runtime_dependency 'activesupport', '~> 5.2.0'
22
+ gem.add_runtime_dependency 'activesupport', '~> 5.2.4.3'
23
23
  gem.add_runtime_dependency 'sinclair', '>= 1.6.5'
24
24
 
25
25
  gem.add_development_dependency 'bundler', '1.16.1'
@@ -57,7 +57,8 @@ module Arstotzka
57
57
  flatten: false,
58
58
  json: :json,
59
59
  klass: nil,
60
- type: :none
60
+ type: :none,
61
+ before: nil
61
62
  }.freeze
62
63
 
63
64
  add_configs DEFAULT_CONFIGS
@@ -93,7 +94,11 @@ module Arstotzka
93
94
  # # flatten: false,
94
95
  # # json: :json,
95
96
  # # klass: Person,
96
- # # type: :none
97
+ # # type: :none,
98
+ # # full_path: nil,
99
+ # # key: nil,
100
+ # # instance: nil,
101
+ # # befor: nil
97
102
  # # }
98
103
  def options(options_hash = {})
99
104
  Options.new(
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Arstotzka
4
- VERSION = '1.5.0'
4
+ VERSION = '1.6.0'
5
5
  end
@@ -75,6 +75,7 @@ module Arstotzka
75
75
  #
76
76
  # @return [Object]
77
77
  def wrap_element(value)
78
+ value = apply_before(value)
78
79
  value = cast(value)
79
80
  return if value.nil?
80
81
 
@@ -83,6 +84,17 @@ module Arstotzka
83
84
  after(value)
84
85
  end
85
86
 
87
+ # @private
88
+ #
89
+ # Apply before option call
90
+ #
91
+ # @return [Object]
92
+ def apply_before(value)
93
+ return value unless options.before
94
+
95
+ options.instance.send(options.before, value)
96
+ end
97
+
86
98
  # @private
87
99
  #
88
100
  # Wraps each element of the array
@@ -59,7 +59,8 @@ describe Arstotzka::Config do
59
59
  full_path: nil,
60
60
  key: nil,
61
61
  instance: nil,
62
- path: nil
62
+ path: nil,
63
+ before: nil
63
64
  }
64
65
  end
65
66
 
@@ -44,7 +44,8 @@ describe Arstotzka::Config do
44
44
  full_path: nil,
45
45
  key: nil,
46
46
  instance: nil,
47
- path: nil
47
+ path: nil,
48
+ before: :before_action
48
49
  }
49
50
  end
50
51
 
@@ -60,6 +61,7 @@ describe Arstotzka::Config do
60
61
  json :@hash
61
62
  klass Account
62
63
  type :string
64
+ before :before_action
63
65
  end
64
66
  end
65
67
 
@@ -89,7 +91,8 @@ describe Arstotzka::Config do
89
91
  full_path: nil,
90
92
  key: nil,
91
93
  instance: nil,
92
- path: nil
94
+ path: nil,
95
+ before: nil
93
96
  }
94
97
  end
95
98
 
@@ -8,7 +8,7 @@ describe Arstotzka::Wrapper do
8
8
  let(:hash) { { a: 1 } }
9
9
 
10
10
  describe '#wrap' do
11
- let(:value) { hash }
11
+ let(:value) { hash }
12
12
  let(:result) { subject.wrap(value) }
13
13
 
14
14
  context 'with default options' do
@@ -25,7 +25,7 @@ describe Arstotzka::Wrapper do
25
25
  end
26
26
  end
27
27
 
28
- context 'with klass otpion' do
28
+ context 'with klass option' do
29
29
  let(:options) { { klass: OpenStruct } }
30
30
 
31
31
  it 'creates new instance from given class' do
@@ -49,6 +49,80 @@ describe Arstotzka::Wrapper do
49
49
  end
50
50
  end
51
51
 
52
+ context 'with klass and before option as symbol' do
53
+ let(:instance) { Arstotzka::Fetcher::Dummy.new(hash) }
54
+ let(:options) do
55
+ { klass: OpenStruct, before: :ensure_age, instance: instance }
56
+ end
57
+
58
+ let(:hash) { { name: 'Joe' } }
59
+
60
+ it 'creates new instance from given class' do
61
+ expect(result).to be_a(OpenStruct)
62
+ end
63
+
64
+ it 'uses the given value on object initialization' do
65
+ expect(result.name).to eq(hash[:name])
66
+ end
67
+
68
+ it 'updates the hash before ' do
69
+ expect(result.age).to eq(10)
70
+ end
71
+
72
+ context 'with an array as value' do
73
+ let(:value) { [hash] }
74
+
75
+ it 'returns an array' do
76
+ expect(result).to be_a(Array)
77
+ end
78
+
79
+ it 'returns an array of objects of the given class' do
80
+ expect(result).to all(be_a(OpenStruct))
81
+ end
82
+
83
+ it 'returns an array of objects of the initialized values' do
84
+ expect(result.map(&:name)).to all(eq(hash[:name]))
85
+ end
86
+
87
+ it 'updates the hash before ' do
88
+ expect(result.map(&:age)).to all(eq(10))
89
+ end
90
+ end
91
+ end
92
+
93
+ context 'with after_each option as symbol' do
94
+ let(:instance) { Group.new(hash) }
95
+ let(:options) do
96
+ { after_each: :create_person, instance: instance }
97
+ end
98
+
99
+ let(:hash) { 'Fred' }
100
+
101
+ it 'Runs before each on each element' do
102
+ expect(result).to be_a(Person)
103
+ end
104
+
105
+ it 'uses the given value on object initialization' do
106
+ expect(result.name).to eq('Fred')
107
+ end
108
+
109
+ context 'when hash is an array' do
110
+ let(:hash) { %w[Wilma Fred Dino] }
111
+
112
+ it do
113
+ expect(result).to be_a(Array)
114
+ end
115
+
116
+ it 'Runs before each on each element' do
117
+ expect(result).to all(be_a(Person))
118
+ end
119
+
120
+ it 'uses the given value on object initialization' do
121
+ expect(result.first.name).to eq('Wilma')
122
+ end
123
+ end
124
+ end
125
+
52
126
  context 'with type otpion' do
53
127
  let(:type) { :integer }
54
128
  let(:value) { '1' }
@@ -10,6 +10,10 @@ module Arstotzka
10
10
  private
11
11
 
12
12
  attr_reader :json
13
+
14
+ def ensure_age(hash)
15
+ hash.merge(age: 10)
16
+ end
13
17
  end
14
18
  end
15
19
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Person
4
- attr_reader :name
4
+ attr_reader :name, :age
5
5
 
6
6
  def initialize(name)
7
7
  @name = name
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.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darthjee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-17 00:00:00.000000000 Z
11
+ date: 2020-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 5.2.0
19
+ version: 5.2.4.3
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 5.2.0
26
+ version: 5.2.4.3
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: sinclair
29
29
  requirement: !ruby/object:Gem::Requirement