Class: PgDice::ConfigurationFileLoader
- Inherits:
-
Object
- Object
- PgDice::ConfigurationFileLoader
- Extended by:
- Forwardable
- Includes:
- LogHelper
- Defined in:
- lib/pgdice/configuration_file_loader.rb
Overview
ConfigurationFileLoader is a class used to load the PgDice configuration file
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #file_loaded? ⇒ Boolean
-
#initialize(config = PgDice::Configuration.new, opts = {}) ⇒ ConfigurationFileLoader
constructor
A new instance of ConfigurationFileLoader.
- #load_file ⇒ Object
Methods included from LogHelper
#blank?, log_duration, #squish
Constructor Details
#initialize(config = PgDice::Configuration.new, opts = {}) ⇒ ConfigurationFileLoader
Returns a new instance of ConfigurationFileLoader.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/pgdice/configuration_file_loader.rb', line 13 def initialize(config = PgDice::Configuration.new, opts = {}) @config = config @file_validator = opts[:file_validator] ||= lambda do |config_file| validate_file(config_file) end @config_loader = opts[:config_loader] ||= lambda do |file| logger.debug { "Loading PgDice configuration file: '#{config_file}'" } YAML.safe_load(ERB.new(File.read(file)).result) end @file_loaded = opts[:file_loaded] end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
9 10 11 |
# File 'lib/pgdice/configuration_file_loader.rb', line 9 def config @config end |
Instance Method Details
#file_loaded? ⇒ Boolean
39 40 41 |
# File 'lib/pgdice/configuration_file_loader.rb', line 39 def file_loaded? @file_loaded end |
#load_file ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/pgdice/configuration_file_loader.rb', line 25 def load_file return if @file_loaded @file_loaded = true @file_validator.call(config_file) @config.approved_tables = @config_loader.call(config_file) .fetch('approved_tables') .reduce(tables(@config)) do |tables, hash| tables << PgDice::Table.from_hash(hash) end end |