Sha256: bd2304e2da50785887d4683366c0e70a14f867457f8d9042932d27ae4e787668

Contents?: true

Size: 972 Bytes

Versions: 3

Compression:

Stored size: 972 Bytes

Contents

module NxtSchema
  class ErrorMessages
    class << self
      def values
        @values ||= {}
      end

      def values=(value)
        @values = value
      end

      def load(paths = files)
        Array(paths).each do |path|
          new_values = YAML.load(ERB.new(File.read(path)).result).with_indifferent_access
          self.values = values.deep_merge!(new_values)
        end
      end

      def resolve(locale, key, **options)
        message = begin
          values.fetch(locale).fetch(key)
        rescue KeyError
          raise "Could not resolve error message for #{locale}->#{key}"
        end

        message % options
      end

      def files
        @files ||= begin
          files = Dir.entries(File.expand_path('../error_messages/', __FILE__)).map do |filename|
            File.expand_path("../error_messages/#{filename}", __FILE__)
          end

          files.select { |f| !File.directory? f }
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nxt_schema-0.1.2 lib/nxt_schema/error_messages.rb
nxt_schema-0.1.1 lib/nxt_schema/error_messages.rb
nxt_schema-0.1.0 lib/nxt_schema/error_messages.rb