Sha256: 83929ab3d4bc42e2618c91d23d6edb5e32197b1a8f60646a36ee0f910801f55f

Contents?: true

Size: 756 Bytes

Versions: 2

Compression:

Stored size: 756 Bytes

Contents

# frozen_string_literal: true

class Code
  class Object
    class Context < Dictionary
      attr_reader :parent

      def initialize(*args, **_kargs, &_block)
        raw = args.first || Dictionary.new
        raw = raw.raw if raw.is_a?(Object)
        @raw = raw.to_h
        @parent = Context.new(args.second) if args.second
      end

      def lookup!(identifier)
        if code_has_key?(identifier).truthy?
          self
        elsif parent?
          parent.lookup!(identifier)
        else
            raise Error::Undefined, "#{identifier} is not defined"
        end
      end

      def merge(other)
        Context.new(raw.merge(other.raw), parent || other.parent)
      end

      def parent?
        !!parent
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
code-ruby-0.13.1 lib/code/object/context.rb
code-ruby-0.13.0 lib/code/object/context.rb