Class: Ez::Resources::Manager::Hooks

Inherits:
Object
  • Object
show all
Defined in:
lib/ez/resources/manager/hooks.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Hooks

Returns a new instance of Hooks.



26
27
28
29
30
# File 'lib/ez/resources/manager/hooks.rb', line 26

def initialize(&block)
  @hooks = []

  instance_eval(&block)
end

Instance Attribute Details

#hooksObject (readonly)

Returns the value of attribute hooks.



9
10
11
# File 'lib/ez/resources/manager/hooks.rb', line 9

def hooks
  @hooks
end

Class Method Details

.can!(name, config, data = nil) ⇒ Object

Raises:



11
12
13
14
15
16
17
18
# File 'lib/ez/resources/manager/hooks.rb', line 11

def self.can!(name, config, data = nil)
  return true if config.hooks.empty?
  if config.hooks.select { |h| h.name == name }.map { |h| h.callback.call(config.controller, data) }.all?(true)
    return true
  end

  raise UnavailableError, "Negative #{name}"
end

.can?(name, config, data = nil) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/ez/resources/manager/hooks.rb', line 20

def self.can?(name, config, data = nil)
  return true if config.hooks.empty?

  config.hooks.select { |h| h.name == name }.map { |h| h.callback.call(config.controller, data) }.all?(true)
end

Instance Method Details

#add(name, callback) ⇒ Object



32
33
34
# File 'lib/ez/resources/manager/hooks.rb', line 32

def add(name, callback)
  @hooks << Hook.new(name: name, callback: callback)
end