Sha256: 39c5e6c90b006cbc0be1b3cdaab95a0a81a57758b2444093a7838d5c342e6758

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

module MessagesDictionary
  def self.included(klass)
    klass.class_exec do
      define_singleton_method :has_messages_dictionary do |opts = {}|
        if opts[:messages]
          messages = Dict.new(opts[:messages])
        else
          file = opts[:file] || "#{SpecialString.new(klass.name).snake_case}.yml"
          file = File.expand_path(file, opts[:dir]) if opts[:dir]
          begin
            messages = Dict.new(YAML.load_file(file))
          rescue Errno::ENOENT
            abort "File #{file} does not exist..."
          end
        end
        klass.const_set(:DICTIONARY_CONF, {msgs: messages.extend(Hashie::Extensions::DeepFetch),
                                           output: opts[:output] || STDOUT,
                                           method: opts[:method] || :puts,
                                           transform: opts[:transform]})
      end

      define_method :pretty_output do |key, values = {}, &block|
        msg = klass::DICTIONARY_CONF[:msgs].deep_fetch(*key.to_s.split('.')) do
          raise KeyError, "#{key} cannot be found in the provided file..."
        end
        values.each do |k, v|
          msg.gsub!(Regexp.new('\{\{' + k.to_s + '\}\}'), v.to_s)
        end
        transform = block || klass::DICTIONARY_CONF[:transform]
        transform ?
            transform.call(msg) :
            klass::DICTIONARY_CONF[:output].send(klass::DICTIONARY_CONF[:method].to_sym, msg)
      end
      private :pretty_output
      alias_method :pou, :pretty_output
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
messages_dictionary-1.0.0 lib/messages_dictionary/injector.rb
messages_dictionary-0.1.3.2 lib/messages_dictionary/injector.rb