Module: Actionizer::ClassMethods

Defined in:
lib/actionizer.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/actionizer.rb', line 16

def method_missing(method_name, *args, &block)
  instance = new(*args)

  if method_name.to_s.end_with?('!')
    method_name = method_name.to_s.chomp('!').to_sym
    instance.raise_on_failure = true
  end

  if instance.respond_to?(method_name)
    error = defined_inputs.check_for_param_error(method_name, *args)
    if error
      raise Actionizer::Failure.new('Failed.', Actionizer::Result.new(error: error).tap(&:fail))
    end

    instance.tap(&method_name).output
  else
    super
  end
rescue Actionizer::Failure => af
  if instance.raise_on_failure
    raise af
  end

  af.output
end

Instance Method Details

#defined_inputsObject



50
51
52
# File 'lib/actionizer.rb', line 50

def defined_inputs
  @defined_inputs ||= Actionizer::Inputs.new
end

#inputs_for(method) ⇒ Object

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
62
# File 'lib/actionizer.rb', line 54

def inputs_for(method)
  raise ArgumentError, 'inputs_for requires a block' if !block_given?

  defined_inputs.start(method)
  yield
  defined_inputs.end

  raise 'You must define at least one optional or required param' if defined_inputs.no_params_declared?(method)
end

#optional(param, opts = {}) ⇒ Object



64
65
66
# File 'lib/actionizer.rb', line 64

def optional(param, opts = {})
  define_input_param(false, param, opts)
end

#required(param, opts = {}) ⇒ Object



68
69
70
# File 'lib/actionizer.rb', line 68

def required(param, opts = {})
  define_input_param(true, param, opts)
end

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

Returns:

  • (Boolean)


42
43
44
45
46
47
48
# File 'lib/actionizer.rb', line 42

def respond_to_missing?(method_name, include_private = false)
  if method_name.to_s.end_with?('!')
    method_name = method_name.to_s.chomp('!').to_sym
  end

  new.respond_to?(method_name, include_private)
end