Skip to content

Commit

Permalink
add rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgesky committed Mar 22, 2017
1 parent ebd66ac commit f9f05b3
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 43 deletions.
51 changes: 51 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Style/StringLiterals:
EnforcedStyle: double_quotes

Style/FrozenStringLiteralComment:
Enabled: false

# Allow some style changes in specs
Metrics/ModuleLength:
Exclude:
- spec/**/*
Metrics/BlockLength:
Exclude:
- spec/**/*
Style/BlockDelimiters:
Exclude:
- spec/**/*
Style/RescueModifier:
Exclude:
- spec/**/*
Metrics/MethodLength:
Exclude:
- spec/interactor/hooks_spec.rb
Style/IndentArray:
Exclude:
- spec/integration_spec.rb
- spec/interactor/hooks_spec.rb

# Allow nice tree-like comments in specs
Style/AsciiComments:
Exclude:
- spec/integration_spec.rb

# Here inconsistent indentation helps to understand
# tree nature of callbacks.
Style/AlignArray:
Exclude:
- spec/integration_spec.rb

# Rubocop suggests using $INPUT_RECORD_SEPARATOR
# variable from stdlib 'English' module over $/.
# This module appeared in Ruby 2.0, so we could use it
# only if we drop 1.9.3 support
Style/SpecialGlobalVars:
Exclude:
- interactor.gemspec

# This could be removed if throws are used instead of
# raising Failure in #fail!
Lint/HandleExceptions:
Exclude:
- lib/interactor.rb
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ rvm:
- "2.3.3"
- "2.4.0"
- ruby-head
script: bundle exec rspec
script: bundle exec rake
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require "rubocop/rake_task"

RSpec::Core::RakeTask.new(:spec)
RuboCop::RakeTask.new(:rubocop)

task default: :spec
task default: [:spec, :rubocop]
4 changes: 3 additions & 1 deletion interactor.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Gem::Specification.new do |spec|

spec.author = "Collective Idea"
spec.email = "info@collectiveidea.com"
spec.description = "Interactor provides a common interface for performing complex user interactions."
spec.description = "Interactor provides a common interface for performing " \
"complex user interactions."
spec.summary = "Simple interactor implementation"
spec.homepage = "https://github.com/collectiveidea/interactor"
spec.license = "MIT"
Expand All @@ -16,4 +17,5 @@ Gem::Specification.new do |spec|

spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.3"
spec.add_development_dependency "rubocop", "~> 0.47.1"
end
6 changes: 2 additions & 4 deletions lib/interactor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,12 @@ def run!
# each interactor class.
#
# Returns nothing.
def call
end
def call; end

# Public: Reverse prior invocation of an Interactor instance. Any interactor
# class that requires undoing upon downstream failure is expected to overwrite
# the "rollback" instance method.
#
# Returns nothing.
def rollback
end
def rollback; end
end
2 changes: 1 addition & 1 deletion lib/interactor/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Context < OpenStruct
#
# Returns the Interactor::Context.
def self.build(context = {})
self === context ? context : new(context)
context.is_a?(Context) ? context : new(context)
end

# Public: Whether the Interactor::Context is successful. By default, a new
Expand Down
4 changes: 2 additions & 2 deletions lib/interactor/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ def with_hooks
#
# Returns nothing.
def run_around_hooks(&block)
self.class.around_hooks.reverse.inject(block) { |chain, hook|
self.class.around_hooks.reverse.inject(block) do |chain, hook|
proc { run_hook(hook, chain) }
}.call
end.call
end

# Internal: Run before hooks.
Expand Down
53 changes: 21 additions & 32 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def build_organizer(options = {}, &block)
# └─ interactor5

let(:organizer) {
build_organizer(organize: [organizer2, interactor3, organizer4, interactor5]) do
interactors = [organizer2, interactor3, organizer4, interactor5]
build_organizer(organize: interactors) do
around do |interactor|
context.steps << :around_before
interactor.call
Expand Down Expand Up @@ -315,7 +316,8 @@ def rollback

context "when an around hook fails early" do
let(:organizer) {
build_organizer(organize: [organizer2, interactor3, organizer4, interactor5]) do
interactors = [organizer2, interactor3, organizer4, interactor5]
build_organizer(organize: interactors) do
around do |interactor|
context.fail!
context.steps << :around_before
Expand Down Expand Up @@ -345,12 +347,10 @@ def rollback

context "when an around hook errors early" do
let(:organizer) {
build_organizer(organize: [organizer2, interactor3, organizer4, interactor5]) do
around do |interactor|
interactors = [organizer2, interactor3, organizer4, interactor5]
build_organizer(organize: interactors) do
around do |_interactor|
raise "foo"
context.steps << :around_before
interactor.call
context.steps << :around_after
end

before do
Expand Down Expand Up @@ -381,7 +381,8 @@ def rollback

context "when a before hook fails" do
let(:organizer) {
build_organizer(organize: [organizer2, interactor3, organizer4, interactor5]) do
interactors = [organizer2, interactor3, organizer4, interactor5]
build_organizer(organize: interactors) do
around do |interactor|
context.steps << :around_before
interactor.call
Expand Down Expand Up @@ -412,7 +413,8 @@ def rollback

context "when a before hook errors" do
let(:organizer) {
build_organizer(organize: [organizer2, interactor3, organizer4, interactor5]) do
interactors = [organizer2, interactor3, organizer4, interactor5]
build_organizer(organize: interactors) do
around do |interactor|
context.steps << :around_before
interactor.call
Expand All @@ -421,7 +423,6 @@ def rollback

before do
raise "foo"
context.steps << :before
end

after do
Expand Down Expand Up @@ -449,7 +450,8 @@ def rollback

context "when an after hook fails" do
let(:organizer) {
build_organizer(organize: [organizer2, interactor3, organizer4, interactor5]) do
interactors = [organizer2, interactor3, organizer4, interactor5]
build_organizer(organize: interactors) do
around do |interactor|
context.steps << :around_before
interactor.call
Expand Down Expand Up @@ -500,7 +502,8 @@ def rollback

context "when an after hook errors" do
let(:organizer) {
build_organizer(organize: [organizer2, interactor3, organizer4, interactor5]) do
interactors = [organizer2, interactor3, organizer4, interactor5]
build_organizer(organize: interactors) do
around do |interactor|
context.steps << :around_before
interactor.call
Expand All @@ -513,7 +516,6 @@ def rollback

after do
raise "foo"
context.steps << :after
end
end
}
Expand Down Expand Up @@ -557,7 +559,8 @@ def rollback

context "when an around hook fails late" do
let(:organizer) {
build_organizer(organize: [organizer2, interactor3, organizer4, interactor5]) do
interactors = [organizer2, interactor3, organizer4, interactor5]
build_organizer(organize: interactors) do
around do |interactor|
context.steps << :around_before
interactor.call
Expand Down Expand Up @@ -609,12 +612,12 @@ def rollback

context "when an around hook errors late" do
let(:organizer) {
build_organizer(organize: [organizer2, interactor3, organizer4, interactor5]) do
interactors = [organizer2, interactor3, organizer4, interactor5]
build_organizer(organize: interactors) do
around do |interactor|
context.steps << :around_before
interactor.call
raise "foo"
context.steps << :around_after
end

before do
Expand Down Expand Up @@ -715,11 +718,8 @@ def rollback
context "when a nested around hook errors early" do
let(:interactor3) {
build_interactor do
around do |interactor|
around do |_interactor|
raise "foo"
context.steps << :around_before3
interactor.call
context.steps << :around_after3
end

before do
Expand Down Expand Up @@ -824,7 +824,6 @@ def rollback

before do
raise "foo"
context.steps << :before3
end

after do
Expand Down Expand Up @@ -934,7 +933,6 @@ def rollback

def call
raise "foo"
context.steps << :call3
end

def rollback
Expand Down Expand Up @@ -1033,7 +1031,6 @@ def rollback

after do
raise "foo"
context.steps << :after3
end

def call
Expand Down Expand Up @@ -1129,7 +1126,6 @@ def rollback
context.steps << :around_before3
interactor.call
raise "foo"
context.steps << :around_after3
end

before do
Expand Down Expand Up @@ -1232,11 +1228,8 @@ def rollback
context "when a deeply nested around hook errors early" do
let(:interactor4b) {
build_interactor do
around do |interactor|
around do |_interactor|
raise "foo"
context.steps << :around_before4b
interactor.call
context.steps << :around_after4b
end

before do
Expand Down Expand Up @@ -1351,7 +1344,6 @@ def rollback

before do
raise "foo"
context.steps << :before4b
end

after do
Expand Down Expand Up @@ -1471,7 +1463,6 @@ def rollback

def call
raise "foo"
context.steps << :call4b
end

def rollback
Expand Down Expand Up @@ -1580,7 +1571,6 @@ def rollback

after do
raise "foo"
context.steps << :after4b
end

def call
Expand Down Expand Up @@ -1686,7 +1676,6 @@ def rollback
context.steps << :around_before4b
interactor.call
raise "foo"
context.steps << :around_after4b
end

before do
Expand Down
4 changes: 3 additions & 1 deletion spec/support/lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
let(:context) { double(:context) }

it "initializes a context" do
expect(Interactor::Context).to receive(:build).once.with(foo: "bar") { context }
expect(
Interactor::Context
).to receive(:build).once.with(foo: "bar") { context }

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

Expand Down

0 comments on commit f9f05b3

Please sign in to comment.