Skip to content
Permalink
Browse files

Move aliases handling from WithReducer to Context

  • Loading branch information...
jpmoral committed Jul 8, 2015
1 parent af185be commit 4aa0cfa102ee16372f408ef22c2c9b6c2f8ae449
@@ -69,7 +69,6 @@ def create_action_context(context)
def all_keys
expected_keys + promised_keys
end

end

end
@@ -19,8 +19,12 @@ def self.make(context={})
raise ArgumentError, 'Argument must be Hash or LightService::Context'
end

return context if context.is_a?(Context)
self.new(context)
unless context.is_a?(Context)
context = self.new(context)
end

context.set_aliases(context.delete(:_aliases)) if context[:_aliases]
context
end

def add_to_context(values)
@@ -87,5 +91,25 @@ def define_accessor_methods_for_keys(keys)
define_singleton_method("#{key}=") { |value| self[key] = value }
end
end

def set_aliases(aliases)
@aliases = aliases

aliases.each_pair do |key, key_alias|
self[key_alias] = self[key]
end
end

def aliases
@aliases ||= {}
end

def [](key)
super(key) || super(aliases.key(key))
end

def fetch(key, default_or_block = nil)
self[key] ||= super(key, default_or_block)
end
end
end
@@ -13,7 +13,8 @@ def self.included(base_class)
# In case this module is included
module ClassMethods
def with(data)
WithReducerFactory.make(self).with(data, @aliases)
data.merge!(:_aliases => @aliases) if @aliases
WithReducerFactory.make(self).with(data)
end

def reduce(*actions)
@@ -2,9 +2,8 @@ module LightService; module Organizer
class WithReducer
attr_reader :context

def with(data = {}, aliases = nil)
def with(data = {})
@context = LightService::Context.make(data)
@aliases = aliases
self
end

@@ -14,7 +13,6 @@ def reduce(*actions)

actions.reduce(context) do |current_context, action|
begin
put_aliases_in_the_context(current_context) if @aliases
result = action.execute(current_context)
rescue FailWithRollbackError
result = reduce_rollback(actions)
@@ -48,10 +46,5 @@ def reversable_actions(actions)
actions.take(index_of_current_action + 1)
end

def put_aliases_in_the_context(context)
@aliases.each_pair do |key, key_alias|
context[key_alias] = context[key]
end
end
end
end; end
@@ -10,10 +10,10 @@ def initialize(decorated = WithReducer.new, organizer)
@logged = false
end

def with(data = {}, aliases = nil)
def with(data = {})
logger.info("[LightService] - calling organizer <#{organizer.to_s}>")

decorated.with(data, aliases)
decorated.with(data)

logger.info("[LightService] - keys in context: #{extract_keys(decorated.context.keys)}")
self
@@ -151,4 +151,25 @@

expect(context.outcome).to eq(::LightService::Outcomes::SUCCESS)
end

context "when aliases are included via .make" do
let(:context) do
LightService::Context.make(
:foo => "foobar",
:_aliases => aliases,
)
end
let(:aliases) { { :foo => :bar } }

it "contains the aliases" do
expect(context.aliases).to eq(aliases)
expect(context).to include(:foo, :bar)
end

it "returns the correct values for #[] and #fetch" do
expect(context[:bar]).to eq context[:foo]
expect(context.fetch(:bar)).to eq context[:foo]
end
end

end
@@ -41,28 +41,4 @@
expect(result).to eq(context)
end
end

context "when aliases are given" do
it "puts the aliased value in the context with the aliasing key" do
data = {
foo: "foo"
}
aliases = {
foo: :alias_of_foo
}
aliased_pair = {
alias_of_foo: "foo"
}

expect(action1).to receive(:execute) \
.with(hash_including(aliased_pair)) \
.and_return(data)

expect(action2).to receive(:execute) \
.with(hash_including(aliased_pair)) \
.and_return(data)

described_class.new.with(data, aliases).reduce(actions)
end
end
end
@@ -6,7 +6,7 @@
Class.new do
extend LightService::Organizer

aliases promised_key: :expected_key
aliases :promised_key => :expected_key

def self.do_something(ctx={})
with(ctx).reduce([
@@ -20,7 +20,8 @@ def self.do_something(ctx={})
context "when aliases is invoked" do
it "makes aliases available to the actions" do
result = organizer_with_alias.do_something
expect(result[:promised_key]).to eq result[:expected_key]
expect(result[:expected_key]).to eq(result[:promised_key])
expect(result.expected_key).to eq(result[:promised_key])
end
end
end
@@ -80,4 +80,31 @@
end
end

context "when aliases are declared" do
let(:organizer) do
Class.new do
extend LightService::Organizer
aliases :foo => :bar

def self.do_stuff
reduce(TestDoubles::AnAction)
end
end
end

it "merges the aliases into the data" do
with_reducer = double(reduce: true)

allow(described_class::WithReducerFactory).to receive(:make).
and_return(with_reducer)

expect(with_reducer).to \
receive(:with).
with(hash_including(:_aliases => { :foo => :bar })).
and_return(with_reducer)

organizer.do_stuff
end
end

end
@@ -5,5 +5,6 @@
require 'ostruct'
require 'rspec/its'
require 'active_support/core_ext/string'
require 'pry'

I18n.enforce_available_locales = true

0 comments on commit 4aa0cfa

Please sign in to comment.
You can’t perform that action at this time.