Class: Bozo::ErubisTemplatingCoordinator
- Inherits:
-
Object
- Object
- Bozo::ErubisTemplatingCoordinator
- Defined in:
- lib/bozo/erubis_templating_coordinator.rb
Overview
Class for creating files based upon configuration files and ERB-style templates.
Overview
This class is primarily intended for generating config files with shared values but it is capable of generating whatever files you want.
Configuration files
Configuration files specify a hash of hashes in a more readable format. For example:
group :example do
set :one, 'foo'
set :two, 'bar'
end
Internally creates a hash like:
{:example => {:one => 'foo', :two => 'bar'}}
A configuration file can overwrite the values specified by a preceding one without error. Groups can be opened and closed as desired and nesting groups is possible.
Template files
To use a value within an ERB template you specify the hash hierarchy as if they were method names rather than having to use the full hash syntax.
Therefore, this is valid:
Foo is <%= example.one %>
Whilst this is not valid:
Foo is <%= self[:example][:one] %>
If a template uses a value that is not specified within the configuration then the hook will raise an error and halt the build.
Instance Method Summary collapse
-
#config_file(path) ⇒ Object
Adds a configuration file to the underlying configuration object if a file exists at the given path.
- #generate_binary_files(&block) ⇒ Object
-
#generate_files(&block) ⇒ Object
Generate all the files matching the underlying configuration.
-
#initialize(configuration = Bozo::Configuration.new) ⇒ ErubisTemplatingCoordinator
constructor
Create a new instance.
-
#required_config_file(path) ⇒ Object
Adds a required configuration file to the underlying configuration object.
-
#template_file(path) ⇒ Object
Adds a template file to use when generating files.
-
#template_files(glob) ⇒ Object
Adds a selection of template files to use when generating files.
Constructor Details
#initialize(configuration = Bozo::Configuration.new) ⇒ ErubisTemplatingCoordinator
Create a new instance.
53 54 55 56 |
# File 'lib/bozo/erubis_templating_coordinator.rb', line 53 def initialize(configuration = Bozo::Configuration.new) @templates = [] @configuration = configuration end |
Instance Method Details
#config_file(path) ⇒ Object
Adds a configuration file to the underlying configuration object if a file exists at the given path.
73 74 75 |
# File 'lib/bozo/erubis_templating_coordinator.rb', line 73 def config_file(path) @configuration.load path if File.exist? path end |
#generate_binary_files(&block) ⇒ Object
107 108 109 |
# File 'lib/bozo/erubis_templating_coordinator.rb', line 107 def generate_binary_files(&block) @templates.each {|template| generate_file template, block, true} end |
#generate_files(&block) ⇒ Object
Generate all the files matching the underlying configuration.
98 99 100 |
# File 'lib/bozo/erubis_templating_coordinator.rb', line 98 def generate_files(&block) @templates.each {|template| generate_file template, block, false} end |
#required_config_file(path) ⇒ Object
Adds a required configuration file to the underlying configuration object.
63 64 65 66 |
# File 'lib/bozo/erubis_templating_coordinator.rb', line 63 def required_config_file(path) raise RuntimeError.new "Required config file #{path} could not be found" unless File.exist? path config_file path end |
#template_file(path) ⇒ Object
Adds a template file to use when generating files.
81 82 83 |
# File 'lib/bozo/erubis_templating_coordinator.rb', line 81 def template_file(path) @templates << path end |
#template_files(glob) ⇒ Object
Adds a selection of template files to use when generating files.
89 90 91 |
# File 'lib/bozo/erubis_templating_coordinator.rb', line 89 def template_files(glob) @templates = @templates + Dir[glob] end |