Skip to content

Commit 539a85b

Browse files
committed
Allow to configure custom inflector
WIP but registration already works
1 parent 1861141 commit 539a85b

File tree

20 files changed

+149
-29
lines changed

20 files changed

+149
-29
lines changed

core/lib/rom/associations/through_identifier.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def self.[](source, target, assoc_name = nil)
2222

2323
# @api private
2424
def self.default_assoc_name(relation)
25-
Inflector.singularize(relation).to_sym
25+
relation.inflector.singularize(relation).to_sym
2626
end
2727

2828
# @api private

core/lib/rom/command_compiler.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ def self.registry
6666
# @return [Cache] local cache instance
6767
option :cache, default: -> { Cache.new }
6868

69+
# @!attribute [r] inflector
70+
# @return [Dry::Inflector] String inflector
71+
# @api private
72+
option :inflector, default: -> { Inflector }
73+
6974
# Return a specific command type for a given adapter and relation AST
7075
#
7176
# This class holds its own registry where all generated commands are being
@@ -114,7 +119,7 @@ def call(*args)
114119

115120
# @api private
116121
def type
117-
@_type ||= Commands.const_get(Inflector.classify(id))[adapter]
122+
@_type ||= Commands.const_get(inflector.classify(id))[adapter]
118123
rescue NameError
119124
nil
120125
end
@@ -139,7 +144,7 @@ def visit_relation(node, parent_relation = nil)
139144
if meta[:combine_type] == :many
140145
name
141146
else
142-
{ Inflector.singularize(name).to_sym => name }
147+
{ inflector.singularize(name).to_sym => name }
143148
end
144149

145150
mapping =
@@ -239,7 +244,7 @@ def setup_associates(klass, relation, _meta, parent_relation)
239244
if relation.associations.key?(parent_relation)
240245
parent_relation
241246
else
242-
singular_name = Inflector.singularize(parent_relation).to_sym
247+
singular_name = inflector.singularize(parent_relation).to_sym
243248
singular_name if relation.associations.key?(singular_name)
244249
end
245250

core/lib/rom/command_proxy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class CommandProxy
1111
attr_reader :command, :root
1212

1313
# @api private
14-
def initialize(command, root = Inflector.singularize(command.name.relation).to_sym)
14+
def initialize(command, inflector: nil, root: inflector.singularize(command.name.relation).to_sym)
1515
@command = command
1616
@root = root
1717
end

core/lib/rom/configuration.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require 'rom/setup'
77
require 'rom/configuration_dsl'
88
require 'rom/support/notifications'
9+
require 'rom/support/inflector'
910

1011
module ROM
1112
class Configuration
@@ -38,7 +39,7 @@ class Configuration
3839

3940
def_delegators :@setup, :register_relation, :register_command, :register_mapper, :register_plugin,
4041
:command_classes, :mapper_classes,
41-
:auto_registration
42+
:auto_registration, :inflector, :inflector=
4243

4344
def_delegators :@environment, :gateways, :gateways_map, :configure, :config
4445

core/lib/rom/configuration_dsl/command.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ class Command
1616
# @api private
1717
def self.build_class(name, relation, options = EMPTY_HASH, &block)
1818
type = options.fetch(:type) { name }
19-
command_type = Inflector.classify(type)
19+
inflector = options.fetch(:inflector) { Inflector }
20+
command_type = inflector.classify(type)
2021
adapter = options.fetch(:adapter)
2122
parent = ROM::Command.adapter_namespace(adapter).const_get(command_type)
22-
class_name = generate_class_name(adapter, command_type, relation)
23+
class_name = generate_class_name(adapter, command_type, relation, inflector)
2324

2425
Dry::Core::ClassBuilder.new(name: class_name, parent: parent).call do |klass|
2526
klass.register_as(name)
@@ -31,11 +32,11 @@ def self.build_class(name, relation, options = EMPTY_HASH, &block)
3132
# Create a command subclass name based on adapter, type and relation
3233
#
3334
# @api private
34-
def self.generate_class_name(adapter, command_type, relation)
35+
def self.generate_class_name(adapter, command_type, relation, inflector)
3536
pieces = ['ROM']
36-
pieces << Inflector.classify(adapter)
37+
pieces << inflector.classify(adapter)
3738
pieces << 'Commands'
38-
pieces << "#{command_type}[#{Inflector.classify(relation)}s]"
39+
pieces << "#{command_type}[#{inflector.classify(relation)}s]"
3940
pieces.join('::')
4041
end
4142
end

core/lib/rom/configuration_dsl/relation.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class Relation
1515
#
1616
# @api private
1717
def self.build_class(name, options = EMPTY_HASH)
18-
class_name = "ROM::Relation[#{Inflector.camelize(name)}]"
18+
inflector = options.fetch(:inflector) { Inflector }
19+
class_name = "ROM::Relation[#{inflector.camelize(name)}]"
1920
adapter = options.fetch(:adapter)
2021

2122
Dry::Core::ClassBuilder.new(name: class_name, parent: ROM::Relation[adapter]).call do |klass|

core/lib/rom/relation.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require 'rom/constants'
77
require 'rom/initializer'
88
require 'rom/support/memoizable'
9+
require 'rom/support/inflector'
910

1011
require 'rom/relation/class_interface'
1112

@@ -189,6 +190,11 @@ class Relation
189190
# @api private
190191
option :meta, reader: true, default: -> { EMPTY_HASH }
191192

193+
# @!attribute [r] inflector
194+
# @return [Dry::Inflector] String inflector
195+
# @api private
196+
option :inflector, reader: true, default: -> { Inflector }
197+
192198
# Return schema attribute
193199
#
194200
# @example accessing canonical attribute
@@ -596,7 +602,7 @@ def foreign_key(name)
596602
if attr
597603
attr.name
598604
else
599-
:"#{Inflector.singularize(name.dataset)}_id"
605+
:"#{inflector.singularize(name.dataset)}_id"
600606
end
601607
end
602608

core/lib/rom/schema/associations_dsl.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,23 @@ class Schema
1111
# This DSL is exposed in `associations do .. end` blocks in schema defintions.
1212
#
1313
# @api public
14-
class AssociationsDSL < BasicObject
14+
class AssociationsDSL < ::BasicObject
1515
# @!attribute [r] source
1616
# @return [Relation::Name] The source relation
1717
attr_reader :source
1818

19+
# @!attribute [r] inflector
20+
# @return [Dry::Inflector] String inflector
21+
attr_reader :inflector
22+
1923
# @!attribute [r] registry
2024
# @return [RelationRegistry] Relations registry from a rom container
2125
attr_reader :registry
2226

2327
# @api private
24-
def initialize(source, &block)
28+
def initialize(source, inflector = ::ROM::Inflector, &block)
2529
@source = source
30+
@inflector = inflector
2631
@registry = {}
2732
instance_exec(&block)
2833
end
@@ -199,7 +204,7 @@ def add(association)
199204

200205
# @api private
201206
def dataset_name(name)
202-
Inflector.pluralize(name).to_sym
207+
inflector.pluralize(name).to_sym
203208
end
204209
end
205210
end

core/lib/rom/setup.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ class Setup
2828
# @api private
2929
attr_reader :notifications
3030

31+
# @api private
32+
attr_accessor :inflector
33+
3134
# @api private
3235
def initialize(notifications)
3336
@relation_classes = []
3437
@command_classes = []
3538
@mapper_classes = []
3639
@plugins = []
3740
@notifications = notifications
41+
@inflector = Inflector
3842
end
3943

4044
# Enable auto-registration for a given setup object
@@ -47,7 +51,7 @@ def initialize(notifications)
4751
#
4852
# @api public
4953
def auto_registration(directory, **options)
50-
auto_registration = AutoRegistration.new(directory, **options)
54+
auto_registration = AutoRegistration.new(directory, inflector: inflector, **options)
5155
auto_registration.relations.map { |r| register_relation(r) }
5256
auto_registration.commands.map { |r| register_command(r) }
5357
auto_registration.mappers.map { |r| register_mapper(r) }
@@ -58,21 +62,21 @@ def auto_registration(directory, **options)
5862
#
5963
# @api private
6064
def register_relation(*klasses)
61-
klasses.reduce(@relation_classes, :<<)
65+
@relation_classes.concat(klasses)
6266
end
6367

6468
# Mapper sub-classes are being registered with this method during setup
6569
#
6670
# @api private
6771
def register_mapper(*klasses)
68-
klasses.reduce(@mapper_classes, :<<)
72+
@mapper_classes.concat(klasses)
6973
end
7074

7175
# Command sub-classes are being registered with this method during setup
7276
#
7377
# @api private
7478
def register_command(*klasses)
75-
klasses.reduce(@command_classes, :<<)
79+
@command_classes.concat(klasses)
7680
end
7781

7882
# @api private

core/lib/rom/setup/auto_registration.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class AutoRegistration
2121

2222
PathnameType = Types.Constructor(Pathname, &Kernel.method(:Pathname))
2323

24+
InflectorType = Types.Strict(Dry::Inflector)
25+
2426
DEFAULT_MAPPING = {
2527
relations: :relations,
2628
mappers: :mappers,
@@ -51,6 +53,11 @@ class AutoRegistration
5153
]
5254
}
5355

56+
# @!attribute [r] inflector
57+
# @return [Dry::Inflector] String inflector
58+
# @api private
59+
option :inflector, type: InflectorType, default: -> { Inflector }
60+
5461
# Load relation files
5562
#
5663
# @api private
@@ -84,19 +91,25 @@ def load_entities(entity)
8491
case namespace
8592
when String
8693
AutoRegistrationStrategies::CustomNamespace.new(
87-
namespace: namespace, file: file, directory: directory
94+
namespace: namespace,
95+
file: file,
96+
directory: directory,
97+
inflector: inflector
8898
).call
8999
when TrueClass
90100
AutoRegistrationStrategies::WithNamespace.new(
91-
file: file, directory: directory
101+
file: file, directory: directory, inflector: inflector
92102
).call
93103
when FalseClass
94104
AutoRegistrationStrategies::NoNamespace.new(
95-
file: file, directory: directory, entity: component_dirs.fetch(entity)
105+
file: file,
106+
directory: directory,
107+
entity: component_dirs.fetch(entity),
108+
inflector: inflector
96109
).call
97110
end
98111

99-
Inflector.constantize(klass_name)
112+
inflector.constantize(klass_name)
100113
end
101114
end
102115
end

0 commit comments

Comments
 (0)