taro 2.1.0 → 2.2.0
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 +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +21 -24
- data/lib/taro/types/base_type.rb +1 -0
- data/lib/taro/types/field.rb +1 -0
- data/lib/taro/types/shared/caching.rb +3 -2
- data/lib/taro/types/shared/derived_types.rb +5 -3
- data/lib/taro/types/shared/rendering.rb +1 -1
- data/lib/taro/types/shared/type_class.rb +12 -0
- data/lib/taro/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d73348290387aeef3de0558f6557af6572326043dff427ee295386a12e8bf6a6
|
4
|
+
data.tar.gz: 89b0a6c8efae50665d43385b0b4182fba6b07e5f97e933d3add5d0e1ba19f421
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67a0d0a5d2464c7ec6954143c95311b92b1a4a78041b298449815b1e57340de7091ac89235fd9e674ee1d3e99ed531049e99207ec62498fa94687b7c91956edd
|
7
|
+
data.tar.gz: '0768d75380ee9b18de093bebf1952b4eeac1e7965fea8c3a0cb5fbec3ef84ec6a27d5e141a837bf6a0f9d698e5b2ceff9ba2e30a33143edd434ebd25812a933a'
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -244,6 +244,27 @@ class ErrorType < ObjectType
|
|
244
244
|
end
|
245
245
|
```
|
246
246
|
|
247
|
+
### Caching
|
248
|
+
|
249
|
+
Taro provides support for caching. The cache instance can be configured by setting `Taro::Cache.cache_instance`. By default, the railtie will set it to `Rails.cache`.
|
250
|
+
|
251
|
+
It supports configuring an ad hoc cache when using a render call, e.g.
|
252
|
+
|
253
|
+
```ruby
|
254
|
+
bike = Bike.find(params[:id])
|
255
|
+
BikeType.with_cache(cache_key: bike.cache_key, expires_in: 3.minutes).render(bike)
|
256
|
+
```
|
257
|
+
|
258
|
+
Or by configuring the cache rule on a per type basis, e.g.
|
259
|
+
|
260
|
+
```ruby
|
261
|
+
class BikeType < ObjectType
|
262
|
+
self.cache_key = ->(bike) { bike.cache_key_with_version }
|
263
|
+
self.expires_in = 1.hour
|
264
|
+
# ...
|
265
|
+
end
|
266
|
+
```
|
267
|
+
|
247
268
|
## FAQ
|
248
269
|
|
249
270
|
### How do I render API docs?
|
@@ -344,30 +365,6 @@ class MyController < ApplicationController
|
|
344
365
|
end
|
345
366
|
```
|
346
367
|
|
347
|
-
## Caching
|
348
|
-
|
349
|
-
Taro provides support for caching. The cache instance can be configured by setting `Taro::Cache.cache_instance`. By default, the railtie will set it to `Rails.cache`.
|
350
|
-
|
351
|
-
It supports configuring an add hoc cache when using a render call, e.g.
|
352
|
-
|
353
|
-
```ruby
|
354
|
-
bike = Bike.find(params[:id])
|
355
|
-
BikeType.with_cache(cache_key: bike.cache_key, expires_in: 3.minutes).render(bike)
|
356
|
-
```
|
357
|
-
|
358
|
-
Or by configuring the cache rule on a per type bases, e.g.
|
359
|
-
|
360
|
-
```ruby
|
361
|
-
class BikeType < ObjectType
|
362
|
-
# Optional description of BikeType (for the OpenAPI export)
|
363
|
-
self.desc = 'A bike and all relevant information about it'
|
364
|
-
self.cache_key = ->(bike) { bike.cache_key_with_version }
|
365
|
-
self.expires_in = 1.hour
|
366
|
-
|
367
|
-
...
|
368
|
-
end
|
369
|
-
```
|
370
|
-
|
371
368
|
## Possible future features
|
372
369
|
|
373
370
|
- warning/raising for undeclared input params (currently they are ignored)
|
data/lib/taro/types/base_type.rb
CHANGED
data/lib/taro/types/field.rb
CHANGED
@@ -3,6 +3,7 @@ require_relative 'field_validation'
|
|
3
3
|
Taro::Types::Field = Data.define(:name, :type, :null, :method, :default, :enum, :defined_at, :desc, :deprecated) do
|
4
4
|
include Taro::Types::FieldValidation
|
5
5
|
include Taro::Types::Shared::Errors
|
6
|
+
include Taro::Types::Shared::TypeClass
|
6
7
|
|
7
8
|
def initialize(name:, type:, null:, method: name, default: Taro::None, enum: nil, defined_at: nil, desc: nil, deprecated: nil)
|
8
9
|
enum = coerce_to_enum(enum)
|
@@ -7,7 +7,7 @@ module Taro::Types::Shared::Caching
|
|
7
7
|
|
8
8
|
def self.included(klass)
|
9
9
|
klass.extend(ClassMethods)
|
10
|
-
klass.singleton_class.attr_accessor :expires_in
|
10
|
+
klass.singleton_class.attr_accessor :expires_in
|
11
11
|
klass.singleton_class.attr_reader :cache_key
|
12
12
|
end
|
13
13
|
|
@@ -23,7 +23,8 @@ module Taro::Types::Shared::Caching
|
|
23
23
|
klass = dup
|
24
24
|
klass.cache_key = cache_key.is_a?(Proc) ? cache_key : ->(_) { cache_key }
|
25
25
|
klass.expires_in = expires_in
|
26
|
-
|
26
|
+
this = self
|
27
|
+
klass.define_singleton_method(:type_class) { this }
|
27
28
|
klass
|
28
29
|
end
|
29
30
|
end
|
@@ -35,9 +35,11 @@ module Taro::Types::Shared::DerivedTypes
|
|
35
35
|
|
36
36
|
root.define_singleton_method(method_name) do
|
37
37
|
derived_types[type] ||= begin
|
38
|
-
|
39
|
-
|
40
|
-
|
38
|
+
name || raise(Taro::ArgumentError, 'Cannot derive from anonymous type')
|
39
|
+
|
40
|
+
coerced_type = Taro::Types::Coercion.call(type:)
|
41
|
+
new_type = Class.new(coerced_type)
|
42
|
+
new_type.define_name("#{name}.#{method_name}")
|
41
43
|
new_type.derive_from(self)
|
42
44
|
new_type
|
43
45
|
end
|
@@ -5,7 +5,7 @@ module Taro::Types::Shared::Rendering
|
|
5
5
|
result = Taro::Cache.call(object, **cache_attrs) do
|
6
6
|
new(object).cached_coerce_response
|
7
7
|
end
|
8
|
-
self.last_render = [
|
8
|
+
self.last_render = [type_class, result.__id__]
|
9
9
|
result
|
10
10
|
end
|
11
11
|
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# `type_class` is a convenience method to get the type class of types,
|
2
|
+
# of with_cache-types, of type instances, and of fields in the same way.
|
3
|
+
module Taro::Types::Shared::TypeClass
|
4
|
+
def self.included(klass)
|
5
|
+
if klass.instance_methods.include?(:type) # Field
|
6
|
+
klass.alias_method :type_class, :type
|
7
|
+
else # BaseType
|
8
|
+
klass.singleton_class.alias_method :type_class, :itself
|
9
|
+
klass.alias_method :type_class, :class
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/taro/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: taro
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Janosch Müller
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2025-02-
|
12
|
+
date: 2025-02-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
@@ -117,6 +117,7 @@ files:
|
|
117
117
|
- lib/taro/types/shared/openapi_type.rb
|
118
118
|
- lib/taro/types/shared/pattern.rb
|
119
119
|
- lib/taro/types/shared/rendering.rb
|
120
|
+
- lib/taro/types/shared/type_class.rb
|
120
121
|
- lib/taro/version.rb
|
121
122
|
- tasks/benchmark.rake
|
122
123
|
- tasks/benchmark_1kb.json
|
@@ -143,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
143
144
|
- !ruby/object:Gem::Version
|
144
145
|
version: '0'
|
145
146
|
requirements: []
|
146
|
-
rubygems_version: 3.5.
|
147
|
+
rubygems_version: 3.5.22
|
147
148
|
signing_key:
|
148
149
|
specification_version: 4
|
149
150
|
summary: Typed Api using Ruby Objects.
|