Class: AngryMob::Builder

Inherits:
Object show all
Includes:
Log
Defined in:
lib/angry_mob/builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

#__class_to_s, #__class_to_s!, #debug, #log

Instance Attribute Details

#node_consolidation_blockObject (readonly)

Returns the value of attribute node_consolidation_block.



13
14
15
# File 'lib/angry_mob/builder.rb', line 13

def node_consolidation_block
  @node_consolidation_block
end

Class Method Details

.from_file(path) ⇒ Object



8
9
10
11
# File 'lib/angry_mob/builder.rb', line 8

def self.from_file(path)
  path = Pathname(path)
  new.from_file(path)
end

Instance Method Details

#act(name, definition_file = nil, &blk) ⇒ Object

Defines an ‘act` block



60
61
62
63
# File 'lib/angry_mob/builder.rb', line 60

def act(name, definition_file=nil, &blk)
  definition_file ||= file
  acts[name.to_sym] = [blk,definition_file.dup,false]
end

#act_helper(&blk) ⇒ Object



70
71
72
# File 'lib/angry_mob/builder.rb', line 70

def act_helper(&blk)
  helper_mod.module_eval(&blk)
end

#consolidate_node(&blk) ⇒ Object



87
88
89
# File 'lib/angry_mob/builder.rb', line 87

def consolidate_node(&blk)
  @node_consolidation_block = blk
end

#fileObject



15
16
17
18
19
20
21
# File 'lib/angry_mob/builder.rb', line 15

def file
  if @file
    @file
  else
    '<no-file>'
  end
end

#finalise(*act_names, &blk) ⇒ Object



74
75
76
# File 'lib/angry_mob/builder.rb', line 74

def finalise(*act_names, &blk)
  act_names.norm.each {|a| act("finalise/#{a}",&blk)}
end

#from_file(path) ⇒ Object

read and evaluate a file in builder context



24
25
26
27
28
29
# File 'lib/angry_mob/builder.rb', line 24

def from_file(path)
  @file = path
  instance_eval path.read, path.to_s
  @file = nil
  self
end

#multi_act(name, definition_file = nil, &blk) ⇒ Object



65
66
67
68
# File 'lib/angry_mob/builder.rb', line 65

def multi_act(name, definition_file=nil, &blk)
  definition_file ||= file
  acts[name.to_sym] = [blk,definition_file.dup,true]
end

#node_defaults(&blk) ⇒ Object

Defaults



92
93
94
# File 'lib/angry_mob/builder.rb', line 92

def node_defaults(&blk)
  node_default_blocks << blk
end

#notifications_for(name, &blk) ⇒ Object



78
79
80
# File 'lib/angry_mob/builder.rb', line 78

def notifications_for(name,&blk)
  act("notifications_for/#{name}") { notifications.for(name, :context => self, &blk) }
end

#setup_node(&blk) ⇒ Object

A ‘setup_node` block allows the mob to set defaults, load resource locators and anything else you like.



83
84
85
# File 'lib/angry_mob/builder.rb', line 83

def setup_node(&blk)
  node_setup_blocks << blk
end

#to_mobObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/angry_mob/builder.rb', line 31

def to_mob
  mob = Mob.new

  # pre-setup
  mob.setup_node = lambda {|node,defaults|
    node_setup_blocks.each {|blk| blk[node,defaults]}
  }

  # in-setup
  mob.node_defaults = lambda {|node,defaults|
    node_default_blocks.each {|blk| blk[node,defaults]}
  }

  # post-setup
  mob.consolidate_node = @node_consolidation_block

  # create and bind acts
  acts.each do |name,(blk,file,multi)|
    act = Act.new(name,multi,&blk)
    act.extend helper_mod 
    act.bind(mob,file)
  end

  mob
end