Sha256: e5b184a339fd7fe67da2b6452ab9d6ec8476cb19899d7075c28d29bb0e8e729d

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

require "active_support/core_ext/hash"

require "config_default/version"
require "config_default/config"
require "config_default/init"
require "config_default/struct"
require "config_default/rails_application_extension"
require "config_default/rails_application_configuration_extension"

module ConfigDefault
  extend self

  attr_accessor :config

  @config = ConfigDefault::Config.new

  def configure
    yield(config) if block_given?
  end

  def init_rails_monkey_patch!
    ConfigDefault::Init.init_rails_monkey_patch!
  end

  def load(name, key: Rails.env, symbolize_keys: false, deep_symbolize_keys: false)
    default_config = load_file("#{name}.#{config.postfix}")
    config = load_file(name)

    data = default_config.deep_merge(config)
    data = key ? data[key] : data

    return {} if data.nil?

    if deep_symbolize_keys
      data.deep_symbolize_keys
    elsif symbolize_keys
      data.symbolize_keys
    else
      data
    end
  end

  def load_file(name)
    file_name = File.join(config.config_path, "#{name}.yml")
    ActiveSupport::ConfigurationFile.parse(file_name)
  rescue
    {}
  end

  def load_struct(name, key: Rails.env, recursive: false, allow_nil: false)
    ConfigDefault::Struct.new(
      attributes: load(name, key: key),
      recursive: recursive,
      allow_nil: allow_nil,
    )
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
config_default-0.2.4 lib/config_default.rb