Skip to content

Commit

Permalink
Command plugin to allow input with aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
waiting-for-dev committed Jan 2, 2019
1 parent 8fd23c5 commit 94036e7
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/lib/rom/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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 @@ -43,5 +44,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 @@
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
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'
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

0 comments on commit 94036e7

Please sign in to comment.