Module: Strum::Service

Defined in:
lib/strum/service.rb,
lib/strum/service/version.rb

Overview

Strum service

Defined Under Namespace

Modules: ClassMethods Classes: Failure

Constant Summary collapse

COERCIVE_METHODS =

Define coercive methods

%i[add_error add_errors any required sliced sliced_list].freeze
VERSION =
"0.2.4"

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



79
80
81
82
83
84
85
# File 'lib/strum/service.rb', line 79

def method_missing(method_name, *args, &_block)
  if args.count.zero? && (input.is_a?(Hash) && input.keys.include?(method_name.to_sym))
    input[method_name.to_sym]
  else
    inputs[method_name.to_sym] || super
  end
end

Class Method Details

.included(base) ⇒ Object



10
11
12
13
14
# File 'lib/strum/service.rb', line 10

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

Instance Method Details

#errorsObject



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

def errors
  strum_errors
end

#executeObject



46
47
48
49
50
51
52
53
# File 'lib/strum/service.rb', line 46

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

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



75
76
77
# File 'lib/strum/service.rb', line 75

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

#hook(name, data = self) ⇒ Object



55
56
57
# File 'lib/strum/service.rb', line 55

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

#initialize(main_input, args = {}) ⇒ Object

Instance methods



36
37
38
39
40
41
42
43
44
# File 'lib/strum/service.rb', line 36

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

#on(arg, &block) ⇒ Object



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

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

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

Returns:

  • (Boolean)


87
88
89
# File 'lib/strum/service.rb', line 87

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



71
72
73
# File 'lib/strum/service.rb', line 71

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

#valid?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/strum/service.rb', line 59

def valid?
  strum_errors.empty?
end