Class: Datacaster::Runtimes::UserContext::ContextStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/datacaster/runtimes/user_context.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, node) ⇒ ContextStruct

Returns a new instance of ContextStruct.



11
12
13
14
# File 'lib/datacaster/runtimes/user_context.rb', line 11

def initialize(context, node)
  @context = context
  @node = node
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/datacaster/runtimes/user_context.rb', line 16

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

  if self.class.context_has_key?(@context, m) && 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

Class Method Details

.context_has_key?(context, key) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/datacaster/runtimes/user_context.rb', line 7

def self.context_has_key?(context, key)
  context.respond_to?(:key?) && context.key?(key) || context.to_h.key?(key.to_sym)
end

Instance Method Details

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/datacaster/runtimes/user_context.rb', line 36

def has_key?(key)
  self.class.context_has_key?(@context, key) || @node.class.send_to_parent(@node, :context).has_key?(key)
rescue NoMethodError
  false
end