rbs_config 0.1.0 → 0.2.0

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: 61793181f02c7345ef7f22fe57204825aad1575141e9de7b6723c13928a6f25d
4
- data.tar.gz: bc22569815b41ee6b65d8e19816376e0312a074baa3b4997ccdc7dcf9d82368d
3
+ metadata.gz: 8f7dc816262f1365ddfbf97de2aa149271b18080d4de58dd96341a71e025ce02
4
+ data.tar.gz: 6197190426cf582d43b13b315174cefea17c22b0d4c2c7da44b00a1d1d18217f
5
5
  SHA512:
6
- metadata.gz: 4299e76aeb9d945dda9fece882aa8c153813d0f9f916b7f6a18faaf644b58dde066411587b7ad68d9fda64a8fe7682976d8273a5e820a6a1dba340f57cc19811
7
- data.tar.gz: d1d0d938d45bef011eefba72b2a35bfcf452f78e02cfb31669bce0d8a4bbb5ade031c587978960404440aac7ca4de18f1cdcdb88f0cc558e122ee42e8e71e1b4
6
+ metadata.gz: 3d712b3aa4dc185dd994bc8d00f9d8c693dd94cc0c68e0620a8398ff3417b14a741561220bc9665460ba0678118fd1ad779e68ae4d1f3d36eb7f597c7f6727ea
7
+ data.tar.gz: c4366d824d02533718b947b00d8f28f8536efe74fc97c14846731df7a836202eac0913840d4a737a644910cf8536e807386485e69fbc3d3d1e2da551bb9acd16
data/.rubocop.yml CHANGED
@@ -15,6 +15,9 @@ Style/StringLiteralsInInterpolation:
15
15
  Layout/LineLength:
16
16
  Max: 120
17
17
 
18
+ Metrics/AbcSize:
19
+ Max: 20
20
+
18
21
  Metrics/BlockLength:
19
22
  Exclude:
20
23
  - "spec/**/*_spec.rb"
data/README.md CHANGED
@@ -1,24 +1,34 @@
1
1
  # rbs_config
2
2
 
3
- rbs_config is a RBS generator for [config gem](https://github.com/rubyconfig/config).
3
+ rbs_config is a RBS generator for Rails configuration.
4
+
5
+ Now supports the following configuration:
6
+
7
+ * Rails configuration (`Rails.configuration`)
8
+ * [config gem](https://github.com/rubyconfig/config)
4
9
 
5
10
  ## Installation
6
11
 
7
12
  Add a new entry to your Gemfile and run `bundle install`:
8
13
 
9
14
  group :development do
10
- gem 'rbs_config'
15
+ gem 'rbs_config', require: false
11
16
  end
12
17
 
13
18
  After the installation, please run rake task generator:
14
19
 
15
20
  bundle exec rails g rbs_config:install
16
21
 
17
- Additionally, it would be better to add the following entry to your `rbs_collection.yml`:
22
+ And then, please modify `lib/tasks/rbs_config.rake` to fit your application.
23
+ For example, set it up like this if you're using Rails configuration:
24
+
25
+ RbsConfig::RakeTask.new do |task|
26
+ task.type = :rails
27
+ task.mapping = { conf_name: Rails.application.config_for(:conf_name) }
28
+ end
18
29
 
19
- gems:
20
- - name: rbs_config
21
- ignore: true
30
+ Then, rbs_config will generate RBS file for `Rails.application.conf_name` from your
31
+ configuration file.
22
32
 
23
33
  ## Usage
24
34
 
@@ -9,11 +9,17 @@ module RbsConfig
9
9
  begin
10
10
  require 'rbs_config/rake_task'
11
11
  RbsConfig::RakeTask.new do |task|
12
- # The class name of configuration object.
12
+ # The type of configuration (default: :config)
13
+ # task.type = :config | :rails
14
+
15
+ # The class name of configuration object (for :config type)
13
16
  # task.class_name = "Settings"
14
17
 
15
- # The files to be loaded.
18
+ # The files to be loaded (for :config type)
16
19
  # task.files = [Pathname(Rails.root / "config/settings.yml")]
20
+
21
+ # The mapping of configuration (for :rails type)
22
+ # task.mapping = { foo: Rails.application.config_for(:foo) }
17
23
  end
18
24
  rescue LoadError
19
25
  # failed to load rbs_config. Skip to load rbs_config tasks.
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rbs"
4
+ require "active_support/ordered_options"
5
+ require "active_support/core_ext/string/inflections"
6
+
7
+ module RbsConfig
8
+ module RailsConfig
9
+ def self.generate(mapping:)
10
+ Generator.new(mapping: mapping).generate
11
+ end
12
+
13
+ class Generator
14
+ attr_reader :mapping
15
+
16
+ def initialize(mapping:)
17
+ @mapping = mapping
18
+ end
19
+
20
+ def generate
21
+ classes = generate_classes(mapping)
22
+ methods = mapping.map do |key, value|
23
+ "def #{key}: () -> #{stringify_type(key, value)}"
24
+ end
25
+
26
+ format <<~RBS
27
+ module Rails
28
+ class Application
29
+ class Configuration
30
+ #{classes.join("\n")}
31
+ #{methods.join("\n")}
32
+ end
33
+ end
34
+
35
+ def self.configuration: () -> Application::Configuration | ...
36
+ end
37
+ RBS
38
+ end
39
+
40
+ private
41
+
42
+ def format(rbs)
43
+ parsed = RBS::Parser.parse_signature(rbs)
44
+ StringIO.new.tap do |out|
45
+ RBS::Writer.new(out: out).write(parsed[1] + parsed[2])
46
+ end.string
47
+ end
48
+
49
+ def generate_classes(config)
50
+ config.filter_map do |key, value|
51
+ next unless value.is_a?(ActiveSupport::OrderedOptions)
52
+
53
+ classes = generate_classes(value)
54
+ methods = generate_methods(value)
55
+
56
+ <<~RBS
57
+ class #{key.to_s.camelize}
58
+ #{classes.join("\n")}
59
+ #{methods.join("\n")}
60
+ end
61
+ RBS
62
+ end
63
+ end
64
+
65
+ def generate_methods(config)
66
+ case config
67
+ when ActiveSupport::OrderedOptions
68
+ generate_ordered_options_methods(config)
69
+ when Hash
70
+ generate_hash_methods(config)
71
+ end
72
+ end
73
+
74
+ def generate_ordered_options_methods(config)
75
+ methods = config.map do |key, value|
76
+ type = stringify_type(key, value)
77
+ <<~RBS
78
+ def #{key}: () -> #{type}
79
+ def #{key}!: () -> #{type}
80
+ RBS
81
+ end
82
+
83
+ brace_method_type = config.map do |key, value|
84
+ "(:#{key}) -> #{stringify_type(key, value)}"
85
+ end.join(" | ")
86
+
87
+ methods + ["def []: #{brace_method_type}"]
88
+ end
89
+
90
+ def generate_hash_methods(config)
91
+ method_type = config.map do |key, value|
92
+ "(:#{key} | \"#{key}\") -> #{stringify_type(key, value)}"
93
+ end.join(" | ")
94
+ ["def []: #{method_type}"]
95
+ end
96
+
97
+ def stringify_type(name, value) # rubocop:disable Metrics/CyclomaticComplexity
98
+ case value
99
+ when ActiveSupport::OrderedOptions
100
+ name.to_s.camelize
101
+ when Hash
102
+ pairs = value.map do |k, v|
103
+ "#{k}: #{stringify_type(k, v)}"
104
+ end
105
+ "{ #{pairs.join(", ")} }"
106
+ when Array
107
+ types = value.map { |v| stringify_type(name, v) }.uniq
108
+ "Array[#{types.join(" | ")}]"
109
+ when NilClass
110
+ "nil"
111
+ when TrueClass, FalseClass
112
+ "bool"
113
+ else
114
+ value.class.to_s
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
@@ -5,16 +5,18 @@ require "rake/tasklib"
5
5
 
6
6
  module RbsConfig
7
7
  class RakeTask < Rake::TaskLib
8
- attr_accessor :class_name, :files, :name, :signature_root_dir
8
+ attr_accessor :type, :class_name, :files, :mapping, :name, :signature_root_dir
9
9
 
10
10
  def initialize(name = :'rbs:config', &block)
11
11
  super()
12
12
 
13
13
  @name = name
14
+ @type = :config
14
15
  @class_name = "Settings"
15
16
  @files = [Pathname(Rails.root / "config/settings.yml"),
16
17
  Pathname(Rails.root / "config/settings.local.yml"),
17
18
  Pathname(Rails.root / "config/settings/production.yml")]
19
+ @mapping = {}
18
20
  @signature_root_dir = Pathname(Rails.root / "sig/config")
19
21
 
20
22
  block&.call(self)
@@ -34,11 +36,17 @@ module RbsConfig
34
36
  def define_generate_task
35
37
  desc "Generate RBS files for config gem"
36
38
  task "#{name}:generate" do
39
+ require "rbs_config" # load RbsConfig lazily
40
+
37
41
  signature_root_dir.mkpath
38
42
 
39
- signature_root_dir.join("#{class_name.underscore}.rbs").write(
40
- RbsConfig::Config.generate(files: files, class_name: class_name)
41
- )
43
+ rbs = if type == :config
44
+ RbsConfig::Config.generate(files: files, class_name: class_name)
45
+ else
46
+ RbsConfig::RailsConfig.generate(mapping: mapping)
47
+ end
48
+
49
+ signature_root_dir.join("#{class_name.underscore}.rbs").write(rbs)
42
50
  end
43
51
  end
44
52
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RbsConfig
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/rbs_config.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "rbs_config/config"
4
+ require_relative "rbs_config/rails_config"
4
5
  require_relative "rbs_config/version"
5
6
 
6
7
  module RbsConfig
@@ -0,0 +1,21 @@
1
+ module RbsConfig
2
+ module RailsConfig
3
+ def self.generate: (mapping: Hash[untyped, Hash[untyped, untyped] | ActiveSupport::OrderedOptions[untyped, untyped]]) -> String
4
+
5
+ class Generator
6
+ attr_reader mapping: Hash[untyped, Hash[untyped, untyped] | ActiveSupport::OrderedOptions[untyped, untyped]]
7
+
8
+ def initialize: (mapping: Hash[untyped, Hash[untyped, untyped] | ActiveSupport::OrderedOptions[untyped, untyped]]) -> void
9
+ def generate: () -> String
10
+
11
+ private
12
+
13
+ def format: (String rbs) -> String
14
+ def generate_classes: (Hash[untyped, untyped] | ActiveSupport::OrderedOptions[untyped, untyped] config) -> Array[String]
15
+ def generate_methods: (Hash[untyped, untyped] | ActiveSupport::OrderedOptions[untyped, untyped] config) -> Array[string]
16
+ def generate_ordered_options_methods: (ActiveSupport::OrderedOptions[untyped, untyped] config) -> Array[string]
17
+ def generate_hash_methods: (Hash[untyped, untyped]) -> Array[string]
18
+ def stringify_type: (untyped name, untyped value) -> String
19
+ end
20
+ end
21
+ end
@@ -1,5 +1,6 @@
1
1
  module RbsConfig
2
2
  class RakeTask < Rake::TaskLib
3
+ attr_accessor type: :config | :rails
3
4
  attr_accessor class_name: String
4
5
  attr_accessor files: Array[Pathname]
5
6
  attr_accessor name: Symbol
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbs_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takeshi KOMIYA
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-23 00:00:00.000000000 Z
11
+ date: 2023-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -56,6 +56,7 @@ files:
56
56
  - lib/generators/rbs_config/install_generator.rb
57
57
  - lib/rbs_config.rb
58
58
  - lib/rbs_config/config.rb
59
+ - lib/rbs_config/rails_config.rb
59
60
  - lib/rbs_config/rake_task.rb
60
61
  - lib/rbs_config/version.rb
61
62
  - rbs_collection.lock.yaml
@@ -63,6 +64,7 @@ files:
63
64
  - rbs_config.gemspec
64
65
  - sig/rbs_config.rbs
65
66
  - sig/rbs_config/config.rbs
67
+ - sig/rbs_config/rails_config.rbs
66
68
  - sig/rbs_config/rake_task.rbs
67
69
  - sig/shims/rake.rbs
68
70
  homepage: https://github.com/tk0miya/rbs_config