Class: Code::Node::OrKeyword

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

Constant Summary collapse

OR_KEYWORD =
"or"
AND_KEYWORD =
"and"

Instance Method Summary collapse

Constructor Details

#initialize(or_keyword) ⇒ OrKeyword

Returns a new instance of OrKeyword.



7
8
9
10
11
12
13
# File 'lib/code/node/or_keyword.rb', line 7

def initialize(or_keyword)
  @first = ::Code::Node::Statement.new(or_keyword.fetch(:first))
  @rest = or_keyword.fetch(:rest)
  @rest.map! do |operation|
    ::Code::Node::Operation::Operation.new(operation)
  end
end

Instance Method Details

#evaluate(**args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/code/node/or_keyword.rb', line 15

def evaluate(**args)
  object = @first.evaluate(**args)

  @rest.each do |operation|
    if operation.operator == OR_KEYWORD
      return object if object.truthy?
    elsif operation.operator == AND_KEYWORD
      return object unless object.truthy?
    else
      raise NotImplementedError.new(operation.operator.inspect)
    end

    object = operation.statement.evaluate(**args)
  end

  object
end