Sha256: 9df9321a1338472b065dc0d22c6959bff5e409ecfffe79ca0a9be34643f7be85

Contents?: true

Size: 1.14 KB

Versions: 5

Compression:

Stored size: 1.14 KB

Contents

require 'ostruct'

module Datacaster
  module Runtimes
    class UserContext < Base
      class ContextStruct
        def initialize(context, node)
          @context = context
          @node = node
        end

        def method_missing(m, *args)
          if args.length > 1 || block_given?
            return super
          end

          key_present = @context.respond_to?(:key?) && @context.key?(m) ||
            @context.to_h.key?(m.to_sym)

          if key_present && args.empty?
            return @context[m]
          end

          if m =~ /\A.+=\z/ && args.length == 1
            return @context[m[0..-2].to_sym] = args[0]
          end

          begin
            @node.class.send_to_parent(@node, :context).public_send(m, *args)
          rescue NoMethodError
            raise NoMethodError.new("Key #{m.inspect} is not found in the context")
          end
        end
      end

      def initialize(parent, user_context)
        super(parent)
        @context_struct = ContextStruct.new(user_context, self)

        @reserved_instance_variables += instance_variables
      end

      def context
        @context_struct
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
datacaster-4.0.1 lib/datacaster/runtimes/user_context.rb
datacaster-3.3.1 lib/datacaster/runtimes/user_context.rb
datacaster-3.3.0 lib/datacaster/runtimes/user_context.rb
datacaster-3.2.7 lib/datacaster/runtimes/user_context.rb
datacaster-3.2.6 lib/datacaster/runtimes/user_context.rb