typelizer 0.1.1 → 0.1.3

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: 65dd6e8b799965363d44992335ce8e1b5c2d0f18250baa64d4a5068e534cacc5
4
- data.tar.gz: 6d1f61eaa776bf58de372516d10ac719e6788beec35fd42448a0f0888a2fc5df
3
+ metadata.gz: 883dbe72ef62317bf7561ca3bc91fde04264c479433b8b8db93e04533a34e017
4
+ data.tar.gz: 72ce93cdf283d81378278a465ac20627b0e5a89237246c953039cfe3aab9f7aa
5
5
  SHA512:
6
- metadata.gz: a87108460c0326c5f6696207a083d1cf9b542da1c1c264f9a894af8297f0b8bae97e0e17b251e895a58c74c415576cc5224c8c6b76fa4465d65257bb081c3d0b
7
- data.tar.gz: 87878d14c898d778a0e00d675d2658d6770295d992f96fcc20ec5efc933f8d4a5895ecc7aec97b369d0a51229a4cd06762bf283c0a085e28cb334210762c9236
6
+ metadata.gz: 35ea6330569e25236a7a77a89d86558eb260c24fc93ca21f2c07191a299e47bc5798c86fda6a0c1c82ac1c1f8ef00eafb2007962fa2219a724d31e2653cb686a
7
+ data.tar.gz: dbdf51e50c2f327e31785be7f3e60f2b016136b7ca9fb282be3e63935f96b6217758a308e305bb4e75d4a58bc5d41d330df26cd27a2eb514fd920295f5a2f151
data/CHANGELOG.md CHANGED
@@ -7,6 +7,33 @@ 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
+
31
+ ## [0.1.2] - 2024-09-05
32
+
33
+ ### Fixed
34
+
35
+ - Prevent Alba's `meta nil` raising an error ([@okuramasafumi])
36
+
10
37
  ## [0.1.1] - 2024-08-26
11
38
 
12
39
  ### Fixed
@@ -18,9 +45,12 @@ and this project adheres to [Semantic Versioning].
18
45
  - Initial release ([@skryukov])
19
46
 
20
47
  [@davidrunger]: https://github.com/davidrunger
48
+ [@okuramasafumi]: https://github.com/@okuramasafumi
21
49
  [@skryukov]: https://github.com/skryukov
22
50
 
23
- [Unreleased]: https://github.com/skryukov/typelizer/compare/v0.1.0...HEAD
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
53
+ [0.1.2]: https://github.com/skryukov/typelizer/compare/v0.1.1...v0.1.2
24
54
  [0.1.1]: https://github.com/skryukov/typelizer/compare/v0.1.0...v0.1.1
25
55
  [0.1.0]: https://github.com/skryukov/typelizer/commits/v0.1.0
26
56
 
@@ -29,7 +29,11 @@ module Typelizer
29
29
  class << self
30
30
  def instance
31
31
  @instance ||= new(
32
- serializer_name_mapper: ->(serializer) { serializer.name.ends_with?("Serializer") ? serializer.name.delete_suffix("Serializer") : serializer.name.delete_suffix("Resource") },
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
 
@@ -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.sort_by(&:name)
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)
@@ -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
- config.serializer_name_mapper.call(serializer).tr_s(":", "")
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
@@ -41,6 +41,8 @@ module Typelizer
41
41
  return nil unless serializer._meta
42
42
 
43
43
  name = serializer._meta.first
44
+ return nil unless name
45
+
44
46
  [
45
47
  build_property(name, name)
46
48
  ]
@@ -0,0 +1,5 @@
1
+ {
2
+ <%- properties.each do |property| -%>
3
+ <%= indent(property) %>;
4
+ <%- end -%>
5
+ }
@@ -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
- <%= property %>;
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
- <%= property %>;
15
+ <%= indent(property) %>;
16
16
  <%- end -%>
17
17
  }
18
18
  <%- else -%>
19
19
  type <%= interface.name %> = {
20
20
  <%- interface.properties.each do |property| -%>
21
- <%= property %>;
21
+ <%= indent(property) %>;
22
22
  <%- end -%>
23
23
  }
24
24
  <%- end -%>
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Typelizer
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
@@ -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] ||= ERB.new(File.read(File.join(File.dirname(__FILE__), "templates/#{template}")), trim_mode: "-")
58
- template_cache[template].result_with_hash(context)
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.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-08-26 00:00:00.000000000 Z
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