Skip to content

Commit

Permalink
Remove uber dependency (#6)
Browse files Browse the repository at this point in the history
* removed

* update gems

* lint

* uber comment

* add some kwargs spec

* reorder
  • Loading branch information
nulldef authored Jun 22, 2021
1 parent 968e210 commit d49e454
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
26 changes: 12 additions & 14 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ PATH
specs:
resol (0.4.1)
smart_initializer (~> 0.5)
uber (~> 0.1)

GEM
remote: https://rubygems.org/
specs:
activesupport (6.1.3.1)
activesupport (6.1.3.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
Expand All @@ -24,18 +23,18 @@ GEM
umbrellio-sequel-plugins (~> 0.4)
coderay (1.1.3)
colorize (0.8.1)
concurrent-ruby (1.1.8)
concurrent-ruby (1.1.9)
diff-lcs (1.4.4)
docile (1.3.5)
docile (1.4.0)
dry-inflector (0.2.0)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
method_source (1.0.0)
minitest (5.14.4)
parallel (1.20.1)
parser (3.0.1.0)
parser (3.0.1.1)
ast (~> 2.4.1)
pry (0.14.0)
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
qonfig (0.25.0)
Expand Down Expand Up @@ -66,9 +65,9 @@ GEM
rubocop-ast (>= 1.2.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.4.1)
parser (>= 2.7.1.5)
rubocop-config-umbrellio (1.11.0.40)
rubocop-ast (1.7.0)
parser (>= 3.0.1.1)
rubocop-config-umbrellio (1.11.0.51)
rubocop (= 1.11.0)
rubocop-performance (= 1.10.0)
rubocop-rails (= 2.9.1)
Expand All @@ -90,14 +89,14 @@ GEM
rubocop-sequel (0.2.0)
rubocop (~> 1.0)
ruby-progressbar (1.11.0)
sequel (5.43.0)
sequel (5.45.0)
simplecov (0.21.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov-lcov (0.8.0)
simplecov_json_formatter (0.1.2)
simplecov_json_formatter (0.1.3)
smart_engine (0.11.0)
smart_initializer (0.5.0)
qonfig (~> 0.24)
Expand All @@ -109,10 +108,9 @@ GEM
thor (1.1.0)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
uber (0.1.0)
umbrellio-sequel-plugins (0.4.0.164)
umbrellio-sequel-plugins (0.5.1.27)
sequel
symbiont-ruby (>= 0.6)
symbiont-ruby
unicode-display_width (2.0.0)
zeitwerk (2.4.2)

Expand Down
1 change: 0 additions & 1 deletion lib/resol.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require "uber/builder"
require "smart_core/initializer"

require_relative "resol/version"
Expand Down
30 changes: 27 additions & 3 deletions lib/resol/builder.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
# frozen_string_literal: true

module Resol
# Most of the code here is borrowed from Uber::Builder code
# See https://github.com/apotonick/uber/blob/master/lib/uber/builder.rb
module Builder
def self.included(base)
base.include(Uber::Builder)
base.extend(ClassMethods)
end

class Builders < Array
def call(initial_klass, *args, **kwargs)
each do |block|
klass = block.call(initial_klass, *args, **kwargs) and return klass
end

initial_klass
end

def <<(proc)
wrapped = -> (ctx, *args, **kwargs) { ctx.instance_exec(*args, **kwargs, &proc) }
super(wrapped)
end
end

module ClassMethods
def build_klass(...)
def builders
@builders ||= Builders.new
end

def builds(proc = nil, &block)
builders << (proc || block)
end

def build_klass(*args, **kwargs)
klass = self

loop do
new_klass = klass.build!(klass, ...)
new_klass = klass.builders.call(klass, *args, **kwargs)
break if new_klass == klass

klass = new_klass
Expand Down
1 change: 0 additions & 1 deletion resol.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "smart_initializer", "~> 0.5"
spec.add_dependency "uber", "~> 0.1"

spec.add_development_dependency "bundler-audit"
spec.add_development_dependency "ci-helper"
Expand Down
16 changes: 13 additions & 3 deletions spec/building_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
class BaseService < Resol::Service
param :type

option :option

builds { |type| ServiceA if type == :a }
builds { |type| ServiceB if type == :b }
builds { |*, option:| ServiceC if option }

def call
success!(:base_service)
Expand All @@ -23,10 +26,17 @@ def call
end
end

class ServiceC < BaseService
def call
success!(:service_c)
end
end

RSpec.describe Resol do
it "builds a right service" do
expect(BaseService.build(:a)).to be_a(ServiceA)
expect(BaseService.build(:b)).to be_a(ServiceB)
expect(BaseService.build(:other)).to be_a(BaseService)
expect(BaseService.build(:a, option: true)).to be_a(ServiceA)
expect(BaseService.build(:b, option: true)).to be_a(ServiceB)
expect(BaseService.build(:other, option: true)).to be_a(ServiceC)
expect(BaseService.build(:other, option: false)).to be_a(BaseService)
end
end

0 comments on commit d49e454

Please sign in to comment.