Class: Autumn::Speciator
- Inherits:
-
Object
- Object
- Autumn::Speciator
- 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
-
#[](sym) ⇒ Object
Returns the global-scope or season-scope config option with the given symbol.
-
#all_leaf_classes ⇒ Object
Returns an array of all leaf class names in use.
-
#each_leaf ⇒ Object
Yields each leaf identifier and its options.
-
#each_stem ⇒ Object
Yields each stem identifier and its options.
-
#global(arg) ⇒ Object
When called with a hash: Takes a hash of options and values, and sets them at the global scope level.
-
#initialize ⇒ Speciator
constructor
Creates a new instance storing no options.
-
#leaf(leaf, arg) ⇒ Object
When called with a hash: Takes a hash of options and values, and sets them at the leaf scope level.
-
#leaf?(leaf) ⇒ Boolean
Returns true if the given identifier is a known leaf identifier.
-
#options_for_leaf(identifier) ⇒ Object
Returns the composite options for a leaf (by identifier), as an amalgamation of all the scope levels’ options.
-
#options_for_stem(identifier) ⇒ Object
Returns the composite options for a stem (by identifier), as an amalgamation of all the scope levels’ options.
-
#season(arg) ⇒ Object
When called with a hash: Takes a hash of options and values, and sets them at the season scope level.
-
#stem(stem, arg) ⇒ Object
When called with a hash: Takes a hash of options and values, and sets them at the stem scope level.
-
#stem?(stem) ⇒ Boolean
Returns true if the given identifier is a known stem identifier.
Constructor Details
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_classes ⇒ Object
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_leaf ⇒ Object
Yields each leaf identifier and its options.
111 112 113 |
# File 'lib/autumn/speciator.rb', line 111 def each_leaf @leaf_options.each { |leaf, | yield leaf, } end |
#each_stem ⇒ Object
Yields each stem identifier and its options.
105 106 107 |
# File 'lib/autumn/speciator.rb', line 105 def each_stem @stem_options.each { |stem, | yield stem, } 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.
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 (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 (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.
69 70 71 |
# File 'lib/autumn/speciator.rb', line 69 def stem?(stem) return !@stem_options[stem].nil? end |