Sha256: 75f98a97a577d460a36e8b9f9b524d69382d29e56d8de36c5cbc0b2c90ae0586

Contents?: true

Size: 742 Bytes

Versions: 8

Compression:

Stored size: 742 Bytes

Contents

# frozen_string_literal: true

class Code
  class Object
    class Context < Dictionary
      attr_reader :parent

      def initialize(raw = {}, parent: nil)
        raw = raw.raw if raw.is_a?(Dictionary)
        @raw = raw.to_h
        @parent = parent
      end

      def self.name
        "Context"
      end

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

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

      def parent?
        !!parent
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
code-ruby-0.11.0 lib/code/object/context.rb
code-ruby-0.10.4 lib/code/object/context.rb
code-ruby-0.10.3 lib/code/object/context.rb
code-ruby-0.10.2 lib/code/object/context.rb
code-ruby-0.10.1 lib/code/object/context.rb
code-ruby-0.10.0 lib/code/object/context.rb
code-ruby-0.9.3 lib/code/object/context.rb
code-ruby-0.9.2 lib/code/object/context.rb