Skip to content

Commit

Permalink
Merge pull request #52 from collectiveidea/ostruct-context
Browse files Browse the repository at this point in the history
Change the context object to an OpenStruct rather than a Hash delegator
  • Loading branch information
emilford committed May 5, 2014
2 parents 06a0bbb + 01eec68 commit 4e8d025
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 37 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ source "https://rubygems.org"
gemspec

group :test do
gem "activesupport", "~> 4.0", require: false
gem "coveralls", "~> 0.6.9", require: false
gem "rspec", "~> 2.14"
end
12 changes: 4 additions & 8 deletions lib/interactor/context.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
require "delegate"
require "ostruct"

module Interactor
class Context < SimpleDelegator
class Context < OpenStruct
def self.build(context = {})
self === context ? context : new(context.dup)
end

def initialize(context = {})
super(context)
self === context ? context : new(context)
end

def success?
Expand All @@ -19,7 +15,7 @@ def failure?
end

def fail!(context = {})
update(context)
modifiable.update(context)
@failure = true
end
end
Expand Down
33 changes: 7 additions & 26 deletions spec/interactor/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ module Interactor
context = Context.build(foo: "bar")

expect(context).to be_a(Context)
expect(context).to eq(foo: "bar")
expect(context.foo).to eq("bar")
end

it "builds an empty context if no hash is given" do
context = Context.build

expect(context).to be_a(Context)
expect(context).to eq({})
expect(context.send(:table)).to eq({})
end

it "doesn't affect the original hash" do
Expand All @@ -23,7 +23,7 @@ module Interactor

expect(context).to be_a(Context)
expect {
context[:foo] = "baz"
context.foo = "baz"
}.not_to change {
hash[:foo]
}
Expand All @@ -35,32 +35,13 @@ module Interactor

expect(context2).to be_a(Context)
expect {
context2[:foo] = "baz"
context2.foo = "baz"
}.to change {
context1[:foo]
context1.foo
}.from("bar").to("baz")
end
end

describe "#initialize" do
it "defaults to empty" do
expect {
expect(Context.new).to eq({})
}.not_to raise_error
end
end

describe "#[]" do
it "maintains indifferent access" do
require "active_support/hash_with_indifferent_access"

context = Context.build(HashWithIndifferentAccess.new(foo: "bar"))

expect(context[:foo]).to eq("bar")
expect(context["foo"]).to eq("bar")
end
end

describe "#success?" do
let(:context) { Context.build }

Expand Down Expand Up @@ -110,15 +91,15 @@ module Interactor
expect {
context.fail!
}.not_to change {
context[:foo]
context.foo
}
end

it "updates the context" do
expect {
context.fail!(foo: "baz")
}.to change {
context[:foo]
context.foo
}.from("bar").to("baz")
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/support/lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@
it "calls setup" do
interactor.class_eval do
def setup
context[:foo] = context[:bar]
context.foo = context.bar
end
end

instance = interactor.new(bar: "baz")

expect(instance.context[:foo]).to eq("baz")
expect(instance.context.foo).to eq("baz")
end
end

Expand Down

0 comments on commit 4e8d025

Please sign in to comment.