typelizer 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +23 -1
- data/lib/typelizer/config.rb +5 -1
- data/lib/typelizer/dsl.rb +2 -2
- data/lib/typelizer/generator.rb +2 -1
- data/lib/typelizer/interface.rb +10 -2
- data/lib/typelizer/renderer.rb +28 -0
- data/lib/typelizer/templates/inline_type.ts.erb +5 -0
- data/lib/typelizer/templates/interface.ts.erb +3 -3
- data/lib/typelizer/version.rb +1 -1
- data/lib/typelizer/writer.rb +2 -3
- data/lib/typelizer.rb +3 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 883dbe72ef62317bf7561ca3bc91fde04264c479433b8b8db93e04533a34e017
|
4
|
+
data.tar.gz: 72ce93cdf283d81378278a465ac20627b0e5a89237246c953039cfe3aab9f7aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 35ea6330569e25236a7a77a89d86558eb260c24fc93ca21f2c07191a299e47bc5798c86fda6a0c1c82ac1c1f8ef00eafb2007962fa2219a724d31e2653cb686a
|
7
|
+
data.tar.gz: dbdf51e50c2f327e31785be7f3e60f2b016136b7ca9fb282be3e63935f96b6217758a308e305bb4e75d4a58bc5d41d330df26cd27a2eb514fd920295f5a2f151
|
data/CHANGELOG.md
CHANGED
@@ -7,6 +7,27 @@ and this project adheres to [Semantic Versioning].
|
|
7
7
|
|
8
8
|
## [Unreleased]
|
9
9
|
|
10
|
+
## [0.1.3] - 2024-09-27
|
11
|
+
|
12
|
+
## Added
|
13
|
+
|
14
|
+
- Support inline associations ([@okuramasafumi], [@skryukov])
|
15
|
+
|
16
|
+
Example of Alba serializer with inline associations (note the `helper Typelizer::DSL`, see [Alba's docs](https://github.com/okuramasafumi/alba?tab=readme-ov-file#helper) for more details):
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
class FooSerializer
|
20
|
+
include Alba::Resource
|
21
|
+
helper Typelizer::DSL
|
22
|
+
|
23
|
+
many :bars do
|
24
|
+
typelize_from Bar
|
25
|
+
|
26
|
+
attributes :id, :name
|
27
|
+
end
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
10
31
|
## [0.1.2] - 2024-09-05
|
11
32
|
|
12
33
|
### Fixed
|
@@ -27,7 +48,8 @@ and this project adheres to [Semantic Versioning].
|
|
27
48
|
[@okuramasafumi]: https://github.com/@okuramasafumi
|
28
49
|
[@skryukov]: https://github.com/skryukov
|
29
50
|
|
30
|
-
[Unreleased]: https://github.com/skryukov/typelizer/compare/v0.1.
|
51
|
+
[Unreleased]: https://github.com/skryukov/typelizer/compare/v0.1.3...HEAD
|
52
|
+
[0.1.3]: https://github.com/skryukov/typelizer/compare/v0.1.2...v0.1.3
|
31
53
|
[0.1.2]: https://github.com/skryukov/typelizer/compare/v0.1.1...v0.1.2
|
32
54
|
[0.1.1]: https://github.com/skryukov/typelizer/compare/v0.1.0...v0.1.1
|
33
55
|
[0.1.0]: https://github.com/skryukov/typelizer/commits/v0.1.0
|
data/lib/typelizer/config.rb
CHANGED
@@ -29,7 +29,11 @@ module Typelizer
|
|
29
29
|
class << self
|
30
30
|
def instance
|
31
31
|
@instance ||= new(
|
32
|
-
serializer_name_mapper: ->(serializer)
|
32
|
+
serializer_name_mapper: ->(serializer) do
|
33
|
+
return "" if serializer.name.nil?
|
34
|
+
|
35
|
+
serializer.name.ends_with?("Serializer") ? serializer.name&.delete_suffix("Serializer") : serializer.name&.delete_suffix("Resource")
|
36
|
+
end,
|
33
37
|
serializer_model_mapper: ->(serializer) do
|
34
38
|
base_class = serializer_name_mapper.call(serializer)
|
35
39
|
Object.const_get(base_class) if Object.const_defined?(base_class)
|
data/lib/typelizer/dsl.rb
CHANGED
@@ -4,12 +4,12 @@ module Typelizer
|
|
4
4
|
# typelize attribute_name: ["string", "Date", optional: true, nullable: true, multi: true]
|
5
5
|
|
6
6
|
def self.included(base)
|
7
|
-
Typelizer.base_classes << base.to_s
|
7
|
+
Typelizer.base_classes << base.to_s if base.name
|
8
8
|
base.extend(ClassMethods)
|
9
9
|
end
|
10
10
|
|
11
11
|
def self.extended(base)
|
12
|
-
Typelizer.base_classes << base.to_s
|
12
|
+
Typelizer.base_classes << base.to_s if base.name
|
13
13
|
base.extend(ClassMethods)
|
14
14
|
end
|
15
15
|
|
data/lib/typelizer/generator.rb
CHANGED
@@ -33,8 +33,9 @@ module Typelizer
|
|
33
33
|
raise ArgumentError, "Please ensure all your serializers include Typelizer::DSL." if base_classes.none?
|
34
34
|
end
|
35
35
|
|
36
|
-
(base_classes + base_classes.flat_map(&:descendants)).uniq
|
36
|
+
(base_classes + base_classes.flat_map(&:descendants)).uniq
|
37
37
|
.reject { |serializer| Typelizer.reject_class.call(serializer: serializer) }
|
38
|
+
.sort_by(&:name)
|
38
39
|
end
|
39
40
|
|
40
41
|
def read_serializers(files = nil)
|
data/lib/typelizer/interface.rb
CHANGED
@@ -11,8 +11,16 @@ module Typelizer
|
|
11
11
|
@serializer_plugin = config.serializer_plugin.new(serializer: serializer, config: config)
|
12
12
|
end
|
13
13
|
|
14
|
+
def inline?
|
15
|
+
!serializer.is_a?(Class) || serializer.name.nil?
|
16
|
+
end
|
17
|
+
|
14
18
|
def name
|
15
|
-
|
19
|
+
if inline?
|
20
|
+
Renderer.new("inline_type.ts.erb").call(properties: properties).strip
|
21
|
+
else
|
22
|
+
config.serializer_name_mapper.call(serializer).tr_s(":", "")
|
23
|
+
end
|
16
24
|
end
|
17
25
|
|
18
26
|
def filename
|
@@ -51,7 +59,7 @@ module Typelizer
|
|
51
59
|
.partition { |type| type.is_a?(Interface) }
|
52
60
|
|
53
61
|
serializer_types = association_serializers
|
54
|
-
.filter_map { |interface| interface.name if interface.name != name }
|
62
|
+
.filter_map { |interface| interface.name if interface.name != name && !interface.inline? }
|
55
63
|
|
56
64
|
custom_type_imports = attribute_types
|
57
65
|
.flat_map { |type| extract_typescript_types(type.to_s) }
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "erb"
|
4
|
+
|
5
|
+
module Typelizer
|
6
|
+
class Renderer
|
7
|
+
def initialize(template)
|
8
|
+
@erb = ERB.new(File.read(File.join(File.dirname(__FILE__), "templates/#{template}")), trim_mode: "-")
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(**context)
|
12
|
+
b = binding
|
13
|
+
context.each_pair do |key, value|
|
14
|
+
b.local_variable_set(key, value)
|
15
|
+
end
|
16
|
+
erb.result(b)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
attr_reader :erb
|
22
|
+
|
23
|
+
def indent(content, multiplier = 2)
|
24
|
+
spaces = " " * multiplier
|
25
|
+
content.to_s.each_line.map { |line| line.blank? ? line : "#{spaces}#{line}" }.join
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -5,20 +5,20 @@ import type {<%= interface.imports.join(", ") %>} from '<%= interface.config.typ
|
|
5
5
|
<%- if interface.root_key -%>
|
6
6
|
type <%= interface.name %>Data = {
|
7
7
|
<%- interface.properties.each do |property| -%>
|
8
|
-
|
8
|
+
<%= indent(property) %>;
|
9
9
|
<%- end -%>
|
10
10
|
}
|
11
11
|
|
12
12
|
type <%= interface.name %> = {
|
13
13
|
<%= interface.root_key %>: <%= interface.name %>Data;
|
14
14
|
<%- interface.meta_fields&.each do |property| -%>
|
15
|
-
|
15
|
+
<%= indent(property) %>;
|
16
16
|
<%- end -%>
|
17
17
|
}
|
18
18
|
<%- else -%>
|
19
19
|
type <%= interface.name %> = {
|
20
20
|
<%- interface.properties.each do |property| -%>
|
21
|
-
|
21
|
+
<%= indent(property) %>;
|
22
22
|
<%- end -%>
|
23
23
|
}
|
24
24
|
<%- end -%>
|
data/lib/typelizer/version.rb
CHANGED
data/lib/typelizer/writer.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "fileutils"
|
4
|
-
require "erb"
|
5
4
|
|
6
5
|
module Typelizer
|
7
6
|
class Writer
|
@@ -54,8 +53,8 @@ module Typelizer
|
|
54
53
|
end
|
55
54
|
|
56
55
|
def render_template(template, **context)
|
57
|
-
template_cache[template] ||=
|
58
|
-
template_cache[template].
|
56
|
+
template_cache[template] ||= Renderer.new(template)
|
57
|
+
template_cache[template].call(**context)
|
59
58
|
end
|
60
59
|
|
61
60
|
def cleanup_output_dir
|
data/lib/typelizer.rb
CHANGED
@@ -4,6 +4,7 @@ require_relative "typelizer/version"
|
|
4
4
|
require_relative "typelizer/config"
|
5
5
|
require_relative "typelizer/property"
|
6
6
|
require_relative "typelizer/interface"
|
7
|
+
require_relative "typelizer/renderer"
|
7
8
|
require_relative "typelizer/writer"
|
8
9
|
require_relative "typelizer/generator"
|
9
10
|
|
@@ -18,6 +19,8 @@ require_relative "typelizer/model_plugins/active_record"
|
|
18
19
|
|
19
20
|
require_relative "typelizer/railtie" if defined?(Rails)
|
20
21
|
|
22
|
+
require "logger"
|
23
|
+
|
21
24
|
module Typelizer
|
22
25
|
class << self
|
23
26
|
def enabled?
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typelizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Svyatoslav Kryukov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-09-
|
11
|
+
date: 2024-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -43,6 +43,7 @@ files:
|
|
43
43
|
- lib/typelizer/model_plugins/active_record.rb
|
44
44
|
- lib/typelizer/property.rb
|
45
45
|
- lib/typelizer/railtie.rb
|
46
|
+
- lib/typelizer/renderer.rb
|
46
47
|
- lib/typelizer/serializer_plugins/alba.rb
|
47
48
|
- lib/typelizer/serializer_plugins/ams.rb
|
48
49
|
- lib/typelizer/serializer_plugins/auto.rb
|
@@ -50,6 +51,7 @@ files:
|
|
50
51
|
- lib/typelizer/serializer_plugins/oj_serializers.rb
|
51
52
|
- lib/typelizer/templates/fingerprint.ts.erb
|
52
53
|
- lib/typelizer/templates/index.ts.erb
|
54
|
+
- lib/typelizer/templates/inline_type.ts.erb
|
53
55
|
- lib/typelizer/templates/interface.ts.erb
|
54
56
|
- lib/typelizer/version.rb
|
55
57
|
- lib/typelizer/writer.rb
|