Class: Autumn::Foliater

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/autumn/foliater.rb

Overview

Loads Stems and Leaves and executes them in their own threads. Manages the threads and oversees all leaves. This is a singleton class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFoliater

:nodoc:



19
20
21
22
23
24
# File 'lib/autumn/foliater.rb', line 19

def initialize # :nodoc:
  @config = Speciator.instance
  @stems = Hash.new
  @leaves = Hash.new
  @ctcp = Autumn::CTCP.new
end

Instance Attribute Details

#configObject (readonly)

The Speciator singleton.



13
14
15
# File 'lib/autumn/foliater.rb', line 13

def config
  @config
end

#leavesObject (readonly)

A hash of all Leaf instances by their config names.



17
18
19
# File 'lib/autumn/foliater.rb', line 17

def leaves
  @leaves
end

#stemsObject (readonly)

A hash of all Stem instances by their config names.



15
16
17
# File 'lib/autumn/foliater.rb', line 15

def stems
  @stems
end

Instance Method Details

#alive?Boolean

Returns true if there is at least one stem still running.

Returns:

  • (Boolean)


56
57
58
# File 'lib/autumn/foliater.rb', line 56

def alive?
  @stem_threads and @stem_threads.any? { |name, thread| thread.alive? }
end

#each_leafObject

This method yields each Leaf subclass that was loaded, allowing you to iterate over each leaf. For instance, to take attendance:

Foliater.instance.each_leaf { |leaf| leaf.stems.message "Here!" }


74
75
76
# File 'lib/autumn/foliater.rb', line 74

def each_leaf
  @leaves.each { |leaf| yield leaf }
end

#each_stemObject

This method yields each Stem that was loaded, allowing you to iterate over each stem. For instance, to take attendance:

Foliater.instance.each_stem { |stem| stem.message "Here!" }


65
66
67
# File 'lib/autumn/foliater.rb', line 65

def each_stem
  @leaves.each { |leaf| yield leaf }
end

#hot_reload(leaf) ⇒ Object

Reloads a leaf while it is running. Re-opens class definition files and runs them to redefine the classes. Does not work exactly as it should, but well enough for a rough hot-reload capability.



46
47
48
49
50
51
52
# File 'lib/autumn/foliater.rb', line 46

def hot_reload(leaf)
  type = leaf.class.to_s.split('::').first
  load_leaf_controller type
  load_leaf_helpers type
  load_leaf_models leaf
  load_leaf_views type
end

#load(stem_config, leaf_config, invoke = true) ⇒ Object

Loads the config files and their classes, initializes all stems and leaves and begins the stems’ execution processes in their own threads. You must pass the stem and leaf config hashes (from the stems.yml and leaves.yml files).

If invoke is set to false, start_stems will not be called.



33
34
35
36
37
38
39
40
# File 'lib/autumn/foliater.rb', line 33

def load(stem_config, leaf_config, invoke=true)
  load_configs stem_config, leaf_config
  load_leaf_classes
  load_leaves
  load_all_leaf_models
  load_stems
  start_stems if invoke
end