Module: Metro::Scenes

Extended by:
Scenes
Included in:
Scenes
Defined in:
lib/metro/scenes.rb

Overview

Examples:

Finding a scene based on the scene name


class IntroScene < Metro::Scene
  # ... scene content ...
end

Scenes.find("intro")    # => IntroScene
Scenes.find(:intro)     # => IntroScene

Creating a scene instance based on the scene name


class IntroScene < Metro::Scene
  # ... scene content ...
end

Scenes.generate("intro")    # => [SCENE: title]
Scenes.generate(:intro)     # => [SCENE: title]

Finding a scene that does not exist


scene = Scenes.find(:unknown)
scene.missing_scene            # => :unknown

Instance Method Summary collapse

Instance Method Details

#add(scene) ⇒ Object

Add a scene to the hash of scenes with the scene name of the scene as the key to retrieving this scene.

Parameters:

  • scene (Scene)

    the scene to be added to the hash of Scenes.



36
37
38
# File 'lib/metro/scenes.rb', line 36

def add(scene)
  all_scenes_for(scene).each { |scene| scenes_hash[scene.scene_name] = scene.to_s }
end

#find(scene_name) ⇒ Object

Finds the scene based on the specified scene name.

Parameters:

  • scene_name (String, Symbol)

    the name of the scene to locate.

Returns:

  • the Scene class that is found matching the specified scene name.



46
47
48
# File 'lib/metro/scenes.rb', line 46

def find(scene_name)
  scenes_hash[scene_name].constantize
end

#generate(scene_or_scene_name, options = {}) ⇒ Object

Finds the scene with the specified name and then creates an instance of that scene.

Parameters:

  • scene_name (String, Symbol, Object)

    the name of the scene to locate.

Returns:

  • an instance of Scene that is found matching the specified scene name



64
65
66
67
# File 'lib/metro/scenes.rb', line 64

def generate(scene_or_scene_name,options = {})
  new_scene = generate_scene_from(scene_or_scene_name)
  apply_post_filters(new_scene,options)
end

#generate_scene_from(scene_or_scene_name) ⇒ Object

If we have been given a scene, then we simply want to use it otherwise we need to find and generate our scene from the scene name.

Parameters:

  • scene_or_scene_name (String, Sybmol, Class)

    the name of the scene or an instance of Scene.



76
77
78
79
80
81
82
# File 'lib/metro/scenes.rb', line 76

def generate_scene_from(scene_or_scene_name)
  if scene_or_scene_name.is_a? Scene
    scene_or_scene_name
  else
    find(scene_or_scene_name).new
  end
end

#listArray<String>

Returns all the names supported by the scenes hash.

Returns:

  • (Array<String>)

    all the names supported by the scenes hash.



53
54
55
# File 'lib/metro/scenes.rb', line 53

def list
  scenes_hash.keys
end

#post_filtersObject

Post filters are applied to the scene after it has been found. These are all objects that can respond to the #filter method.



88
89
90
# File 'lib/metro/scenes.rb', line 88

def post_filters
  @post_filters ||= []
end

#register_post_filter(post_filter) ⇒ Object

Register a filter that will be executed after a scene is found and generated. This allows for the scene to be modified or changed based on the provided options.

A filter is any object that responds to #filter and accepts two parameters: the scene and a hash of options.

Parameters:

  • post_filter (#filter)

    a filter is an object that can act as a filter.



101
102
103
# File 'lib/metro/scenes.rb', line 101

def register_post_filter(post_filter)
  post_filters.push(post_filter)
end