Skip to content

Commit

Permalink
Add an outgoing Processor::Transformer
Browse files Browse the repository at this point in the history
Respective handlers may transform the response
data into any other object.

This will be useful for handlers that perform
serialization, rendering, generating a rack
response etc ...
  • Loading branch information
snusnu committed Jun 26, 2013
1 parent e14a32a commit 383b035
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/substation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module Substation
require 'substation/processor'
require 'substation/processor/evaluator'
require 'substation/processor/wrapper'
require 'substation/processor/transformer'
require 'substation/environment'
require 'substation/environment/dsl'
require 'substation/dispatcher'
23 changes: 23 additions & 0 deletions lib/substation/processor/transformer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Substation
module Processor

# A processor that transforms output data into something else
class Transformer

include Processor::Outgoing

# Transform response data into something else
#
# @param [Response] response
# the response to process
#
# @return [Response]
#
# @api private
def call(response)
respond_with(response, handler.call(response))
end

end # class Wrapper
end # module Processor
end # module Substation
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class Presenter
include Concord.new(:data)
end

class Transformer
def self.call(response)
:transformed
end
end

module Handler

class Evaluator
Expand Down
21 changes: 21 additions & 0 deletions spec/unit/substation/processor/transformer/call_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# encoding: utf-8

require 'spec_helper'

describe Processor::Transformer, '#call' do
subject { object.call(response) }

let(:object) { described_class.new(s_env, Spec::Transformer) }
let(:s_env) { mock }
let(:response) { Response::Success.new(request, output) }
let(:request) { Request.new(name, env, input) }
let(:name) { mock }
let(:env) { mock }
let(:input) { mock }
let(:output) { mock }

let(:transformed) { Response::Success.new(request, data) }
let(:data) { Spec::Transformer.call(response) }

it { should eql(transformed) }
end

0 comments on commit 383b035

Please sign in to comment.