Class: Mutant::Hooks Private

Inherits:
Object
  • Object
show all
Includes:
Unparser::Adamantium
Defined in:
lib/mutant/hooks.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: Builder, UnknownHook

Constant Summary collapse

DEFAULTS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%i[
  env_infection_pre
  env_infection_post
  mutation_insert_post
  mutation_insert_pre
].product([EMPTY_ARRAY]).to_h.transform_values(&:freeze).freeze
MESSAGE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

'Unknown hook %s'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.assert_name(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



20
21
22
23
# File 'lib/mutant/hooks.rb', line 20

def self.assert_name(name)
  fail UnknownHook, MESSAGE % name.inspect unless DEFAULTS.key?(name)
  self
end

.emptyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



25
26
27
# File 'lib/mutant/hooks.rb', line 25

def self.empty
  new(DEFAULTS)
end

.load_config(config) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:enable Security/Eval



71
72
73
74
75
# File 'lib/mutant/hooks.rb', line 71

def self.load_config(config)
  config.hooks.reduce(empty) do |current, path|
    current.merge(load_pathname(path))
  end
end

.load_pathname(pathname) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Security/Eval



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

def self.load_pathname(pathname)
  hooks = Builder.new

  binding.eval(pathname.read, pathname.to_s)

  hooks.to_hooks
end

Instance Method Details

#merge(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



29
30
31
32
33
# File 'lib/mutant/hooks.rb', line 29

def merge(other)
  self.class.new(
    other.map.merge(map) { |_key, new, old| (old + new).freeze }.freeze
  )
end

#run(name, payload) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



35
36
37
38
39
40
41
# File 'lib/mutant/hooks.rb', line 35

def run(name, payload)
  Hooks.assert_name(name)

  map.fetch(name).each { |block| block.call(payload) }

  self
end