Class: BigqueryMigration::ConfigLoader
- Inherits:
-
Object
- Object
- BigqueryMigration::ConfigLoader
- Defined in:
- lib/bigquery_migration/config_loader.rb
Defined Under Namespace
Classes: AlreayIncluded
Instance Attribute Summary collapse
-
#config_path ⇒ Object
readonly
Returns the value of attribute config_path.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
Instance Method Summary collapse
-
#initialize(config_path, vars = {}) ⇒ ConfigLoader
constructor
A new instance of ConfigLoader.
- #load ⇒ Object
- #load_erb(path = config_path) ⇒ Object
Constructor Details
#initialize(config_path, vars = {}) ⇒ ConfigLoader
Returns a new instance of ConfigLoader.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/bigquery_migration/config_loader.rb', line 12 def initialize(config_path, vars = {}) @config_path = File.(config_path) @included_files = Set.new @namespace = OpenStruct.new(vars) unless @namespace.respond_to?(:include_file) itself = self # ToDo: better way? @namespace.define_singleton_method(:include_file) do |path| caller_path = caller[0][/^([^:]+):\d+:in `[^']*'$/, 1] abs_path = File.(path, File.dirname(caller_path)) if File.extname(path) == '.erb' itself.load_erb(abs_path) else File.read(abs_path) end end end end |
Instance Attribute Details
#config_path ⇒ Object (readonly)
Returns the value of attribute config_path.
8 9 10 |
# File 'lib/bigquery_migration/config_loader.rb', line 8 def config_path @config_path end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
8 9 10 |
# File 'lib/bigquery_migration/config_loader.rb', line 8 def namespace @namespace end |
Instance Method Details
#load ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/bigquery_migration/config_loader.rb', line 32 def load if File.extname(config_path) == '.erb' YAML.load(load_erb(config_path)) else YAML.load(File.read(config_path)) end end |
#load_erb(path = config_path) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/bigquery_migration/config_loader.rb', line 40 def load_erb(path = config_path) unless @included_files.add?(path) raise AlreayIncluded, "#{path} was included twice" end raw = File.read(path) erb = ERB.new(raw, nil, "-") erb.filename = path erb.result(namespace.instance_eval { binding }) end |