Class: Jam_Func

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/Jam_Func.rb

Defined Under Namespace

Modules: DSL, Helpers Classes: Jam, Run

Constant Summary

Constants included from Helpers

Helpers::WHITE

Instance Method Summary collapse

Methods included from Helpers

#canon_name

Constructor Details

#initialize(*args) ⇒ Jam_Func

Returns a new instance of Jam_Func.



37
38
39
40
41
42
43
44
45
46
# 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

Instance Method Details

#entire_list_for(name) ⇒ Object



94
95
96
97
98
99
100
101
# 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) ⇒ Object



66
67
68
# 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) ⇒ Object



85
86
87
88
89
90
91
92
# 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) ⇒ Object



103
104
105
106
107
108
109
110
# 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(*args) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# 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) ⇒ Object



61
62
63
64
# File 'lib/Jam_Func.rb', line 61

def on_error raw_name, func
  @on_errs[canon_name(raw_name)] = func
  self
end

#run(*args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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) ⇒ Object



121
122
123
124
# 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