-
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Command plugin to allow input with aliases
- Loading branch information
1 parent
8fd23c5
commit 94036e7
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'set' | ||
|
||
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, Types::String.meta(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 | ||
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 | ||
# @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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'rom/command' | ||
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.meta(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 |