class Jam_Func

Public Class Methods

new(*args) click to toggle source
# File lib/Jam_Func.rb, line 37
def initialize *args
  @ons      = {}
  @on_errs  = {}

  # generate .includes table (array)
  @includes = args.flatten.uniq

  # include itself in .includes
  @includes.push(self)
end

Public Instance Methods

entire_list_for(name) click to toggle source
# File lib/Jam_Func.rb, line 94
def entire_list_for(name)
  [
    list_with_includes('before ' + name),
    list_with_includes(name),
    list_with_includes('after '  + name)
  ]
  .flatten
end
events_for(raw_name) click to toggle source
# File lib/Jam_Func.rb, line 66
def events_for raw_name
  @ons[canon_name(raw_name)]
end
list(raw_name, create_if_needed = false) click to toggle source
# File lib/Jam_Func.rb, line 85
def list(raw_name, create_if_needed = false)
  name = canon_name(raw_name);
  if !@ons[name] && create_if_needed
    @ons[name] = []
  end

  return @ons[name] || []
end
list_with_includes(raw_name) click to toggle source
# File lib/Jam_Func.rb, line 103
def list_with_includes raw_name
  @includes.map { |t|
    (t == self) ?
      t.list(raw_name) :
      t.list_with_includes(raw_name)
  }
  .flatten
end
on(raw_name, func) click to toggle source
# File lib/Jam_Func.rb, line 48
def on(raw_name, func)
  name = canon_name(raw_name)

  if !@ons[name]
    @ons[name] = {}
  end

  list = @ons[name]
  list.push func

  self
end
on_error(raw_name, func) click to toggle source
# File lib/Jam_Func.rb, line 61
def on_error raw_name, func
  @on_errs[canon_name(raw_name)] = func
  self
end
run(*args) click to toggle source
# File lib/Jam_Func.rb, line 70
def run *args
  args
  .map { |v|

    v.kind_of?(String) ?
      self.events_for(v) :
      v

  }
  .flatten
  .each { |f| f() }

  self
end
run_error(*args) click to toggle source
# File lib/Jam_Func.rb, line 121
def run_error *args
  raise("Error handlers not found for: " + args[0]) if entire_list_for(args[0]).empty?
  run *args
end