Skip to content

Commit

Permalink
extract modules to make code structure more apparent
Browse files Browse the repository at this point in the history
  • Loading branch information
markburns committed Jan 22, 2024
1 parent e545af9 commit f258b6a
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 102 deletions.
94 changes: 5 additions & 89 deletions lib/interactify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,100 +11,16 @@
require "interactify/wiring"
require "interactify/configuration"
require "interactify/interactify_callable"

module Interactify
class << self
delegate :on_definition_error, :trigger_definition_error, to: :configuration

def railties_missing?
@railties_missing
end

def railties_missing!
@railties_missing = true
end

def railties
railties?
end

def railties?
!railties_missing?
end

def sidekiq_missing?
@sidekiq_missing
end

def sidekiq_missing!
@sidekiq_missing = true
end

def sidekiq
sidekiq?
end

def sidekiq?
!sidekiq_missing?
end
end
end

Interactify.instance_eval do
@sidekiq_missing = nil
@railties_missing = nil
end

begin
require "sidekiq"
rescue LoadError
Interactify.sidekiq_missing!
end

begin
require "rails/railtie"
rescue LoadError
Interactify.railties_missing!
end
require "interactify/dependency_inference"
require "interactify/hooks"
require "interactify/configure"

module Interactify
extend ActiveSupport::Concern
extend Hooks
extend Configure

class << self
def validate_app(ignore: [])
Interactify::Wiring.new(root: Interactify.configuration.root, ignore:).validate_app
end

def reset
@on_contract_breach = nil
@before_raise_hook = nil
@configuration = nil
end

def trigger_contract_breach_hook(...)
@on_contract_breach&.call(...)
end

def on_contract_breach(&block)
@on_contract_breach = block
end

def trigger_before_raise_hook(...)
@before_raise_hook&.call(...)
end

def before_raise(&block)
@before_raise_hook = block
end

def configure
yield configuration
end

def configuration
@configuration ||= Configuration.new
end

delegate :root, to: :configuration
end

Expand Down
15 changes: 15 additions & 0 deletions lib/interactify/configure.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Interactify
module Configure
def validate_app(ignore: [])
Interactify::Wiring.new(root: Interactify.configuration.root, ignore:).validate_app
end

def configure
yield configuration
end

def configuration
@configuration ||= Configuration.new
end
end
end
26 changes: 26 additions & 0 deletions lib/interactify/contracts/breaches.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Interactify
module Breaches
def self.handle_with_failure(context, breaches)
breaches = preamble(context, breaches)
context.fail! contract_failures: breaches
end

def self.handle_with_exception(context, failure_klass, breaches)
breaches = preamble(context, breaches)

# e.g. raises
# SomeNamespace::SomeClass::ContractFailure, {whatever: 'is missing'}
# but also sending the context into Sentry
exception = failure_klass.new(breaches.to_json)
Interactify.trigger_before_raise_hook(exception)
raise exception
end

def self.preamble(context, breaches)
breaches = breaches.map { |b| { b.property => b.messages } }.inject(&:merge)

Interactify.trigger_contract_breach_hook(context, breaches)
breaches
end
end
end
18 changes: 5 additions & 13 deletions lib/interactify/contracts/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "interactify/contracts/setup"
require "interactify/contracts/promising"
require "interactify/contracts/organizing"
require "interactify/contracts/breaches"
require "interactify/dsl/organizer"

module Interactify
Expand Down Expand Up @@ -60,27 +61,18 @@ def _interactify_extract_keys(clauses)
# rubocop: enable Metrics/BlockLength

included do
c = Class.new(Contracts::Failure)
failure_klass = Class.new(Contracts::Failure)
# example self is Whatever::SomeInteractor
# failure class: Whatever::SomeInteractor::InteractorContractFailure
const_set "InteractorContractFailure", c
const_set "InteractorContractFailure", failure_klass
prepend Contracts::CallWrapper
include Dsl::Organizer

on_breach do |breaches|
breaches = breaches.map { |b| { b.property => b.messages } }.inject(&:merge)

Interactify.trigger_contract_breach_hook(context, breaches)

if @_interactor_called_by_non_bang_method == true
context.fail! contract_failures: breaches
Breaches.handle_with_failure(context, breaches)
else
# e.g. raises
# SomeNamespace::SomeClass::ContractFailure, {whatever: 'is missing'}
# but also sending the context into Sentry
exception = c.new(breaches.to_json)
Interactify.trigger_before_raise_hook(exception)
raise exception
Breaches.handle_with_exception(context, failure_klass, breaches)
end
end
end
Expand Down
56 changes: 56 additions & 0 deletions lib/interactify/dependency_inference.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module Interactify
class << self
delegate :on_definition_error, :trigger_definition_error, to: :configuration

def railties_missing?
@railties_missing
end

def railties_missing!
@railties_missing = true
end

def railties
railties?
end

def railties?
!railties_missing?
end

def sidekiq_missing?
@sidekiq_missing
end

def sidekiq_missing!
@sidekiq_missing = true
end

def sidekiq
sidekiq?
end

def sidekiq?
!sidekiq_missing?
end
end
end

Interactify.instance_eval do
@sidekiq_missing = nil
@railties_missing = nil
end

begin
require "sidekiq"
rescue LoadError
Interactify.sidekiq_missing!
end

begin
require "rails/railtie"
rescue LoadError
Interactify.railties_missing!
end


25 changes: 25 additions & 0 deletions lib/interactify/hooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Interactify
module Hooks
def reset
@on_contract_breach = nil
@before_raise_hook = nil
@configuration = nil
end

def trigger_contract_breach_hook(...)
@on_contract_breach&.call(...)
end

def on_contract_breach(&block)
@on_contract_breach = block
end

def trigger_before_raise_hook(...)
@before_raise_hook&.call(...)
end

def before_raise(&block)
@before_raise_hook = block
end
end
end

0 comments on commit f258b6a

Please sign in to comment.