Skip to content

Commit

Permalink
Pass inflector around
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-gordon committed Mar 9, 2020
1 parent 34b5a9f commit ad4ac13
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 59 deletions.
7 changes: 6 additions & 1 deletion core/lib/rom/associations/definitions/abstract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ def self.process_options(target, options)
through = options[:through]

if through
options[:through] = ThroughIdentifier[through, target.relation, options[:assoc]]
options[:through] = ThroughIdentifier[
through,
target.relation,
options[:assoc],
**options.slice(:inflector)
]
end

options[:name] = target.relation
Expand Down
8 changes: 4 additions & 4 deletions core/lib/rom/associations/through_identifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class ThroughIdentifier
attr_reader :assoc_name

# @api private
def self.[](source, target, assoc_name = nil)
new(source, target, assoc_name || default_assoc_name(target))
def self.[](source, target, assoc_name = nil, inflector: Inflector)
new(source, target, assoc_name || default_assoc_name(target, inflector: inflector))
end

# @api private
def self.default_assoc_name(relation)
relation.inflector.singularize(relation).to_sym
def self.default_assoc_name(relation, inflector:)
inflector.singularize(relation).to_sym
end

# @api private
Expand Down
5 changes: 3 additions & 2 deletions core/lib/rom/command_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ def call(*args)
command = ROM::Commands::Graph.build(registry, graph_opts)

if command.graph?
CommandProxy.new(command)
root = inflector.singularize(command.name.relation).to_sym
CommandProxy.new(command, root)
elsif command.lazy?
command.unwrap
else
Expand Down Expand Up @@ -195,7 +196,7 @@ def visit_attribute(*_args)
def register_command(rel_name, type, rel_meta, parent_relation = nil)
relation = relations[rel_name]

type.create_class(rel_name, type) do |klass|
type.create_class(rel_name, type, inflector: inflector) do |klass|
klass.result(rel_meta.fetch(:combine_type, result))

meta.each do |name, value|
Expand Down
8 changes: 5 additions & 3 deletions core/lib/rom/command_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ module ROM
#
# @api private
class CommandProxy
attr_reader :command, :root
attr_reader :command

attr_reader :root

# @api private
def initialize(command, inflector: nil, root: inflector.singularize(command.name.relation).to_sym)
def initialize(command, root)
@command = command
@root = root
end
Expand All @@ -23,7 +25,7 @@ def call(input)

# @api private
def >>(other)
self.class.new(command >> other)
self.class.new(command >> other, root)
end

# @api private
Expand Down
4 changes: 2 additions & 2 deletions core/lib/rom/commands/class_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ def build(relation, **options)
# @return [Class, Object]
#
# @api public
def create_class(name, type, &block)
def create_class(name, type, inflector: Inflector, &block)
klass = Dry::Core::ClassBuilder
.new(name: "#{Inflector.classify(type)}[:#{name}]", parent: type)
.new(name: "#{inflector.classify(type)}[:#{name}]", parent: type)
.call

if block
Expand Down
3 changes: 2 additions & 1 deletion core/lib/rom/create_container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def finalize(environment, setup)
mappers: setup.mapper_classes,
plugins: setup.plugins,
notifications: setup.notifications,
config: environment.config.dup.freeze
config: environment.config.dup.freeze,
inflector: setup.inflector
)

finalize.run!
Expand Down
14 changes: 7 additions & 7 deletions core/lib/rom/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,22 @@ class Relation
# @api public
param :dataset

# @!attribute [r] inflector
# @return [Dry::Inflector] String inflector
# @api private
option :inflector, reader: true, default: -> { Inflector }

# @!attribute [r] schema
# @return [Schema] relation schema, defaults to class-level canonical
# schema (if it was defined) and sets an empty one as
# the fallback
# @api public
option :schema, default: -> { self.class.schema || self.class.default_schema }
option :schema, default: -> { self.class.schema || self.class.default_schema(inflector: inflector) }

# @!attribute [r] name
# @return [Object] The relation name
# @api public
option :name, default: -> { self.class.schema ? self.class.schema.name : self.class.default_name }
option :name, default: -> { self.class.schema ? self.class.schema.name : self.class.default_name(inflector) }

# @!attribute [r] input_schema
# @return [Object#[]] tuple processing function, uses schema or defaults to Hash[]
Expand Down Expand Up @@ -190,11 +195,6 @@ class Relation
# @api private
option :meta, reader: true, default: -> { EMPTY_HASH }

# @!attribute [r] inflector
# @return [Dry::Inflector] String inflector
# @api private
option :inflector, reader: true, default: -> { Inflector }

# Return schema attribute
#
# @example accessing canonical attribute
Expand Down
12 changes: 6 additions & 6 deletions core/lib/rom/relation/class_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ def schema(dataset = nil, as: nil, infer: false, &block)

@relation_name = Name[relation, ds_name]

@schema_proc = proc do |*args, &inner_block|
@schema_proc = proc do |**kwargs, &inner_block|
schema_dsl.new(
relation_name,
schema_class: schema_class,
attr_class: schema_attr_class,
inferrer: schema_inferrer.with(enabled: infer),
&block
).call(*args, &inner_block)
).call(**kwargs, &inner_block)
end
end
end
Expand Down Expand Up @@ -294,15 +294,15 @@ def schemas
# @return [Name]
#
# @api private
def default_name
Name[Inflector.underscore(name).tr('/', '_').to_sym]
def default_name(inflector = Inflector)
Name[inflector.underscore(name).tr('/', '_').to_sym]
end

# @api private
def default_schema(klass = self)
def default_schema(klass = self, inflector: Inflector)
klass.schema ||
if klass.schema_proc
klass.set_schema!(klass.schema_proc.call)
klass.set_schema!(klass.schema_proc.(inflector: inflector))
else
klass.schema_class.define(klass.default_name)
end
Expand Down
24 changes: 17 additions & 7 deletions core/lib/rom/schema/associations_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ class Schema
#
# @api public
class AssociationsDSL < ::BasicObject
class << self
define_method(:const_missing, ::Object.method(:const_get))
end

include Associations::Definitions

# @!attribute [r] source
# @return [Relation::Name] The source relation
attr_reader :source
Expand All @@ -25,7 +31,7 @@ class AssociationsDSL < ::BasicObject
attr_reader :registry

# @api private
def initialize(source, inflector = ::ROM::Inflector, &block)
def initialize(source, inflector = Inflector, &block)
@source = source
@inflector = inflector
@registry = {}
Expand Down Expand Up @@ -66,9 +72,9 @@ def initialize(source, inflector = ::ROM::Inflector, &block)
# @api public
def one_to_many(target, **options)
if options[:through]
many_to_many(target, **options)
many_to_many(target, **options, inflector: inflector)
else
add(::ROM::Associations::Definitions::OneToMany.new(source, target, **options))
add(build(OneToMany, target, options))
end
end
alias_method :has_many, :one_to_many
Expand All @@ -93,7 +99,7 @@ def one_to_one(target, **options)
if options[:through]
one_to_one_through(target, **options)
else
add(::ROM::Associations::Definitions::OneToOne.new(source, target, **options))
add(build(OneToOne, target, options))
end
end

Expand All @@ -106,7 +112,7 @@ def one_to_one(target, **options)
#
# @api public
def one_to_one_through(target, **options)
add(::ROM::Associations::Definitions::OneToOneThrough.new(source, target, **options))
add(build(OneToOneThrough, target, options))
end

# Establish a many-to-many association
Expand All @@ -123,7 +129,7 @@ def one_to_one_through(target, **options)
#
# @api public
def many_to_many(target, **options)
add(::ROM::Associations::Definitions::ManyToMany.new(source, target, **options))
add(build(ManyToMany, target, options))
end

# Establish a many-to-one association
Expand All @@ -140,7 +146,7 @@ def many_to_many(target, **options)
#
# @api public
def many_to_one(target, **options)
add(::ROM::Associations::Definitions::ManyToOne.new(source, target, **options))
add(build(ManyToOne, target, options))
end

# Shortcut for many_to_one which sets alias automatically
Expand Down Expand Up @@ -188,6 +194,10 @@ def call

private

def build(definition, target, options)
definition.new(source, target, **options, inflector: inflector)
end

# @api private
def add(association)
key = association.as || association.name
Expand Down
19 changes: 16 additions & 3 deletions core/lib/rom/schema/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'rom/types'
require 'rom/attribute'
require 'rom/schema/associations_dsl'
require 'rom/support/inflector'

module ROM
class Schema
Expand Down Expand Up @@ -56,6 +57,11 @@ class DSL < BasicObject
# @return [AssociationDSL] Associations defined within a block
attr_reader :associations_dsl

# @!attribute [r] inflector
# @return [Dry::Inflector] String inflector
# @api private
attr_reader :inflector

# @api private
def initialize(*, &block)
super
Expand Down Expand Up @@ -115,7 +121,7 @@ def attribute(name, type_or_options, options = EMPTY_HASH)
#
# @api public
def associations(&block)
@associations_dsl = AssociationsDSL.new(relation, &block)
@associations_dsl = AssociationsDSL.new(relation, inflector, &block)
end

# Builds a representation of the information needed to create an
Expand Down Expand Up @@ -181,7 +187,9 @@ def app_plugin(plugin, options = ::ROM::EMPTY_HASH)
end

# @api private
def call(&block)
def call(inflector: Inflector, &block)
@inflector = inflector

instance_exec(&block) if block
instance_exec(&definition) if definition

Expand All @@ -205,7 +213,12 @@ def plugin_options(plugin)
#
# @api private
def opts
opts = { attributes: attributes.values, inferrer: inferrer, attr_class: attr_class }
opts = {
attributes: attributes.values,
inferrer: inferrer,
attr_class: attr_class,
inflector: Inflector
}

if associations_dsl
{ **opts, associations: associations_dsl.call }
Expand Down
14 changes: 11 additions & 3 deletions core/lib/rom/setup/finalize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ module ROM
#
# @private
class Finalize
attr_reader :gateways, :repo_adapter,
attr_reader :gateways, :repo_adapter, :inflector,
:relation_classes, :mapper_classes, :mapper_objects,
:command_classes, :plugins, :config, :notifications

# @api private
def initialize(options)
@gateways = options.fetch(:gateways)
@inflector = options.fetch(:inflector)

@relation_classes = options.fetch(:relation_classes)
@command_classes = options.fetch(:command_classes)
Expand Down Expand Up @@ -84,7 +85,8 @@ def load_relations(mappers)
relation_classes,
mappers: mappers,
plugins: global_plugins,
notifications: notifications
notifications: notifications,
inflector: inflector
).run!
end

Expand All @@ -99,7 +101,13 @@ def load_mappers
#
# @api private
def load_commands(relations)
FinalizeCommands.new(relations, gateways, command_classes, notifications).run!
FinalizeCommands.new(
relations,
gateways,
command_classes,
notifications: notifications,
inflector: inflector
).run!
end
end
end
15 changes: 12 additions & 3 deletions core/lib/rom/setup/finalize/finalize_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ class Finalize
class FinalizeCommands
attr_reader :notifications

attr_reader :inflector

# Build command registry hash for provided relations
#
# @param [RelationRegistry] relations registry
# @param [Hash] gateways
# @param [Array] command_classes a list of command subclasses
#
# @api private
def initialize(relations, gateways, command_classes, notifications)
def initialize(relations, gateways, command_classes, **options)
@relations = relations
@gateways = gateways
@command_classes = command_classes
@notifications = notifications
@inflector = options.fetch(:inflector, Inflector)
@notifications = options.fetch(:notifications)
end

# @return [Hash]
Expand All @@ -42,7 +45,13 @@ def run!
end

registry = Registry.new
compiler = CommandCompiler.new(@gateways, @relations, registry, notifications)
compiler = CommandCompiler.new(
@gateways,
@relations,
registry,
notifications,
inflector: inflector
)

@relations.each do |(name, relation)|
rel_commands = commands.select { |c| c.relation.name == relation.name }
Expand Down
Loading

0 comments on commit ad4ac13

Please sign in to comment.