Class: BlocklyInterpreter::CoreBlocks::IfBlock

Inherits:
Block
  • Object
show all
Includes:
DSLGenerator
Defined in:
lib/blockly_interpreter/core_blocks/if_block.rb

Defined Under Namespace

Modules: DSLMethods Classes: Conditional

Instance Attribute Summary

Attributes inherited from Block

#block_type, #comment, #comment_pinned, #fields, #is_shadow, #mutation, #next_block, #statements, #values, #x, #y

Instance Method Summary collapse

Methods included from DSLGenerator

#deep_flatten, #formatted_keyword_args, #indent, #keyword_args_without_defaults, #method_call, #method_call_with_block_or_nothing, #method_call_with_possible_block, #start_block_to_dsl, #strip_trailing_whitespace, #timestamp_to_dsl

Methods inherited from Block

#each_block, #has_comment?, #has_position?, #initialize, #to_xml, #to_xml_element, #value

Constructor Details

This class inherits a constructor from BlocklyInterpreter::Block

Instance Method Details

#else_blockObject



32
33
34
# File 'lib/blockly_interpreter/core_blocks/if_block.rb', line 32

def else_block
  statements['ELSE']
end

#elseif_countObject



28
29
30
# File 'lib/blockly_interpreter/core_blocks/if_block.rb', line 28

def elseif_count
  @elseif_count ||= mutation.try(:[], 'elseif').try(:to_i) || 0
end

#execute_statement(execution_context) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/blockly_interpreter/core_blocks/if_block.rb', line 36

def execute_statement(execution_context)
  match = matching_predicate(execution_context)

  if match
    match.execute_statements(execution_context)
  elsif else_block
    execution_context.execute(else_block)
  end
end

#matching_predicate(execution_context) ⇒ Object



46
47
48
# File 'lib/blockly_interpreter/core_blocks/if_block.rb', line 46

def matching_predicate(execution_context)
  predicates.detect { |predicate| predicate.matches? execution_context }
end

#predicate_action_dsl(num) ⇒ Object



50
51
52
53
54
55
# File 'lib/blockly_interpreter/core_blocks/if_block.rb', line 50

def predicate_action_dsl(num)
  [
    method_call_with_possible_block("predicate", "", values["IF#{num}"]),
    method_call_with_possible_block("action", "", statements["DO#{num}"])
  ]
end

#predicatesObject



22
23
24
25
26
# File 'lib/blockly_interpreter/core_blocks/if_block.rb', line 22

def predicates
  @predicates ||= begin
    (0..elseif_count).map { |num| Conditional.new(self, num) }
  end
end

#to_dslObject



57
58
59
60
61
62
63
64
# File 'lib/blockly_interpreter/core_blocks/if_block.rb', line 57

def to_dsl
  block_contents = predicate_action_dsl(0) + (1..elseif_count).flat_map do |num|
    method_call_with_possible_block("elsif_clause", "", predicate_action_dsl(num))
  end
  block_contents << method_call_with_block_or_nothing("else_action", "", else_block)

  method_call_with_block_or_nothing("controls_if", "", block_contents.compact)
end