Skip to content

Commit

Permalink
Refacor, plugins feature, initializer feature
Browse files Browse the repository at this point in the history
Now supports plugins like sequel, can choose initializer, remove return engine
  • Loading branch information
AnotherRegularDude committed Dec 14, 2024
1 parent 72d2929 commit 5777ce4
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 116 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ gem "rubocop-config-umbrellio"
gem "simplecov"
gem "simplecov-lcov"
gem "smart_initializer"
gem "qonfig", "0.28.0"
3 changes: 2 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ GEM
pry (0.15.0)
coderay (~> 1.1)
method_source (~> 1.0)
qonfig (0.29.0)
qonfig (0.28.0)
racc (1.8.1)
rack (3.1.8)
rainbow (3.1.1)
Expand Down Expand Up @@ -149,6 +149,7 @@ DEPENDENCIES
ci-helper
dry-initializer
pry
qonfig (= 0.28.0)
rake
resol!
rspec
Expand Down
10 changes: 10 additions & 0 deletions Steepfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

D = Steep::Diagnostic

target :lib do
signature "sig"
check "lib/resol/result.rb"

configure_code_diagnostics(D::Ruby.default)
end
13 changes: 12 additions & 1 deletion lib/resol.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# frozen_string_literal: true

require_relative "resol/version"
require_relative "resol/return_engine"
require_relative "resol/configuration"
require_relative "resol/initializers"
require_relative "resol/service"
require_relative "resol/plugins"

module Resol
extend self
Expand All @@ -15,4 +16,14 @@ def config
def configure
yield config
end

# rubocop:disable Naming/MethodName
def Success(...)
Success.new(...)
end

def Failure(...)
Failure.new(...)
end
# rubocop:enable Naming/MethodName
end
13 changes: 0 additions & 13 deletions lib/resol/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,16 @@ module Resol
module Configuration
extend self

DEFAULTS = { return_engine: Resol::ReturnEngine::Catch }.freeze

DEFAULTS.each_key do |attr_name|
define_method(attr_name) { values[attr_name] }
define_method(:"#{attr_name}=") { |value| values[attr_name] = value }
end

def smart_config
return nil if smart_not_loaded?

SmartCore::Initializer::Configuration.config
end

def to_h = values.dup

private

def smart_not_loaded?
!defined?(SmartCore::Initializer::Configuration)
end

def values
@values ||= DEFAULTS.dup
end
end
end
28 changes: 15 additions & 13 deletions lib/resol/plugins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,44 @@

module Resol
module Plugins
PLUGINS_PATH = Pathname("resol/plugins")
class Manager
include Singleton

def initialize
self.patched_class = Resol::Service
self.plugins = []
self.plugin_lib_path = PathName("resol/plugins")
end

def plugin(plugin_name, ...)
plugin_module = find_plugin_module(plugin_name)
plugin_module.apply(target_class, ...) if plugin_module.respond_to?(:apply)
def plugin(plugin_name)
return if plugins.include?(plugin_name)

plugin_module = find_plugin_module(plugin_name)
if defined?(plugin_module::InstanceMethods)
target_class.include(plugin_module::InstanceMethods)
target_class.prepend(plugin_module::InstanceMethods)
end

if defined?(plugin_module::ClassMethods)
target_class.extend(plugin_module::ClassMethods)
target_class.singleton_class.prepend(plugin_module::ClassMethods)
end

plugins << plugin_name
end

private

attr_accessor :target_class, :plugins, :plugin_lib_path
attr_accessor :plugins

def find_plugin_module(plugin_name)
require plugin_lib_path.join(plugin_name)
require PLUGINS_PATH.join(plugin_name)
Plugins.const_get(classify_plugin_name(plugin_name))
rescue LoadError, NameError => e
raise "Failed to load plugin '#{plugin_name}': #{e.message}"
end

def camel_case(string)
string.split("_").map(&:capitalize).join
def classify_plugin_name(string)
string.split(/_|-/).map!(&:capitalize).join
end

def target_class
Resol::Service
end
end
end
Expand Down
25 changes: 25 additions & 0 deletions lib/resol/plugins/return_in_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Resol
module Plugins
module ReturnInService
module ClassMethods
private

def handle_catch(_service)
yield
end

def call_service(service)
service.call.tap { |res| return unless res.is_a?(Service::Result) }
end
end

module InstanceMethods
private

def proceed_return(_service, data) = data
end
end
end
end
61 changes: 29 additions & 32 deletions lib/resol/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,11 @@
module Resol
class UnwrapError < StandardError; end

class Result
# @!method success?
# @!method failure?
# @!method value_or
# @!method value!

def initialize(*); end

def or
yield(@value) if failure?
end

def either(success_proc, failure_proc)
success? ? success_proc.call(@value) : failure_proc.call(@value)
end
end
class Result; end

class Success < Result
def initialize(value)
super
super()
@value = value
end

Expand All @@ -34,22 +19,34 @@ def failure?
false
end

def value_or(*)
def value_or(_other_value = nil)
@value
end

def value!
@value
end

def error
nil
def error = nil

def or = nil

def either(success_proc, _failure_proc)
success_proc.call(@value)
end

def bind
yield @value
end

def fmap(&)
Resol.Success(bind(&))
end
end

class Failure < Result
def initialize(error)
super
super()
@value = error
end

Expand All @@ -62,11 +59,7 @@ def failure?
end

def value_or(other_value = nil)
if block_given?
yield(@value)
else
other_value
end
block_given? ? yield(@value) : other_value
end

def value!
Expand All @@ -76,13 +69,17 @@ def value!
def error
@value
end
end

def self.Success(...)
Success.new(...)
end
def or
yield @value
end

def either(_success_proc, failure_proc)
failure_proc.call(@value)
end

def bind = self

def self.Failure(...)
Failure.new(...)
alias fmap bind
end
end
24 changes: 0 additions & 24 deletions lib/resol/return_engine/catch.rb

This file was deleted.

21 changes: 0 additions & 21 deletions lib/resol/return_engine/return.rb

This file was deleted.

Loading

0 comments on commit 5777ce4

Please sign in to comment.