Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/lib/rom/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
require 'rom/plugins/relation/instrumentation'
require 'rom/plugins/command/schema'
require 'rom/plugins/command/timestamps'
require 'rom/plugins/command/alias'
require 'rom/plugins/schema/timestamps'

module ROM
Expand All @@ -45,5 +46,6 @@ module ROM
register :instrumentation, ROM::Plugins::Relation::Instrumentation, type: :relation
register :schema, ROM::Plugins::Command::Schema, type: :command
register :timestamps, ROM::Plugins::Command::Timestamps, type: :command
register :alias, ROM::Plugins::Command::Alias, type: :command
end
end
51 changes: 51 additions & 0 deletions core/lib/rom/plugins/command/alias.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

module ROM
module Plugins
module Command
# A plugin for transparently translating schema defined aliases
# into canonical names expected by adapters.
#
# @example
# class User < ROM::Relations[:sql]
# schema(infer: true) do
# attribute :first_name, alias: name
# end
# end
#
# class CreateUser < ROM::Commands::Create[:sql]
# result :one
# use :alias
# end
#
# result = rom.command(:user).create.call(name: 'Jane')
# result[:first_name] #=> 'Jane'
#
# @api public
module Alias
module T

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/Documentation: Missing top-level module documentation comment.

extend Transproc::Registry

import :rename_keys, from: Transproc::HashTransformations
end

# @api private
def self.included(klass)
super
klass.before :map_aliases
klass.include(InstanceMethods)
end

module InstanceMethods

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/Documentation: Missing top-level module documentation comment.

# @api private
def map_aliases(tuples, *)
mapping = relation.class.schema.alias_mapping.invert
map_input_tuples(tuples) do |t|
T[:rename_keys].(t, mapping)
end
end
end
end
end
end
end
11 changes: 11 additions & 0 deletions core/lib/rom/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,17 @@ def to_ast
[:schema, [name, attributes.map(&:to_ast)]]
end

# Hash from canonical name to alias for aliased attributes
#
# @return [Hash<Symbol, Symbol>]
#
# @api public
def alias_mapping
each_with_object({}) do |attr, obj|
obj[attr.name] = attr.alias if attr.aliased?
end
end

# @api private
def set!(key, value)
instance_variable_set("@#{ key }", value)
Expand Down
36 changes: 36 additions & 0 deletions core/spec/unit/rom/plugins/command/alias_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'rom/command'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.

require 'rom/plugins/command/alias'

RSpec.describe ROM::Plugins::Command::Alias do
include_context 'container'

let(:users) { container.commands.users }
let(:command) { users.create }

before do
configuration.relation :users do
schema(:users) do
attribute :first_name, Types::String, alias: :name
end
end

configuration.commands(:users) do
define :create, type: :create do
result :one
use :alias
end
end
end

it 'accepts input with aliased names' do
result = command.call(name: 'Joe')

expect(result[:first_name]).to eq('Joe')
end

it 'accepts input with canonical names' do
result = command.call(first_name: 'Joe')

expect(result[:first_name]).to eq('Joe')
end
end
12 changes: 12 additions & 0 deletions core/spec/unit/rom/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@
expect(schema.project(:name).primary_key_name).to be(:id)
end
end

describe '#alias_mapping' do
it 'returns a hash from canonical name to alias including aliased attributes' do
attrs = [
define_attr_info(:Integer, name: :id, alias: :pk),
define_attr_info(:String, name: :name)
]
schema = ROM::Schema.define(:name, attributes: attrs)

expect(schema.alias_mapping).to eq(id: :pk)
end
end
end