Sha256: aa2c27e4c770b184ecdb2464aad52a31f3f255686cfa61dba6d536a1535afce1

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

require 'yaml'
require 'pathname'

require 'dry/validation/messages/abstract'

module Dry
  module Validation
    class Messages::YAML < Messages::Abstract
      attr_reader :data

      configure do |config|
        config.root = 'en.errors'.freeze
      end

      def self.load(path = config.path)
        new(load_file(path))
      end

      def self.load_file(path)
        flat_hash(YAML.load_file(path))
      end

      def self.flat_hash(h, f = [], g = {})
        return g.update(f.join('.'.freeze) => h) unless h.is_a? Hash
        h.each { |k, r| flat_hash(r, f + [k], g) }
        g
      end

      def initialize(data)
        @data = data
      end

      def get(key, _options = {})
        data[key]
      end

      def key?(key, *args)
        data.key?(key)
      end

      def merge(overrides)
        if overrides.is_a?(Hash)
          self.class.new(data.merge(self.class.flat_hash(overrides)))
        else
          self.class.new(data.merge(Messages::YAML.load_file(overrides)))
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-validation-0.3.0 lib/dry/validation/messages/yaml.rb