Skip to content

Commit b5187be

Browse files
committed
refactor: configuration is now a reusable class instead of a singleton
1 parent 2e8fc70 commit b5187be

File tree

3 files changed

+70
-28
lines changed

3 files changed

+70
-28
lines changed

bin/console

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#!/usr/bin/env ruby
22

3-
require "bundler/setup"
4-
require "inertia_rails/rails"
5-
6-
# You can add fixtures and/or initialization code here to make experimenting
7-
# with your gem easier. You can also use a different console, if you like.
3+
require "pathname"
4+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
5+
Pathname.new(__FILE__).realpath)
86

9-
# (If you use this, don't forget to add pry to your Gemfile!)
10-
# require "pry"
11-
# Pry.start
7+
require "rubygems"
8+
require "bundler/setup"
9+
require "rails/all"
10+
require "inertia_rails"
1211

1312
require "irb"
1413
IRB.start(__FILE__)

lib/inertia_rails/configuration.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
3+
module InertiaRails
4+
class Configuration
5+
DEFAULTS = {
6+
default_render: false,
7+
layout: nil,
8+
deep_merge_shared_data: false,
9+
merge_props_with: ->(shared_data, props) { shared_data.merge(props) },
10+
ssr_enabled: false,
11+
ssr_url: 'http://localhost:13714',
12+
version: nil,
13+
}.freeze
14+
15+
OPTION_NAMES = DEFAULTS.keys.freeze
16+
17+
protected attr_reader :options
18+
19+
def initialize(**attrs)
20+
@options = attrs.extract!(*OPTION_NAMES)
21+
22+
unless attrs.empty?
23+
raise ArgumentError, "Unknown options for #{self.class}: #{attrs.keys}"
24+
end
25+
end
26+
27+
def to_h
28+
@options.to_h
29+
end
30+
31+
def merge(config)
32+
Configuration.new(**@options.merge(config.options))
33+
end
34+
35+
OPTION_NAMES.each do |option|
36+
define_method(option) {
37+
value = @options[option]
38+
value.respond_to?(:call) ? value.call : value
39+
}
40+
define_method("#{option}=") { |value|
41+
@options[option] = value
42+
}
43+
end
44+
45+
def self.default
46+
new(**DEFAULTS)
47+
end
48+
end
49+
end

lib/inertia_rails/inertia_rails.rb

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
# Needed for `thread_mattr_accessor`
22
require 'active_support/core_ext/module/attribute_accessors_per_thread'
33
require 'inertia_rails/lazy'
4+
require 'inertia_rails/configuration'
45

56
module InertiaRails
67
thread_mattr_accessor :threadsafe_shared_plain_data
78
thread_mattr_accessor :threadsafe_shared_blocks
89
thread_mattr_accessor :threadsafe_html_headers
910

11+
CONFIGURATION = Configuration.default
12+
1013
def self.configure
11-
yield(Configuration)
14+
yield(CONFIGURATION)
15+
end
16+
17+
def self.configuration
18+
CONFIGURATION
1219
end
1320

1421
# "Getters"
@@ -18,31 +25,31 @@ def self.shared_data(controller)
1825
end
1926

2027
def self.version
21-
Configuration.evaluated_version
28+
configuration.version
2229
end
2330

2431
def self.layout
25-
Configuration.layout
32+
configuration.layout
2633
end
2734

2835
def self.ssr_enabled?
29-
Configuration.ssr_enabled
36+
configuration.ssr_enabled
3037
end
3138

3239
def self.ssr_url
33-
Configuration.ssr_url
40+
configuration.ssr_url
3441
end
3542

3643
def self.default_render?
37-
Configuration.default_render
44+
configuration.default_render
3845
end
3946

4047
def self.html_headers
4148
self.threadsafe_html_headers || []
4249
end
4350

4451
def self.deep_merge_shared_data?
45-
Configuration.deep_merge_shared_data
52+
configuration.deep_merge_shared_data
4653
end
4754

4855
# "Setters"
@@ -70,19 +77,6 @@ def self.lazy(value = nil, &block)
7077

7178
private
7279

73-
module Configuration
74-
mattr_accessor(:layout) { nil }
75-
mattr_accessor(:version) { nil }
76-
mattr_accessor(:ssr_enabled) { false }
77-
mattr_accessor(:ssr_url) { 'http://localhost:13714' }
78-
mattr_accessor(:default_render) { false }
79-
mattr_accessor(:deep_merge_shared_data) { false }
80-
81-
def self.evaluated_version
82-
self.version.respond_to?(:call) ? self.version.call : self.version
83-
end
84-
end
85-
8680
# Getters and setters to provide default values for the threadsafe attributes
8781
def self.shared_plain_data
8882
self.threadsafe_shared_plain_data || {}

0 commit comments

Comments
 (0)