typelizer 0.1.2 → 0.1.4

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: d728a2d15357f1cd3316c78dde5407a130c26e80cfc8e4e706cfa3836ef4d650
4
- data.tar.gz: f3f4c3b3420da55c4c2a671a85b9a03d06cf02a09a1df63578c4df0f91a4ea75
3
+ metadata.gz: 45bdcfd99663e0a61f6d70b5825f4ee29db4bc70927676c162fab5e8557675d6
4
+ data.tar.gz: ff3df8f4e1433bf7e8eb2f88318c117d58f86d14b974d6c1100fafe04988edc6
5
5
  SHA512:
6
- metadata.gz: e1adc62cf80343300db84b62a34e13fb0f6bf5b6749a3940f34c56463054b0f4b50727ce946fcecff3fd05584ba3d45c0cd94ac77b733a6cdb676381b7163716
7
- data.tar.gz: a7e6101a95798e1a1f5522b096fec15e966cfd3f354004c32d83702ea7cea230b06185df2180226d5b93325de51414f03d59cb05caba880dd9cd72ad87473b9e
6
+ metadata.gz: ed256b928dd5621f2a573c74997bcb2dd072af8ed02e79919e3f0506056bfd579405d234257136d87a3840393cc6fa625f2b50c1e9362eabcb5b7682e0d0eb00
7
+ data.tar.gz: ba1dab8fa94374dc25b02ac67b7e995f4a5d10c9492ceddf7bb866f22abaf609d148393af8a6c1eb899ccb553f0b2be542618811b58e1ae6b08f0d9d95d916ae
data/CHANGELOG.md CHANGED
@@ -7,6 +7,34 @@ and this project adheres to [Semantic Versioning].
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.1.4] - 2024-10-04
11
+
12
+ ## Added
13
+
14
+ - PORO model plugin ([@okuramasafumi])
15
+ - Auto model plugin ([@skryukov])
16
+
17
+ ## [0.1.3] - 2024-09-27
18
+
19
+ ## Added
20
+
21
+ - Support inline associations ([@okuramasafumi], [@skryukov])
22
+
23
+ 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):
24
+
25
+ ```ruby
26
+ class FooSerializer
27
+ include Alba::Resource
28
+ helper Typelizer::DSL
29
+
30
+ many :bars do
31
+ typelize_from Bar
32
+
33
+ attributes :id, :name
34
+ end
35
+ end
36
+ ```
37
+
10
38
  ## [0.1.2] - 2024-09-05
11
39
 
12
40
  ### Fixed
@@ -27,7 +55,9 @@ and this project adheres to [Semantic Versioning].
27
55
  [@okuramasafumi]: https://github.com/@okuramasafumi
28
56
  [@skryukov]: https://github.com/skryukov
29
57
 
30
- [Unreleased]: https://github.com/skryukov/typelizer/compare/v0.1.2...HEAD
58
+ [Unreleased]: https://github.com/skryukov/typelizer/compare/v0.1.4...HEAD
59
+ [0.1.4]: https://github.com/skryukov/typelizer/compare/v0.1.3...v0.1.4
60
+ [0.1.3]: https://github.com/skryukov/typelizer/compare/v0.1.2...v0.1.3
31
61
  [0.1.2]: https://github.com/skryukov/typelizer/compare/v0.1.1...v0.1.2
32
62
  [0.1.1]: https://github.com/skryukov/typelizer/compare/v0.1.0...v0.1.1
33
63
  [0.1.0]: https://github.com/skryukov/typelizer/commits/v0.1.0
data/README.md CHANGED
@@ -202,8 +202,8 @@ Typelizer.configure do |config|
202
202
  # Custom transformation for generated properties
203
203
  config.properties_transformer = ->(properties) { ... }
204
204
 
205
- # Plugin for model type inference (default: ModelPlugins::ActiveRecord)
206
- config.model_plugin = Typelizer::ModelPlugins::ActiveRecord
205
+ # Plugin for model type inference (default: ModelPlugins::Auto)
206
+ config.model_plugin = Typelizer::ModelPlugins::Auto
207
207
 
208
208
  # Plugin for serializer parsing (default: SerializerPlugins::Auto)
209
209
  config.serializer_plugin = Typelizer::SerializerPlugins::Auto
@@ -29,13 +29,17 @@ 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)
36
40
  end,
37
41
 
38
- model_plugin: ModelPlugins::ActiveRecord,
42
+ model_plugin: ModelPlugins::Auto,
39
43
  serializer_plugin: SerializerPlugins::Auto,
40
44
  plugin_configs: {},
41
45
 
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,19 @@
1
+ module Typelizer
2
+ module ModelPlugins
3
+ module Auto
4
+ class << self
5
+ def new(model_class:, config:)
6
+ plugin(model_class).new(model_class: model_class, config: config)
7
+ end
8
+
9
+ def plugin(model_class)
10
+ if model_class && model_class < ::ActiveRecord::Base
11
+ ActiveRecord
12
+ else
13
+ Poro
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ module Typelizer
2
+ module ModelPlugins
3
+ class Poro
4
+ # We don't care about intialization
5
+ def initialize(...)
6
+ end
7
+
8
+ def infer_types(prop)
9
+ prop
10
+ end
11
+ end
12
+ end
13
+ end
@@ -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
@@ -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.2"
4
+ VERSION = "0.1.4"
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
 
@@ -15,9 +16,13 @@ require_relative "typelizer/serializer_plugins/alba"
15
16
  require_relative "typelizer/serializer_plugins/ams"
16
17
 
17
18
  require_relative "typelizer/model_plugins/active_record"
19
+ require_relative "typelizer/model_plugins/poro"
20
+ require_relative "typelizer/model_plugins/auto"
18
21
 
19
22
  require_relative "typelizer/railtie" if defined?(Rails)
20
23
 
24
+ require "logger"
25
+
21
26
  module Typelizer
22
27
  class << self
23
28
  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.2
4
+ version: 0.1.4
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-05 00:00:00.000000000 Z
11
+ date: 2024-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -41,8 +41,11 @@ files:
41
41
  - lib/typelizer/interface.rb
42
42
  - lib/typelizer/listen.rb
43
43
  - lib/typelizer/model_plugins/active_record.rb
44
+ - lib/typelizer/model_plugins/auto.rb
45
+ - lib/typelizer/model_plugins/poro.rb
44
46
  - lib/typelizer/property.rb
45
47
  - lib/typelizer/railtie.rb
48
+ - lib/typelizer/renderer.rb
46
49
  - lib/typelizer/serializer_plugins/alba.rb
47
50
  - lib/typelizer/serializer_plugins/ams.rb
48
51
  - lib/typelizer/serializer_plugins/auto.rb
@@ -50,6 +53,7 @@ files:
50
53
  - lib/typelizer/serializer_plugins/oj_serializers.rb
51
54
  - lib/typelizer/templates/fingerprint.ts.erb
52
55
  - lib/typelizer/templates/index.ts.erb
56
+ - lib/typelizer/templates/inline_type.ts.erb
53
57
  - lib/typelizer/templates/interface.ts.erb
54
58
  - lib/typelizer/version.rb
55
59
  - lib/typelizer/writer.rb