Module: Lepus::Producers::Hooks

Included in:
Lepus::Producers
Defined in:
lib/lepus/producers/hooks.rb

Constant Summary collapse

KEY =
:lepus_producers

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.reset!Object



8
9
10
# File 'lib/lepus/producers/hooks.rb', line 8

def self.reset!
  Thread.current[KEY] = nil
end

Instance Method Details

#disable!(*targets) ⇒ void

This method returns an undefined value.

Global disable publishing callbacks. If no producer/exchange is specified, all producers will be disabled.

Parameters:

  • targets (Array<Lepus::Producer, String, Symbol>)

    Producer classes, exchange names, or both



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lepus/producers/hooks.rb', line 38

def disable!(*targets)
  if targets.empty?
    # Disable all producers
    all_producers.each { |producer| repo[:producers][producer] = false }
  else
    targets.each do |target|
      case target
      when Class
        ensure_producer_class(target)
        repo[:producers][target] = false
      when String, Symbol
        exchange_name = target.to_s
        repo[:exchanges][exchange_name] = false
      else
        raise ArgumentError, "Invalid producer or exchange name: #{target.inspect}"
      end
    end
  end
end

#disabled?(*producers) ⇒ Boolean

Check if the given producer is enabled for publishing. If no producer is specified, all producers will be checked.

Parameters:

Returns:

  • (Boolean)


62
63
64
65
66
67
68
# File 'lib/lepus/producers/hooks.rb', line 62

def disabled?(*producers)
  if producers.empty?
    all_producers.all? { |producer| !producer_enabled?(producer) }
  else
    producers.all? { |producer| !producer_enabled?(producer) }
  end
end

#enable!(*targets) ⇒ void

This method returns an undefined value.

Global enable publishing callbacks. If no producer/exchange is specified, all producers will be enabled.

Parameters:

  • targets (Array<Lepus::Producer, String, Symbol>)

    Producer classes, exchange names, or both



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lepus/producers/hooks.rb', line 15

def enable!(*targets)
  if targets.empty?
    # Enable all producers
    all_producers.each { |producer| repo[:producers][producer] = true }
  else
    targets.each do |target|
      case target
      when Class
        ensure_producer_class(target)
        repo[:producers][target] = true
      when String, Symbol
        exchange_name = target.to_s
        repo[:exchanges][exchange_name] = true
      else
        raise ArgumentError, "Invalid producer or exchange name: #{target.inspect}"
      end
    end
  end
end

#enabled?(*producers) ⇒ Boolean

Check if the given producer is enabled for publishing. If no producer is specified, all producers will be checked.

Parameters:

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# File 'lib/lepus/producers/hooks.rb', line 74

def enabled?(*producers)
  if producers.empty?
    all_producers.all? { |producer| producer_enabled?(producer) }
  else
    producers.all? { |producer| producer_enabled?(producer) }
  end
end

#exchange_enabled?(exchange_name) ⇒ Boolean

Check if the given exchange is enabled for publishing.

Parameters:

  • exchange_name (String)

    The exchange name to check

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/lepus/producers/hooks.rb', line 86

def exchange_enabled?(exchange_name)
  # Check if exchange is explicitly configured
  if repo[:exchanges].key?(exchange_name)
    return repo[:exchanges][exchange_name]
  end

  # Find all producers that use this exchange
  matching_producers = all_producers.select do |producer|
    producer.definition&.exchange_name == exchange_name
  end

  # If no producers use this exchange, consider it enabled by default
  return true if matching_producers.empty?

  # Check if all matching producers are enabled
  matching_producers.all? { |producer| producer_enabled?(producer) }
end

#with_publishing(*targets) ⇒ Object

Enable the publishing callbacks execution for the block execution. Example:

Lepus::Producers.with_publishing { User.create! }
Lepus::Producers.with_publishing(UsersIndex, "exchange_name") { User.create! }


121
122
123
124
125
126
127
128
# File 'lib/lepus/producers/hooks.rb', line 121

def with_publishing(*targets)
  state_before_enable = deep_copy_repo
  enable!(*targets)

  yield
ensure
  restore_repo(state_before_enable)
end

#without_publishing(*targets) ⇒ Object

Disable publishing callbacks execution for the block execution. Example:

Lepus::Producers.without_publishing { User.create! }
Lepus::Producers.without_publishing(UsersIndex, "exchange_name") { User.create! }


108
109
110
111
112
113
114
115
# File 'lib/lepus/producers/hooks.rb', line 108

def without_publishing(*targets)
  state_before_disable = deep_copy_repo
  disable!(*targets)

  yield
ensure
  restore_repo(state_before_disable)
end