Module: Crossbeam::Output::ClassMethods

Defined in:
lib/crossbeam/output.rb

Overview

Methods to load in the service object as class methods

Constant Summary collapse

CB_ALLOWED_OUTPUTS =

Returns:

  • (Array)
[NilClass, String, Symbol].freeze

Instance Method Summary collapse

Instance Method Details

#allowed_output?(output_type) ⇒ Boolean

Determine if the output attribute type is allowed

Parameters:

  • output_type (String)

Returns:

  • (Boolean)


38
39
40
# File 'lib/crossbeam/output.rb', line 38

def allowed_output?(output_type)
  CB_ALLOWED_OUTPUTS.include?(output_type.class)
end

#build_error_listVoid

Add errors to @result.errors

Returns:

  • (Void)


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/crossbeam/output.rb', line 62

def build_error_list
  return if @klass.nil?
  return unless @klass&.errors&.any?

  @klass.errors.each do |error|
    # options is usually passed with an ActiveRecord validation error
    # @example:
    #   <attribute=age, type=greater_than_or_equal_to, options={:value=>15, :count=>18}>
    @result.errors.add(error.attribute, error.message)
  end
  @klass.errors.clear
  reassign_results
end

#output(param) ⇒ String, Symbol

Used to specify an attribute/instance variable that should be used too return instead of @result

Parameters:

  • param (String, Symbol)

Returns:

  • (String, Symbol)

Raises:



28
29
30
31
32
# File 'lib/crossbeam/output.rb', line 28

def output(param)
  raise(ArguementError, 'A string or symbol is require for output') unless allowed_output?(param)

  @output_param = param
end

#output?Boolean

Used to determine if a output parameter has been set or not

Returns:

  • (Boolean)


45
46
47
# File 'lib/crossbeam/output.rb', line 45

def output?
  !output_param.nil?
end

#output_paramString, ...

Used hold the parameter which can/will be used instead of @result

Returns:

  • (String, Symbol, nil)


94
95
96
# File 'lib/crossbeam/output.rb', line 94

def output_param
  @output_param ||= nil
end

#reassign_resultsVoid

Reassign result, unless errors

Returns:

  • (Void)


79
80
81
82
# File 'lib/crossbeam/output.rb', line 79

def reassign_results
  @result.results = nil
  @result.results = @klass.instance_variable_get(:"@#{output_param}") if specified_output?
end

#set_results_outputHash

Determine the output to return if the instance_variable exists in the klass

Returns:

  • (Hash)


52
53
54
55
56
57
# File 'lib/crossbeam/output.rb', line 52

def set_results_output
  return unless @klass
  return unless output? && @klass.instance_variable_defined?(:"@#{output_param}")

  @result.results = @klass.instance_variable_get(:"@#{output_param}")
end

#specified_output?Boolean

Does the klass have an assigned output

Returns:

  • (Boolean)


87
88
89
# File 'lib/crossbeam/output.rb', line 87

def specified_output?
  output? && @klass.instance_variable_defined?(:"@#{output_param}")
end