From d49e454632f39ad458da28168c058e3b6d650324 Mon Sep 17 00:00:00 2001 From: Alexey Date: Tue, 22 Jun 2021 12:57:39 +0300 Subject: [PATCH] Remove uber dependency (#6) * removed * update gems * lint * uber comment * add some kwargs spec * reorder --- Gemfile.lock | 26 ++++++++++++-------------- lib/resol.rb | 1 - lib/resol/builder.rb | 30 +++++++++++++++++++++++++++--- resol.gemspec | 1 - spec/building_spec.rb | 16 +++++++++++++--- 5 files changed, 52 insertions(+), 22 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 71e07d0..c612094 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/lib/resol.rb b/lib/resol.rb index b6304dd..22f4fe5 100644 --- a/lib/resol.rb +++ b/lib/resol.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require "uber/builder" require "smart_core/initializer" require_relative "resol/version" diff --git a/lib/resol/builder.rb b/lib/resol/builder.rb index e028487..fea981e 100644 --- a/lib/resol/builder.rb +++ b/lib/resol/builder.rb @@ -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 diff --git a/resol.gemspec b/resol.gemspec index 3c7f2d0..9e12044 100644 --- a/resol.gemspec +++ b/resol.gemspec @@ -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" diff --git a/spec/building_spec.rb b/spec/building_spec.rb index 0e94463..767cfe6 100644 --- a/spec/building_spec.rb +++ b/spec/building_spec.rb @@ -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) @@ -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