-
-
Notifications
You must be signed in to change notification settings - Fork 163
Command plugin to accept input with aliased keys #517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6c0aa1d
e4ae2da
d480b5c
22f3c58
8319f13
e5b2a2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| require 'rom/command' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment.
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.