Class: Verifly::DependentCallbacks::Callback

Inherits:
ApplicatorWithOptions show all
Defined in:
lib/verifly/dependent_callbacks/callback.rb

Overview

ApplicatorWithOptions improved to handle everything needed in DependentCallbacks

Constant Summary collapse

POSITIONS =

Available positions of calblack: before, after or around action

%i[before after around].freeze

Instance Attribute Summary collapse

Attributes inherited from ApplicatorWithOptions

#action, #if_condition, #unless_condition

Instance Method Summary collapse

Methods inherited from ApplicatorWithOptions

#call

Constructor Details

#initialize(position, action = block, options = {}, &block) ⇒ Callback

Returns a new instance of Callback.

Parameters:

  • position (:before, :after, :around)

    position

  • action (applicable) (defaults to: block)

    main action

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :if (applicable)

    main action is only applied if this evaluates to truthy value

  • :unless (applicable)

    main action is only applied if this evaluates to falsey value

  • :name (Symbol)

    name override for callback. By default, name is taken from applicable if it is a symbol or set to nil. This option allows to use named applicables like proc

  • :insert_before ([Symbol])

    array of callback names which should be sequenced after current. Note, that if position == :after, sequence would go backwards

  • :require ([Symbol])

    array of callback names which should be sequenced before current.

Raises:

  • (ArgumentError)

    if there is more than three arguments and block

  • (ArgumentError)

    if there is one argument and no block

See Also:



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/verifly/dependent_callbacks/callback.rb', line 34

def initialize(position, *args, &block)
  super(*args, &block)

  action, options = normalize_options(*args, &block)

  self.name = options.fetch(:name) { action if action.is_a?(Symbol) }

  self.position = position
  raise "#{position} should be one of #{POSITIONS}" unless POSITIONS.include?(position)

  self.before = Array(options.fetch(:insert_before, []))
  self.after = Array(options.fetch(:require, []))
end

Instance Attribute Details

#after[Symbol]

names of calblacks after which this is

Returns:

  • ([Symbol])

    the current value of after



10
11
12
# File 'lib/verifly/dependent_callbacks/callback.rb', line 10

def after
  @after
end

#before[Symbol]

names of calblacks before which this is

Returns:

  • ([Symbol])

    the current value of before



10
11
12
# File 'lib/verifly/dependent_callbacks/callback.rb', line 10

def before
  @before
end

#nameSymbol?

callback name

Returns:

  • (Symbol?)

    the current value of name



10
11
12
# File 'lib/verifly/dependent_callbacks/callback.rb', line 10

def name
  @name
end

#position:before, ...

callback position

Returns:

  • (:before, :after, :around)

    the current value of position



10
11
12
# File 'lib/verifly/dependent_callbacks/callback.rb', line 10

def position
  @position
end

Instance Method Details

#to_dot_label(binding_) ⇒ String

Converts callback to nice table in dot label format

Parameters:

  • binding_ (#instance_exec)

Returns:

  • (String)

    graphviz LabelHTML



51
52
53
54
55
56
# File 'lib/verifly/dependent_callbacks/callback.rb', line 51

def to_dot_label(binding_)
  template_path = File.expand_path("callback.dothtml.erb", __dir__)
  erb = ERB.new(File.read(template_path))
  erb.filename = template_path
  erb.result(binding)
end