Class: Autumn::Speciator

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

Overview

The Speciator stores the global, season, stem, and leaf configurations. It generates composite hashes, so that any leaf or stem can know its specific configuration as a combination of its options and those of the scopes above it.

Smaller scopes override larger ones; any season-specific options will replace global options, and leaf or stem options will overwrite season options. Leaf and stem options are independent from each other, however, since leaves and stems share a many-to-many relationship.

Option identifiers can be specified as strings or symbols but are always stored as symbols and never accessed as strings.

This is a singleton class; only one instance of it exists for any Autumn process. However, for the sake of convenience, many other objects use a config attribute containing the instance.

Instance Method Summary collapse

Constructor Details

#initializeSpeciator

Creates a new instance storing no options.



30
31
32
33
34
35
# File 'lib/autumn/speciator.rb', line 30

def initialize
  @global_options = Hash.new
  @season_options = Hash.new
  @stem_options = Hash.autonew
  @leaf_options = Hash.autonew
end

Instance Method Details

#[](sym) ⇒ Object

Returns the global-scope or season-scope config option with the given symbol. Season-scope config options will override global ones.



40
41
42
# File 'lib/autumn/speciator.rb', line 40

def [](sym)
  @season_options[sym] or @global_options[sym]
end

#all_leaf_classesObject

Returns an array of all leaf class names in use.



117
118
119
# File 'lib/autumn/speciator.rb', line 117

def all_leaf_classes
  @leaf_options.values.collect { |opts| opts[:class] }.uniq
end

#each_leafObject

Yields each leaf identifier and its options.



111
112
113
# File 'lib/autumn/speciator.rb', line 111

def each_leaf
  @leaf_options.each { |leaf, options| yield leaf, options }
end

#each_stemObject

Yields each stem identifier and its options.



105
106
107
# File 'lib/autumn/speciator.rb', line 105

def each_stem
  @stem_options.each { |stem, options| yield stem, options }
end

#global(arg) ⇒ Object

When called with a hash: Takes a hash of options and values, and sets them at the global scope level.

When called with an option identifier: Returns the value for that option at the global scope level.



50
51
52
# File 'lib/autumn/speciator.rb', line 50

def global(arg)
  arg.kind_of?(Hash) ? @global_options.update(arg.rekey(&:to_sym)) : @global_options[arg]
end

#leaf(leaf, arg) ⇒ Object

When called with a hash: Takes a hash of options and values, and sets them at the leaf scope level.

When called with an option identifier: Returns the value for that option exclusively at the leaf scope level.

The identifier for the leaf must be specified.



99
100
101
# File 'lib/autumn/speciator.rb', line 99

def leaf(leaf, arg)
  arg.kind_of?(Hash) ? @leaf_options[leaf].update(arg.rekey(&:to_sym)) : @leaf_options[leaf][arg]
end

#leaf?(leaf) ⇒ Boolean

Returns true if the given identifier is a known leaf identifier.

Returns:

  • (Boolean)


87
88
89
# File 'lib/autumn/speciator.rb', line 87

def leaf?(leaf)
  return !@leaf_options[leaf].nil?
end

#options_for_leaf(identifier) ⇒ Object

Returns the composite options for a leaf (by identifier), as an amalgamation of all the scope levels’ options.



131
132
133
# File 'lib/autumn/speciator.rb', line 131

def options_for_leaf(identifier)
  OptionsProxy.new(@global_options, @season_options, @leaf_options[identifier])
end

#options_for_stem(identifier) ⇒ Object

Returns the composite options for a stem (by identifier), as an amalgamation of all the scope levels’ options.



124
125
126
# File 'lib/autumn/speciator.rb', line 124

def options_for_stem(identifier)
  OptionsProxy.new(@global_options, @season_options, @stem_options[identifier])
end

#season(arg) ⇒ Object

When called with a hash: Takes a hash of options and values, and sets them at the season scope level.

When called with an option identifier: Returns the value for that option exclusively at the season scope level.

Since Autumn can only be run in one season per process, there is no need to store the options of specific seasons, only the current season.



63
64
65
# File 'lib/autumn/speciator.rb', line 63

def season(arg)
  arg.kind_of?(Hash) ? @season_options.update(arg.rekey(&:to_sym)) : @season_options[arg]
end

#stem(stem, arg) ⇒ Object

When called with a hash: Takes a hash of options and values, and sets them at the stem scope level.

When called with an option identifier: Returns the value for that option exclusively at the stem scope level.

The identifier for the stem must be specified.



81
82
83
# File 'lib/autumn/speciator.rb', line 81

def stem(stem, arg)
  arg.kind_of?(Hash) ? @stem_options[stem].update(arg.rekey(&:to_sym)) : @stem_options[stem][arg]
end

#stem?(stem) ⇒ Boolean

Returns true if the given identifier is a known stem identifier.

Returns:

  • (Boolean)


69
70
71
# File 'lib/autumn/speciator.rb', line 69

def stem?(stem)
  return !@stem_options[stem].nil?
end