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


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

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


5
6
7
# File 'lib/easy_tags/options/callback.rb', line 5

def context
  @context
end

#typeString (readonly)


19
20
21
# File 'lib/easy_tags/options/callback.rb', line 19

def type
  @type
end

Instance Method Details

#run(taggable:, tagging:) ⇒ Object

invoke callback for a taggable object for a specific tagging


25
26
27
28
29
30
31
# File 'lib/easy_tags/options/callback.rb', line 25

def run(taggable:, tagging:)
  if @callback.is_a?(Symbol)
    taggable.send(@callback, tagging)
  elsif @callback.is_a?(Proc)
    taggable.instance_exec tagging, &@callback
  end
end