Module: Strum::Service

Overview

Module

Defined Under Namespace

Modules: ClassMethods Classes: Failure

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &_block) ⇒ Object



61
62
63
64
65
# File 'lib/strum/service.rb', line 61

def method_missing(method_name, *args, &_block)
  super unless input.is_a?(Hash) && input[method_name.to_sym]

  args.count.positive? ? public_send(input[method_name.to_sym], args) : input[method_name.to_sym]
end

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
# File 'lib/strum/service.rb', line 8

def self.included(base)
  base.class_eval do
    extend ClassMethods
  end
end

Instance Method Details

#errorsObject



45
46
47
# File 'lib/strum/service.rb', line 45

def errors
  strum_errors
end

#execute {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



30
31
32
33
34
35
# File 'lib/strum/service.rb', line 30

def execute
  audit
  yield(self) if block_given?
  call if valid?
  valid? ? valid_result : invalid_result
end

#failure(arg = nil, &block) ⇒ Object



57
58
59
# File 'lib/strum/service.rb', line 57

def failure(arg = nil, &block)
  service_handlers[:failure][arg] = block
end

#hook(name, data = self) ⇒ Object



37
38
39
# File 'lib/strum/service.rb', line 37

def hook(name, data = self)
  service_handlers[:on][name].is_a?(Proc) && service_handlers[:on][name].call(data)
end

#initialize(_input, **args) ⇒ Object

Instance methods



22
23
24
25
26
27
28
# File 'lib/strum/service.rb', line 22

def initialize(_input, **args)
  self.strum_errors = {}
  self.service_handlers = { on: {}, success: {}, failure: {} }
  self.outputs = {}
  self.inputs = args.merge(default: _input)
  self._inputs = inputs.dup.freeze
end

#on(arg, &block) ⇒ Object



49
50
51
# File 'lib/strum/service.rb', line 49

def on(arg, &block)
  service_handlers[:on][arg] = block
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/strum/service.rb', line 67

def respond_to_missing?(method_name, include_private = false)
  input.is_a?(Hash) && input[method_name.to_sym] || super
end

#success(arg = nil, &block) ⇒ Object



53
54
55
# File 'lib/strum/service.rb', line 53

def success(arg = nil, &block)
  service_handlers[:success][arg] = block
end

#valid?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/strum/service.rb', line 41

def valid?
  strum_errors.empty?
end