Class: Bunto::Assets::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/bunto/assets/hook.rb

Defined Under Namespace

Classes: UnknownHookError

Constant Summary collapse

HOOK_ALIASES =

{
  :env => {
    :post_init => :init,
    :pre_init  => :init
  }
}
HOOK_POINTS =

{
  :env => [
    :init
  ]
}

Class Method Summary collapse

Class Method Details

.allObject




35
36
37
# File 'lib/bunto/assets/hook.rb', line 35

def self.all
  @_all ||= {}
end

.point(base, point, when_ = :late) ⇒ Object




56
57
58
59
60
61
62
63
# File 'lib/bunto/assets/hook.rb', line 56

def self.point(base, point, when_ = :late)
  point = all[base][point] ||= {
    :early => Set.new,
    :late  => Set.new
  }

  point[when_]
end

.register(base, point, when_ = :late, &block) ⇒ Object


Raises:



67
68
69
70
71
72
73
74
75
# File 'lib/bunto/assets/hook.rb', line 67

def self.register(base, point, when_ = :late, &block)
  raise UnknownHookError, base: base unless HOOK_POINTS.key?(base)
  point = HOOK_ALIASES[base][point] if HOOK_ALIASES.fetch(base, {}).key?(point)
  raise UnknownHookError, point: point unless HOOK_POINTS[base].include?(point)
  all[base] ||= {}

  point(base, point, when_) \
    .add(block)
end

.trigger(base, point_, *args, &block) ⇒ Object


Trigger a hook, giving an optional block where we pass you the, proc we got and then you can do as you please (such as instance eval) but if you do not give us one then we simply pass the args.


Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
# File 'lib/bunto/assets/hook.rb', line 45

def self.trigger(base, point_, *args, &block)
  raise ArgumentError, "Do not give args with a block" if args.size > 0 && block_given?
  if all.key?(base) && all[base].key?(point_)
    Set.new.merge(point(base, point_, :early)).merge(point(base, point_)).map do |v|
      block_given?? block.call(v) : v.call(*args)
    end
  end
end