Skip to content

Commit

Permalink
Small refactoring and improvements, fix travis setup
Browse files Browse the repository at this point in the history
Description:
- Refactor Pluggable module a bit to fit rubocop constraints.
- Fix `Service::SpecHelpers` module to extend example goups with
it's ClassMethods, add test for `service_context` method.
- As it was before, `require` plugin file when it is used, but
is not registered.
- Refactor `SpecHelper` for common approach.
- Fix `.travis.yml` to specify proper Ruby version
  • Loading branch information
akuzko committed Mar 24, 2021
1 parent a3632dd commit f948b21
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: ruby
rvm:
- 2.2.0
- 2.4.0
before_install: gem install bundler -v 1.16.0
2 changes: 2 additions & 0 deletions lib/zen/service/plugins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module Zen
module Service::Plugins
def self.fetch(name)
require("zen/service/plugins/#{name}") unless plugins.key?(name)

plugins[name] || raise("extension `#{name}` is not registered")
end

Expand Down
4 changes: 4 additions & 0 deletions lib/zen/service/plugins/pluggable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def use(name, **opts)
return extension
end

use_extension(extension, name, **opts)
end

private def use_extension(extension, name, **opts)
include extension
extend extension::ClassMethods if extension.const_defined?(:ClassMethods)

Expand Down
4 changes: 4 additions & 0 deletions lib/zen/service/spec_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

module Zen
module Service::SpecHelpers
def self.included(target)
target.extend(ClassMethods)
end

# Example:
# stub_service(MyService)
# .with_atributes(foo: 'foo')
Expand Down
20 changes: 10 additions & 10 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
require "rspec/its"

module SpecHelper
module GroupMethods
def def_service(&block)
let(:service_class) { Class.new(Zen::Service, &block) }
end
def self.included(target)
target.extend(ClassMethods)
end

def build_service(*args)
service_class.new(*args)
end

module ExampleMethods
def build_service(*args)
service_class.new(*args)
module ClassMethods
def def_service(&block)
let(:service_class) { Class.new(Zen::Service, &block) }
end
end
end
Expand All @@ -29,8 +31,6 @@ def build_service(*args)
c.syntax = :expect
end

config.extend SpecHelper::GroupMethods
config.include SpecHelper::ExampleMethods

config.include SpecHelper
config.include Zen::Service::SpecHelpers
end
13 changes: 12 additions & 1 deletion spec/zen/service/rspec_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

RSpec.describe "Zen::Service::SpecHelpers" do
class ::SpecHelpersService < Zen::Service
use :context

attributes :foo

def execute!
failure!
context[:foo]
end
end

Expand All @@ -24,4 +26,13 @@ def execute!
expect(service.result).to eq(6)
end
end

describe "service_context" do
service_context { { foo: 5 } }

it "allows to set service context" do
service = SpecHelpersService.new
expect(service.execute.result).to eq(5)
end
end
end

0 comments on commit f948b21

Please sign in to comment.