Sha256: e17d5576567c2741195984fef09883d9719f404420d539e775f2ead1923a6aa4

Contents?: true

Size: 1.01 KB

Versions: 10

Compression:

Stored size: 1.01 KB

Contents

module Paradocs
  class Top
    attr_reader :errors

    def initialize
      @errors = {}
    end

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

  class Context
    attr_reader :environment
    def initialize(path=nil, top=Top.new, environment={}, subschemes={})
      @top = top
      @path = Array(path).compact
      @environment = environment
      @subschemes = subschemes
    end

    def subschema(subschema_name)
      subschema = @subschemes[subschema_name]
      return unless subschema
      @subschemes.merge!(subschema.subschemes)
      subschema
    end

    def errors
      top.errors
    end

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

    def sub(key)
      self.class.new(path + [key], top, environment, @subschemes)
    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
paradocs-1.1.6 lib/paradocs/context.rb
paradocs-1.1.5 lib/paradocs/context.rb
paradocs-1.1.4 lib/paradocs/context.rb
paradocs-1.1.3 lib/paradocs/context.rb
paradocs-1.1.2 lib/paradocs/context.rb
paradocs-1.1.1 lib/paradocs/context.rb
paradocs-1.1.0 lib/paradocs/context.rb
paradocs-1.0.24 lib/paradocs/context.rb
paradocs-1.0.23 lib/paradocs/context.rb
paradocs-1.0.22 lib/paradocs/context.rb