Sha256: 86bf1ae3ea2925be8bd41b2a28810c605d5bf410c0c7491910a3ffb6e8dde1d2

Contents?: true

Size: 815 Bytes

Versions: 10

Compression:

Stored size: 815 Bytes

Contents

# frozen_string_literal: true

module Parametric
  class Top
    attr_reader :errors

    def initialize
      @errors = {}
    end

    def add_error(key, msg)
      errors[key] ||= []
      errors[key] << msg
    end
  end

  class Context
    def initialize(path = nil, top = Top.new)
      @top = top
      @path = Array(path).compact
    end

    def errors
      top.errors
    end

    def add_error(msg)
      top.add_error(string_path, msg)
    end

    def add_base_error(key, msg)
      top.add_error(key, msg)
    end

    def sub(key)
      self.class.new(path + [key], top)
    end

    protected
    attr_reader :path, :top

    def string_path
      path.reduce(['$']) do |m, segment|
        m << (segment.is_a?(Integer) ? "[#{segment}]" : ".#{segment}")
        m
      end.join
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
parametric-0.2.21 lib/parametric/context.rb
parametric-0.2.20 lib/parametric/context.rb
parametric-0.2.19 lib/parametric/context.rb
parametric-0.2.18 lib/parametric/context.rb
parametric-0.2.17 lib/parametric/context.rb
parametric-0.2.16 lib/parametric/context.rb
parametric-0.2.15 lib/parametric/context.rb
parametric-0.2.14 lib/parametric/context.rb
parametric-0.2.13 lib/parametric/context.rb
parametric-0.2.12 lib/parametric/context.rb