From 56004cbed3e502048366b29b01f0d7d6e095f9be Mon Sep 17 00:00:00 2001 From: Svyatoslav Kryukov Date: Mon, 30 Sep 2024 14:15:49 +0300 Subject: [PATCH] Introduce Typelizer::ModelPlugins::Auto --- CHANGELOG.md | 5 +++++ README.md | 4 ++-- lib/typelizer.rb | 1 + lib/typelizer/config.rb | 2 +- lib/typelizer/model_plugins/auto.rb | 19 +++++++++++++++++++ .../app/serializers/alba/poro_serializer.rb | 7 +------ 6 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 lib/typelizer/model_plugins/auto.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b526b2..725f493 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning]. ## [Unreleased] +## Added + +- PORO model plugin ([@okuramasafumi]) +- Auto model plugin ([@skryukov]) + ## [0.1.3] - 2024-09-27 ## Added diff --git a/README.md b/README.md index c9d50bb..d3a15c2 100644 --- a/README.md +++ b/README.md @@ -202,8 +202,8 @@ Typelizer.configure do |config| # Custom transformation for generated properties config.properties_transformer = ->(properties) { ... } - # Plugin for model type inference (default: ModelPlugins::ActiveRecord) - config.model_plugin = Typelizer::ModelPlugins::ActiveRecord + # Plugin for model type inference (default: ModelPlugins::Auto) + config.model_plugin = Typelizer::ModelPlugins::Auto # Plugin for serializer parsing (default: SerializerPlugins::Auto) config.serializer_plugin = Typelizer::SerializerPlugins::Auto diff --git a/lib/typelizer.rb b/lib/typelizer.rb index 8050eb1..ebed7b4 100644 --- a/lib/typelizer.rb +++ b/lib/typelizer.rb @@ -17,6 +17,7 @@ require_relative "typelizer/model_plugins/active_record" require_relative "typelizer/model_plugins/poro" +require_relative "typelizer/model_plugins/auto" require_relative "typelizer/railtie" if defined?(Rails) diff --git a/lib/typelizer/config.rb b/lib/typelizer/config.rb index b5c634e..e7d0fff 100644 --- a/lib/typelizer/config.rb +++ b/lib/typelizer/config.rb @@ -39,7 +39,7 @@ def instance Object.const_get(base_class) if Object.const_defined?(base_class) end, - model_plugin: ModelPlugins::ActiveRecord, + model_plugin: ModelPlugins::Auto, serializer_plugin: SerializerPlugins::Auto, plugin_configs: {}, diff --git a/lib/typelizer/model_plugins/auto.rb b/lib/typelizer/model_plugins/auto.rb new file mode 100644 index 0000000..eb46b04 --- /dev/null +++ b/lib/typelizer/model_plugins/auto.rb @@ -0,0 +1,19 @@ +module Typelizer + module ModelPlugins + module Auto + class << self + def new(model_class:, config:) + plugin(model_class).new(model_class: model_class, config: config) + end + + def plugin(model_class) + if model_class && model_class < ::ActiveRecord::Base + ActiveRecord + else + Poro + end + end + end + end + end +end diff --git a/spec/app/app/serializers/alba/poro_serializer.rb b/spec/app/app/serializers/alba/poro_serializer.rb index 1bad199..4232323 100644 --- a/spec/app/app/serializers/alba/poro_serializer.rb +++ b/spec/app/app/serializers/alba/poro_serializer.rb @@ -3,12 +3,7 @@ class PoroSerializer include Alba::Serializer include Typelizer::DSL - typelize_from :poro - - typelizer_config do |c| - # This is required - c.model_plugin = Typelizer::ModelPlugins::Poro - end + typelize_from Poro attributes :foo, bar: :String end