Class: EasyTags::Options::Callback

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_tags/options/callback.rb

Overview

Represents a single callback item

Constant Summary collapse

TYPES =
%i[after_add after_remove].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(callback:, type:) ⇒ Callback

Returns a new instance of Callback.

Parameters:

  • callback (Proc)
  • type (Symbol)


13
14
15
16
17
18
# File 'lib/easy_tags/options/callback.rb', line 13

def initialize(callback:, type:)
  @callback = callback
  @type = type

  raise "unknown callback type - must be one of #{TYPES}" unless TYPES.include?(type)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/easy_tags/options/callback.rb', line 7

def context
  @context
end

#typeString (readonly)

Returns:

  • (String)


21
22
23
# File 'lib/easy_tags/options/callback.rb', line 21

def type
  @type
end

Instance Method Details

#run(taggable:, tagging:) ⇒ Object

invoke callback for a taggable object for a specific tagging

Parameters:

  • taggable (Object)
  • tagging (Object)


27
28
29
30
31
32
33
34
# File 'lib/easy_tags/options/callback.rb', line 27

def run(taggable:, tagging:)
  case @callback
  when Symbol
    taggable.send(@callback, tagging)
  when Proc
    taggable.instance_exec tagging, &@callback
  end
end