Sha256: b6463cb228174619decfaf5eaa923fabef8ffe15255dd85d3f9afb6d98e23b60

Contents?: true

Size: 1.29 KB

Versions: 22

Compression:

Stored size: 1.29 KB

Contents

require 'set'
require 'yaml'
require 'erb'
require 'ostruct'

class BigqueryMigration
  class ConfigLoader
    attr_reader :config_path, :namespace

    class AlreayIncluded < ::StandardError; end

    def initialize(config_path, vars = {})
      @config_path = File.expand_path(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.expand_path(path, File.dirname(caller_path))
          if File.extname(path) == '.erb'
            itself.load_erb(abs_path)
          else
            File.read(abs_path)
          end
        end
      end
    end

    def load
      if File.extname(config_path) == '.erb'
        YAML.load(load_erb(config_path))
      else
        YAML.load(File.read(config_path))
      end
    end

    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
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
bigquery_migration-0.3.2 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.3.1 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.3.0 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.2.2 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.2.1 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.2.0 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.2.0.pre2 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.2.0.pre1 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.1.7 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.1.6 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.1.5 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.1.4 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.1.3 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.1.2 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.1.1 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.1.0 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.1.0.pre6 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.1.0.pre5 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.1.0.pre4 lib/bigquery_migration/config_loader.rb
bigquery_migration-0.1.0.pre3 lib/bigquery_migration/config_loader.rb