-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c91c46
commit 342aebf
Showing
5 changed files
with
86 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
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,52 @@ | ||
module Pragma | ||
module Operation | ||
# Provides integration with {https://github.com/pragmarb/pragma-decorator Pragma::Decorator}. | ||
# | ||
# @author Alessandro Desantis | ||
module Decoration | ||
def self.included(base) | ||
base.extend ClassMethods | ||
base.include InstanceMethods | ||
end | ||
|
||
module ClassMethods # :nodoc: | ||
# Sets the decorator to use for validating this operation. | ||
# | ||
# @param klass [Class] a subclass of +Pragma::Decorator::Base+ | ||
def decorator(klass) | ||
@decorator = klass | ||
end | ||
|
||
# Builds the decorator for the given resource, using the previously defined decorator class. | ||
# | ||
# Works with both singular resources and collections. | ||
# | ||
# @param resource [Object] | ||
# | ||
# @return [Pragma::Decorator::Base] | ||
# | ||
# @see #decorator | ||
def decorate(resource) | ||
@decorator.represent(resource) | ||
end | ||
end | ||
|
||
module InstanceMethods # :nodoc: | ||
# Builds the decorator for the given resource, using the previously defined decorator class. | ||
# | ||
# This is just an instance-level alias of {.decorate}. You should use this from | ||
# inside the operation. | ||
# | ||
# @param resource [Object] | ||
# | ||
# @return [Pragma::Decorator::Base] | ||
# | ||
# @see .decorator | ||
# @see .decorate | ||
def decorate(resource) | ||
self.class.decorate(resource) | ||
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
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,31 @@ | ||
# frozen_string_literal: true | ||
require 'pragma/decorator' | ||
|
||
RSpec.describe Pragma::Operation::Authorization do | ||
let(:context) { operation.call } | ||
|
||
let(:decorator_klass) do | ||
Class.new(Pragma::Decorator::Base) do | ||
property :title | ||
end | ||
end | ||
|
||
describe '#decorate' do | ||
let(:operation) do | ||
Class.new(Pragma::Operation::Base) do | ||
def call | ||
respond_with status: :ok, resource: decorate(OpenStruct.new( | ||
title: 'Test', | ||
foo: 'bar' | ||
)).to_hash | ||
end | ||
end.tap do |klass| | ||
klass.send(:decorator, decorator_klass) | ||
end | ||
end | ||
|
||
it 'decorates the resource' do | ||
expect(context.resource).to eq('title' => 'Test') | ||
end | ||
end | ||
end |