Class: BlocklyInterpreter::ExtensionBlocks::SwitchBlock

Inherits:
Block
  • Object
show all
Includes:
DSLGenerator
Defined in:
lib/blockly_interpreter/extension_blocks/switch_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



29
30
31
# File 'lib/blockly_interpreter/extension_blocks/switch_block.rb', line 29

def else_block
  statements['ELSE']
end

#execute_statement(execution_context) ⇒ Object



33
34
35
# File 'lib/blockly_interpreter/extension_blocks/switch_block.rb', line 33

def execute_statement(execution_context)
  (matching_predicate(execution_context) || else_block).try!(:execute_statement, execution_context)
end

#matching_predicate(execution_context) ⇒ Object



37
38
39
40
41
# File 'lib/blockly_interpreter/extension_blocks/switch_block.rb', line 37

def matching_predicate(execution_context)
  val = switch_value(execution_context)
  execution_context.set_variable(fields['CAPTURE_VAR'], val) if fields['CAPTURE_VAR'].present?
  predicates.detect { |predicate| predicate.matches? val, execution_context }
end

#predicatesObject



22
23
24
25
26
27
# File 'lib/blockly_interpreter/extension_blocks/switch_block.rb', line 22

def predicates
  @predicates ||= begin
    case_count = mutation.try(:[], 'case').try(:to_i) || 0
    (0...case_count).map { |num| Conditional.new(self, num) }
  end
end

#switch_value(execution_context) ⇒ Object



43
44
45
# File 'lib/blockly_interpreter/extension_blocks/switch_block.rb', line 43

def switch_value(execution_context)
  values['SWITCH_VAL'].value(execution_context)
end

#to_dslObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/blockly_interpreter/extension_blocks/switch_block.rb', line 47

def to_dsl
  preamble_dsl = [method_call_with_possible_block('switch_value', '', values['SWITCH_VAL'])]
  preamble_dsl << "capture_as #{fields['CAPTURE_VAR'].inspect}" if fields['CAPTURE_VAR'].present?

  clauses_dsl = predicates.map do |predicate|
    [
      method_call_with_possible_block('predicate', '', predicate.predicate_block),
      method_call_with_possible_block('action', '', predicate.action_block)
    ].join("\n")
  end

  clauses_dsl << method_call_with_block_or_nothing('else_action', '', statements['ELSE'])

  method_call_with_possible_block('controls_switch', '', [preamble_dsl.join("\n"), clauses_dsl.join("\n")].join("\n"))
end