Class: BlocklyInterpreter::DSL::BlockContext

Inherits:
Object
  • Object
show all
Defined in:
lib/blockly_interpreter/dsl.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBlockContext

Returns a new instance of BlockContext.



13
14
15
16
# File 'lib/blockly_interpreter/dsl.rb', line 13

def initialize
  @blocks = []
  @procedure_blocks = []
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



5
6
7
# File 'lib/blockly_interpreter/dsl.rb', line 5

def blocks
  @blocks
end

#procedure_blocksObject (readonly)

Returns the value of attribute procedure_blocks.



5
6
7
# File 'lib/blockly_interpreter/dsl.rb', line 5

def procedure_blocks
  @procedure_blocks
end

Class Method Details

.register_block_class(block_class) ⇒ Object



7
8
9
10
11
# File 'lib/blockly_interpreter/dsl.rb', line 7

def self.register_block_class(block_class)
  if block_class.const_defined?(:DSLMethods)
    self.include block_class.const_get(:DSLMethods)
  end
end

Instance Method Details

#block(block_type, &proc) ⇒ Object



18
19
20
21
22
# File 'lib/blockly_interpreter/dsl.rb', line 18

def block(block_type, &proc)
  @blocks << BlockBuilder.new(block_type).tap do |builder|
    builder.instance_exec(&proc) if proc
  end
end

#procedures(&proc) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/blockly_interpreter/dsl.rb', line 39

def procedures(&proc)
  procedures_context = BlockContext.new.tap do |builder|
    builder.instance_exec(&proc) if proc
  end

  @procedure_blocks.push(*procedures_context.blocks)
end

#shadow(&proc) ⇒ Object



29
30
31
32
# File 'lib/blockly_interpreter/dsl.rb', line 29

def shadow(&proc)
  instance_exec(&proc)
  @blocks.last.tap { |block| block.shadow! }
end

#to_xml(doc, block_index = 0) ⇒ Object



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

def to_xml(doc, block_index = 0)
  return unless blocks.any?

  block = blocks[block_index]
  Nokogiri::XML::Node.new(block.tag_name, doc).tap do |node|
    blocks[block_index].to_xml(node)

    if blocks.size > block_index + 1
      next_node = Nokogiri::XML::Node.new('next', doc)
      next_node.add_child to_xml(doc, block_index + 1)
      node.add_child(next_node)
    end
  end
end

#with_comment(comment, pinned = false, &proc) ⇒ Object



24
25
26
27
# File 'lib/blockly_interpreter/dsl.rb', line 24

def with_comment(comment, pinned = false, &proc)
  instance_exec(&proc)
  @blocks.last.tap { |block| block.set_comment(comment, pinned) }
end

#with_position(x, y, &proc) ⇒ Object



34
35
36
37
# File 'lib/blockly_interpreter/dsl.rb', line 34

def with_position(x, y, &proc)
  instance_exec(&proc)
  @blocks.last.tap { |block| block.set_position!(x, y) }
end