lib/localer/config.rb in localer-0.0.2 vs lib/localer/config.rb in localer-0.1.0
- old
+ new
@@ -1,22 +1,45 @@
# frozen_string_literal: true
require 'yaml'
+require_relative '../localer/ext/hash'
+require_relative 'config/locale'
-module Localer
+module Localer # :nodoc:
+ using Localer::Ext::Hash
+
# Loads and parse Localer config file `.localer.yml`
- class Config < Service
+ class Config
+ extend Dry::Initializer
+
+ APP_PATH = Dir.pwd
+ CONFIG_FILENAME = ".localer.yml"
+
option :exclude, default: -> { [] }
- def call
- return unless File.exist?(filename)
- yaml = YAML.load_file(filename)
- @exclude = yaml["Exclude"] if yaml&.key?("Exclude")
- self
- end
+ option :locale, proc { |hash| parse_locales(hash) }, default: -> { Hash.new(Locale.new) }
+ option :app_path, default: -> { APP_PATH }
- private
+ class << self
+ def load(options = {})
+ opts = options.deep_symbolize_keys
+ app_path = opts.fetch(:app_path, APP_PATH)
+ file_options = file_config(CONFIG_FILENAME, app_path)
+ new(file_options.deep_merge(opts).deep_symbolize_keys)
+ end
- def filename
- File.expand_path(".localer.yml", Dir.pwd)
+ def file_config(filename, path)
+ filename = File.expand_path(filename, path)
+ return {} unless File.exist?(filename)
+ YAML
+ .load_file(filename)
+ .deep_downcase_keys
+ .deep_symbolize_keys
+ end
+
+ def parse_locales(hash)
+ hash.each_with_object(Hash.new(Locale.new)) do |(l, v), h|
+ h[l] = Locale.new(v)
+ end
+ end
end
end
end