Module: PayDirt::UseCase

Included in:
Base
Defined in:
lib/pay_dirt/use_case.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
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
41
42
43
44
45
46
47
48
49
# File 'lib/pay_dirt/use_case.rb', line 3

def self.included(base)
  # Load instance variables from the provided hash of dependencies.
  #
  # Raises if any required dependencies are missing from +options+ hash.
  #
  # @param [List<String,Symbol>]
  #   option_names list of keys representing required dependencies
  #
  # @param [Hash]
  #   options A hash of dependencies
  #
  # @public
  def load_options(*option_names, options)
    # Load required options
    option_names.each { |o| options = load_option(o, options) }

    # Load remaining options
    options.each_key  { |o| options = load_option(o, options) }
  end

  # Returns a result object representing success.
  #
  # @param [List<String,Symbol>]
  #   success should the result be +#successful?+
  #
  # @param [Object]
  #   data (nil) optional, an object containing information
  #
  # @public
  def result(success, data = nil)
    PayDirt::Result.new(success: success, data: data)
  end

  private
  # @private
  def load_option(option, options)
    instance_variable_set("@#{option}", fetch(option, options))
    options.delete(option)

    return options
  end

  # @private
  def fetch(opt, opts)
    opts.fetch(opt.to_sym) { raise "Missing required option: #{opt}" }
  end
end

Instance Method Details

#fetch(opt, opts) ⇒ Object



46
47
48
# File 'lib/pay_dirt/use_case.rb', line 46

def fetch(opt, opts)
  opts.fetch(opt.to_sym) { raise "Missing required option: #{opt}" }
end

#load_option(option, options) ⇒ Object



38
39
40
41
42
43
# File 'lib/pay_dirt/use_case.rb', line 38

def load_option(option, options)
  instance_variable_set("@#{option}", fetch(option, options))
  options.delete(option)

  return options
end

#load_options(*option_names, options) ⇒ Object

Load instance variables from the provided hash of dependencies.

Raises if any required dependencies are missing from options hash.

Parameters:

  • option_names (List<String,Symbol>)

    list of keys representing required dependencies

  • options (Hash)

    A hash of dependencies



15
16
17
18
19
20
21
# File 'lib/pay_dirt/use_case.rb', line 15

def load_options(*option_names, options)
  # Load required options
  option_names.each { |o| options = load_option(o, options) }

  # Load remaining options
  options.each_key  { |o| options = load_option(o, options) }
end

#result(success, data = nil) ⇒ Object

Returns a result object representing success.

Parameters:

  • success (List<String,Symbol>)

    should the result be #successful?

  • data (Object) (defaults to: nil)

    (nil) optional, an object containing information



32
33
34
# File 'lib/pay_dirt/use_case.rb', line 32

def result(success, data = nil)
  PayDirt::Result.new(success: success, data: data)
end