Sha256: 19b73f74900b018213f406eaff555ec168cce805fecaebe3f0a401085e55dcc0
Contents?: true
Size: 1.87 KB
Versions: 1
Compression:
Stored size: 1.87 KB
Contents
# frozen_string_literal: true module MessagesDictionary # This class contains all configuration params class Config using MessagesDictionary::Utils::StringUtils attr_reader :msgs, :output_target, :output_method, :transform, :file, :on_key_missing, :file_loader def initialize(opts, klass) @__klass = klass @__lazy = opts[:lazy] prepare_storage_for opts @on_key_missing = opts[:on_key_missing] || :raise @output_target = opts[:output] || $stdout @output_method = (opts[:method] || :puts).to_sym @transform = opts[:transform] load_messages end # This method loads messages from a file but respects the "lazy" option. # In other words, it does not load anything if "lazy" is "true". # To force messages loading, use the load_messages! method instead. def load_messages return if @__lazy do_load_messages! end # Loads messages from the file even if "lazy" is "true" def load_messages! do_load_messages! end private def do_load_messages! return if @__storage == :hash || @__loaded begin data = @file_loader.call @file rescue Errno::ENOENT raise Errno::ENOENT, "File #{@file} does not exist..." else @msgs = MessagesDictionary::Utils::Dict.new data @__loaded = true end end def prepare_storage_for(opts) if opts.key?(:messages) @__storage = :hash @msgs = MessagesDictionary::Utils::Dict.new opts[:messages] @__loaded = true else @__storage = :file @file = opts[:file] || "#{@__klass.name.nil? ? 'unknown' : @__klass.name.snakecase}.yml" @file = File.expand_path(@file, opts[:dir] || '.') @file_loader = opts[:file_loader] || ->(f) { YAML.load_file(f) } end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
messages_dictionary-2.1.0 | lib/messages_dictionary/config.rb |