Sha256: 1d4fd636281880e279c1d1d8f8e537191f6d1af94e1cc863fad99f76e2e8a6ac

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

require "yaml"

# This class wraps around the YAML files and performs the parsing/applying of
# the config values read from them.
module Conflate
  class Conflation
    attr_accessor :yaml_path, :config_object

    # Public: Initialize a new Conflation
    #
    # yaml_path - Path to the YAML file to read config information from
    # config_object - Object to receive the configuration values (e.g., Rails.application.config)
    def initialize(yaml_path, config_object)
      self.yaml_path = yaml_path
      self.config_object = config_object
    end

    # Public: Add the contents of the YAML file to the config object
    def apply
      if config_object.respond_to?(name) && !config_object.public_send(name).nil?
        # doing this to properly handle the slightly different behaviors of
        # OpenStruct (which does respond to unassigned attributes) or the
        # Rails.application.config object (which doesn't)
        warn "#{config_object.class} already has a #{config_object.public_send(name).class} for the key '#{name}'. Skipping conflating it with the contents of #{File.basename yaml_path}."
        return # so don't set it
      end

      config_object.public_send "#{name}=", data
    end

    # Public: The name of the conflation, based on the YAML file name
    #
    # Returns a String
    def name
      File.basename(yaml_path, ".yml")
    end

    # Private: The parsed data from the YAML file
    def data
      YAML.load ERB.new(File.read File.expand_path yaml_path).result
    rescue StandardError, SyntaxError => e
      {}
    end
    private :data

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
conflate-0.0.3 lib/conflate/conflation.rb