Sha256: 8b53cf39e3c4f86eccaed09895b73fa95cf049f7e9f3cea6b6412f90f1c2b9b1

Contents?: true

Size: 1.72 KB

Versions: 5

Compression:

Stored size: 1.72 KB

Contents

module Origen
  class SiteConfig
    require 'pathname'
    require 'yaml'

    def method_missing(method, *args, &block)
      method = method.to_s
      if method =~ /(.*)!$/
        method = Regexp.last_match(1)
        must_be_present = true
      end
      env = "ORIGEN_#{method.upcase}"
      if ENV.key?(env)
        val = ENV[env]
      else
        config = configs.find { |c| c.key?(method) }
        val = config ? config[method] : nil
      end
      if must_be_present && val.nil?
        puts "No value assigned for site_config attribute '#{method}'"
        puts
        fail 'Missing site_config value!'
      end
      define_singleton_method(method) do
        val
      end
      val
    end

    private

    def configs
      @configs ||= begin
        path = Pathname.pwd
        configs = []
        # Add any site_configs from where we are currently running from, i.e. the application
        # directory area
        until path.root?
          file = File.join(path, 'origen_site_config.yml')
          configs << YAML.load_file(file) if File.exist?(file)
          path = path.parent
        end
        # Add and any site_configs from the directory hierarchy where Ruby is installed
        path = Pathname.new($LOAD_PATH.last)
        until path.root?
          file = File.join(path, 'origen_site_config.yml')
          configs << YAML.load_file(file) if File.exist?(file)
          path = path.parent
        end
        # Add the one from the Origen core as the lowest priority, this one defines
        # the default values
        configs << YAML.load_file(File.expand_path('../../../origen_site_config.yml', __FILE__))
        configs
      end
    end
  end

  def self.site_config
    @site_config ||= SiteConfig.new
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
origen-0.0.6 lib/origen/site_config.rb
origen-0.0.5 lib/origen/site_config.rb
origen-0.0.4 lib/origen/site_config.rb
origen-0.0.3 lib/origen/site_config.rb
origen-0.0.2 lib/origen/site_config.rb