Class: Config
- Inherits:
-
Object
- Object
- Config
- Defined in:
- lib/ccios/config.rb
Instance Attribute Summary collapse
-
#templates_collection ⇒ Object
readonly
Returns the value of attribute templates_collection.
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(config_hash, source_path = nil) ⇒ Config
constructor
A new instance of Config.
- #variables_for_template(template) ⇒ Object
- #variables_for_template_element(template, element_name, element_default_variables = {}) ⇒ Object
Constructor Details
#initialize(config_hash, source_path = nil) ⇒ Config
Returns a new instance of Config.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ccios/config.rb', line 26 def initialize(config_hash, source_path = nil) @variables = config_hash["variables"] || {} @templates_collection = config_hash["templates_collection"] || nil @templates_config = {} raise "Invalid \"templates_collection\" in config, should be a string" unless @templates_collection.is_a?(String) || @templates_collection.nil? templates_config = config_hash["templates_config"] || {} raise "Invalid \"templates_config\" in configuration, it should be a dictionary" unless templates_config.is_a?(Hash) templates_config.each do |key, hash| raise "Invalid template configuration for \"#{key}\"" unless hash.is_a?(Hash) template_config = TemplateConfig.new(hash) @templates_config[key] = template_config end end |
Instance Attribute Details
#templates_collection ⇒ Object (readonly)
Returns the value of attribute templates_collection.
5 6 7 |
# File 'lib/ccios/config.rb', line 5 def templates_collection @templates_collection end |
#variables ⇒ Object (readonly)
Returns the value of attribute variables.
5 6 7 |
# File 'lib/ccios/config.rb', line 5 def variables @variables end |
Class Method Details
.default ⇒ Object
18 19 20 |
# File 'lib/ccios/config.rb', line 18 def self.default self.new default_config_hash end |
.default_config_hash ⇒ Object
22 23 24 |
# File 'lib/ccios/config.rb', line 22 def self.default_config_hash {} end |
.parse(source_path) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/ccios/config.rb', line 7 def self.parse(source_path) if File.exist?(source_path) config = YAML.load_file(source_path) raise "Invalid config file" unless config.is_a?(Hash) self.new config, source_path else puts "File #{source_path} does not exist. Using default config." self.default end end |
Instance Method Details
#variables_for_template(template) ⇒ Object
42 43 44 45 |
# File 'lib/ccios/config.rb', line 42 def variables_for_template(template) template_config = @templates_config[template.name] || TemplateConfig.new({}) @variables.merge(template.variables).merge(template_config.variables) end |
#variables_for_template_element(template, element_name, element_default_variables = {}) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/ccios/config.rb', line 47 def variables_for_template_element(template, element_name, element_default_variables = {}) template_config = @templates_config[template.name] || TemplateConfig.new({}) element_config = template_config.element_configuration_for(element_name) template_variables = @variables.merge(template.variables).merge(template_config.variables) template_variables.merge(element_default_variables).merge(element_config.variables) end |