Class: Verifly::ApplicatorWithOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/verifly/applicator_with_options.rb

Overview

An applicator with useful options

Examples:

if

ApplicatorWithOptions.new(action, if: true)

unless

ApplicatorWithOptions.new(action, unless: false)

Direct Known Subclasses

DependentCallbacks::Callback

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ApplicatorWithOptions.

Parameters:

  • 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

Raises:

  • (ArgumentError)

    if there is more than two arguments and block

  • (ArgumentError)

    if there is zero arguments and no block



26
27
28
29
30
31
32
# File 'lib/verifly/applicator_with_options.rb', line 26

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

  self.action = Applicator.build(action)
  self.if_condition = Applicator.build(options.fetch(:if, true))
  self.unless_condition = Applicator.build(options.fetch(:unless, false))
end

Instance Attribute Details

#actionApplicator

main action to apply on call

Returns:



15
16
17
# File 'lib/verifly/applicator_with_options.rb', line 15

def action
  @action
end

#if_conditionApplicator

main action only apply if condition evaluates to truthy value

Returns:

  • (Applicator)

    the current value of if_condition



15
16
17
# File 'lib/verifly/applicator_with_options.rb', line 15

def if_condition
  @if_condition
end

#unless_conditionApplicator

main action only apply if condition evaluates to falsey value

Returns:

  • (Applicator)

    the current value of unless_condition



15
16
17
# File 'lib/verifly/applicator_with_options.rb', line 15

def unless_condition
  @unless_condition
end

Instance Method Details

#call(binding_, *context) ⇒ Object

Applies main action if if_condition is evaluated to truthy value and unless_condition is evaluated to falsey value

Parameters:

  • binding_ (#instance_exec)

    binding to apply (see Applicator)

  • context

    generic context to apply (see Applicator)

Returns:

  • main action application result

  • (nil)

    if condition checks failed



42
43
44
45
46
# File 'lib/verifly/applicator_with_options.rb', line 42

def call(binding_, *context)
  return unless if_condition.call(binding_, *context)
  return if unless_condition.call(binding_, *context)
  action.call(binding_, *context)
end