json_statham 0.1.0 → 0.1.2

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
  SHA256:
3
- metadata.gz: bcc57468bc33bb597b1817adc3f882a28b6f9f06e2377d3cd6abe2e0a16c1bd8
4
- data.tar.gz: fae57291e39be69d2fe5a84763eb2280c3c0112719e4cc43959c0decf94bb35d
3
+ metadata.gz: 9241e02a36d573851c728c187de5200135c00a07cc50911c14da92447b7da2fb
4
+ data.tar.gz: 86bc9cf226cc9229e776413c8953c00a65444ff817faf3d3477d070919d979f2
5
5
  SHA512:
6
- metadata.gz: ba7b836e09f469ec095b73b0908bf0ccffa19605fb3e1ccbf6aa855824a1c1b8f55677370595bda2b326445b61b7a8f1d010544edef8e3b30a0c923b8cce5591
7
- data.tar.gz: 3c5fda63e093ff347e378b75fa507559b23b7b59cbacfaf72ef7151651ecbe43a48d5fd56dca817573a17137cd744d882ea02afa6f3ddfbe86de500fca11cb75
6
+ metadata.gz: cd2fc935a733ea0a3eb9f56f8ef8d8b78339a5e5f768a4cc45a38532cb1ea030a41cf040a84ae4cad8a3f1b2aa1a1816915511f09753e2b20a1d2dd54e743d64
7
+ data.tar.gz: 39139005d3b156f53296b0c57607fdc4fc4c718a56dca022186ed773993743c63468ae4d09d2251eb3ebfb6b75ae6bcbe7a6da10da1f30021e9561392ae35074
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  ## Changelog
2
2
 
3
+ ### [0.1.2] - 2023-02-07
4
+
5
+ * Features:
6
+ * Add `raise_ratio` to config.
7
+
8
+ * Deprecations:
9
+ * Remove `logger` from config.
10
+
11
+ ### [0.1.1] - 2023-02-07
12
+ yanked
13
+
3
14
  ### [0.1.0] - 2023-02-06
4
15
 
5
16
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_statham (0.1.0)
4
+ json_statham (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -69,19 +69,22 @@ Available configuration attributes:
69
69
  JsonStatham.configure do |config|
70
70
  config.schemas_path = "schemas"
71
71
  config.store_schema = true
72
- config.logger = true
72
+ config.raise_ratio = 10
73
73
  end
74
74
  ```
75
75
 
76
76
  *Required attributes:*
77
77
 
78
- - `schemas_path` The path where the json files will be read and created
78
+ - `schemas_path` String.
79
+ The path where the json files will be read and created.
79
80
 
80
81
  *Optional attributes:*
81
82
 
82
- - `store_schema` Default to `false`. It allows to create or not a new file
83
+ - `store_schema` Boolean, default to `false`.
84
+ It allows to create or not a new file.
83
85
 
84
- - `logger` Default to `false`. It allows to create or not a new file
86
+ - `raise_ratio` Integer, default to `nil`.
87
+ The ratio of increase in execution time. This allows to raise an error when the execution of the block takes longer than expected.
85
88
 
86
89
  ## Example using RSpec
87
90
 
@@ -114,7 +117,7 @@ You can thenuse stathamnize with different traits in your spec file.
114
117
  ```ruby
115
118
  RSpec.describe UserSerializer do
116
119
  describe "Schema" do
117
- subject { stathamnize(trait) { serializer }.success? }
120
+ subject { stathamnize(trait) { serializer } }
118
121
 
119
122
  context "Given a valid user" do
120
123
  let(:serializer) { UserSerializer.new(user).to_h }
@@ -122,7 +125,7 @@ RSpec.describe UserSerializer do
122
125
  let(:user) { create(:user, :valid) }
123
126
 
124
127
  it "has a valid schema" do
125
- expect(subject).to eq(true)
128
+ expect(subject.success?).to eq(true)
126
129
  end
127
130
  end
128
131
 
@@ -132,13 +135,51 @@ RSpec.describe UserSerializer do
132
135
  let(:user) { create(:user, :invalid) }
133
136
 
134
137
  it "has a valid schema" do
135
- expect(subject).to eq(true)
138
+ expect(subject.success?).to eq(true)
136
139
  end
137
140
  end
138
141
  end
139
142
  end
140
143
  ```
141
144
 
145
+ Or you can create a shared example
146
+
147
+ ```ruby
148
+ RSpec.shared_examples 'a serializer' do |schema_name|
149
+ describe "#as_json" do
150
+ subject do
151
+ stathamnize("#{serializer_path}/#{schema_name}") do
152
+ described_class.new(record).as_json
153
+ end
154
+ end
155
+
156
+ let(:serializer_path) { described_class.name.underscore }
157
+
158
+ it "returns #{schema_name} object as json" do
159
+ expect(subject.success?).to eq(true)
160
+ end
161
+ end
162
+ end
163
+ ```
164
+
165
+ Then you can use it inside your specs
166
+
167
+ ```ruby
168
+ require "spec_helper"
169
+
170
+ RSpec.describe FooSerializer do
171
+ it_behaves_like "a serializer", "foo" do
172
+ let(:record) { create(:foo) }
173
+ end
174
+
175
+ FactoryBot.factories[:foo].definition.defined_traits.map(&:name).each do |trait|
176
+ it_behaves_like "a serializer", trait do
177
+ let(:record) { create(:foo, trait) }
178
+ end
179
+ end
180
+ end
181
+ ```
182
+
142
183
  ## Development
143
184
 
144
185
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -2,26 +2,26 @@
2
2
 
3
3
  module JsonStatham
4
4
  class Config
5
- attr_reader :schemas_path, :store_schema, :logger
5
+ attr_reader :schemas_path, :store_schema, :raise_ratio
6
6
 
7
7
  def initialize
8
8
  @schemas_path = nil
9
9
  @store_schema = nil
10
- @logger = nil
10
+ @raise_ratio = nil
11
11
  end
12
12
 
13
13
  def store_schema?
14
14
  !!store_schema
15
15
  end
16
16
 
17
- def logger?
18
- !!logger
19
- end
20
-
21
17
  def schemas_path_present?
22
18
  !!schemas_path
23
19
  end
24
20
 
21
+ def raise_on_failure?
22
+ !!raise_ratio
23
+ end
24
+
25
25
  def schemas_path=(value)
26
26
  Validation.check_object_class(value, [String])
27
27
 
@@ -34,10 +34,10 @@ module JsonStatham
34
34
  @store_schema = value
35
35
  end
36
36
 
37
- def logger=(value)
38
- Validation.check_object_class(value, [TrueClass, FalseClass, NilClass])
37
+ def raise_ratio=(value)
38
+ Validation.check_object_class(value, [Integer])
39
39
 
40
- @logger = value
40
+ @raise_ratio = value
41
41
  end
42
42
  end
43
43
  end
@@ -18,10 +18,11 @@ module JsonStatham
18
18
  def call
19
19
  @reader = JsonStatham::Requests::Reader.call(self)
20
20
  @observer = JsonStatham::Requests::Observer.call(self)
21
+ result = JsonStatham::Result.call(self)
21
22
 
22
23
  store_current_schema
23
24
 
24
- JsonStatham::Result.call(self)
25
+ result
25
26
  end
26
27
 
27
28
  def store_current_schema
@@ -1,8 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonStatham
4
+ class InvalidRatioError < StandardError
5
+ end
6
+
4
7
  class Result
5
- attr_reader :parser
8
+ attr_reader :parser, :config
6
9
 
7
10
  def self.call(parser)
8
11
  new(parser).call
@@ -12,15 +15,11 @@ module JsonStatham
12
15
  Validation.check_object_class(parser, [JsonStatham::Parser])
13
16
 
14
17
  @parser = parser
18
+ @config = JsonStatham.config
15
19
  end
16
20
 
17
21
  def call
18
- if JsonStatham.config.logger?
19
- puts <<-RESULT_MSG
20
- Previous duration: #{previous_duration}
21
- Current duration: #{current_duration}
22
- RESULT_MSG
23
- end
22
+ ensure_valid_ratio
24
23
 
25
24
  self
26
25
  end
@@ -39,6 +38,10 @@ module JsonStatham
39
38
  parser.previous_duration
40
39
  end
41
40
 
41
+ def previous_duration?
42
+ !!previous_duration
43
+ end
44
+
42
45
  def success?
43
46
  parser.valid?
44
47
  end
@@ -46,5 +49,23 @@ module JsonStatham
46
49
  def failure?
47
50
  !success?
48
51
  end
52
+
53
+ def ratio
54
+ return 0 unless observed?
55
+
56
+ current_duration.fdiv(previous_duration)
57
+ end
58
+
59
+ def raise_ratio
60
+ config.raise_ratio
61
+ end
62
+
63
+ private
64
+
65
+ def ensure_valid_ratio
66
+ return unless config.raise_on_failure? && previous_duration? && ratio > raise_ratio
67
+
68
+ raise JsonStatham::InvalidRatioError, "raise_ratio: #{raise_ratio}. current_ratio: #{ratio}"
69
+ end
49
70
  end
50
71
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonStatham
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_statham
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-06 00:00:00.000000000 Z
11
+ date: 2023-02-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: JsonStatham allows to check the structure changes in a json
14
14
  email: