Class: Code::Node::Dictionary::KeyValue

Inherits:
Code::Node show all
Defined in:
lib/code/node/dictionary.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Node

#resolve

Constructor Details

#initialize(parsed) ⇒ KeyValue

Returns a new instance of KeyValue.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/code/node/dictionary.rb', line 9

def initialize(parsed)
  return if parsed.blank?

  if (name_code = parsed.delete(:name_code))
    if name_code.key?(:code)
      @key =
        Node::String.new([{ text: name_code.delete(:name).presence }])
      @value = Node::Code.new(name_code.delete(:code).presence)
      @resolve_key = false
    else
      @key = Node::Call.new({ name: name_code.delete(:name).presence })
      @value = @key
      @resolve_key = true
    end
  elsif (statement_code = parsed.delete(:statement_code))
    @key =
      Node::Statement.new(statement_code.delete(:statement).presence)

    if statement_code.key?(:code)
      @value = Node::Code.new(statement_code.delete(:code).presence)
      @resolve_key = false
    else
      @value = @key
      @resolve_key = true
    end
  else
    @key = @value = Node::Code.new(parsed.delete(:code).presence)
    @resolve_key = true
  end
end

Instance Attribute Details

#resolve_keyObject (readonly)

Returns the value of attribute resolve_key.



7
8
9
# File 'lib/code/node/dictionary.rb', line 7

def resolve_key
  @resolve_key
end

Instance Method Details

#evaluate(**args) ⇒ Object



40
41
42
43
44
45
# File 'lib/code/node/dictionary.rb', line 40

def evaluate(**args)
  key = resolve_key? ? @key&.resolve(**args) : @key&.evaluate(**args)
  key ||= Object::Nothing.new
  value = @value.evaluate(**args)
  [key, value]
end

#resolve_key?Boolean

Returns:



47
48
49
# File 'lib/code/node/dictionary.rb', line 47

def resolve_key?
  !!resolve_key
end