Sha256: 86d246cd06e99732a2d06a158e6c7b5b304efa928bf58047914cf2c7bd00f196

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 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 = klass::DICTIONARY_CONF[:transform] || block
        transform ?
            transform.call(msg) :
            klass::DICTIONARY_CONF[:output].send(klass::DICTIONARY_CONF[:method].to_sym, msg)
      end
      private :pretty_output
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
messages_dictionary-0.0.2 lib/messages_dictionary/injector.rb
messages_dictionary-0.0.1 lib/messages_dictionary/injector.rb