Skip to content

Commit

Permalink
Implement Decoration mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
aldesantis committed Dec 26, 2016
1 parent 0c91c46 commit 342aebf
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/pragma/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'pragma/operation/base'
require 'pragma/operation/authorization'
require 'pragma/operation/validation'
require 'pragma/operation/decoration'

module Pragma
# Operations provide business logic encapsulation for your JSON API.
Expand Down
1 change: 1 addition & 0 deletions lib/pragma/operation/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def inherited(child)
child.class_eval do
include Authorization
include Validation
include Decoration

before :setup_context
around :handle_halt
Expand Down
52 changes: 52 additions & 0 deletions lib/pragma/operation/decoration.rb
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
1 change: 1 addition & 0 deletions pragma-operation.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Gem::Specification.new do |spec|

spec.add_development_dependency 'pragma-policy', '~> 0.1.0'
spec.add_development_dependency 'pragma-contract', '~> 0.1.0'
spec.add_development_dependency 'pragma-decorator', '~> 0.1.0'

spec.add_development_dependency "bundler"
spec.add_development_dependency "rake"
Expand Down
31 changes: 31 additions & 0 deletions spec/pragma/operation/decoration_spec.rb
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

0 comments on commit 342aebf

Please sign in to comment.