Class: BlocklyInterpreter::Block

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block_type, fields, values, statements, next_block, mutation, comment, comment_pinned, is_shadow, x, y) ⇒ Block

Returns a new instance of Block.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/blockly_interpreter/block.rb', line 11

def initialize(block_type, fields, values, statements, next_block, mutation, comment, comment_pinned, is_shadow, x, y)
  @block_type = block_type
  @fields = fields
  @values = values
  @statements = statements
  @next_block = next_block
  @mutation = mutation
  @comment = comment
  @comment_pinned = comment_pinned
  @is_shadow = is_shadow
  @x = x
  @y = y
end

Class Attribute Details

.block_typeObject

Returns the value of attribute block_type.



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

def block_type
  @block_type
end

Instance Attribute Details

#block_typeObject (readonly)

Returns the value of attribute block_type.



8
9
10
# File 'lib/blockly_interpreter/block.rb', line 8

def block_type
  @block_type
end

#commentObject (readonly)

Returns the value of attribute comment.



8
9
10
# File 'lib/blockly_interpreter/block.rb', line 8

def comment
  @comment
end

#comment_pinnedObject (readonly)

Returns the value of attribute comment_pinned.



8
9
10
# File 'lib/blockly_interpreter/block.rb', line 8

def comment_pinned
  @comment_pinned
end

#fieldsObject (readonly)

Returns the value of attribute fields.



8
9
10
# File 'lib/blockly_interpreter/block.rb', line 8

def fields
  @fields
end

#is_shadowObject (readonly) Also known as: is_shadow?

Returns the value of attribute is_shadow.



8
9
10
# File 'lib/blockly_interpreter/block.rb', line 8

def is_shadow
  @is_shadow
end

#mutationObject (readonly)

Returns the value of attribute mutation.



8
9
10
# File 'lib/blockly_interpreter/block.rb', line 8

def mutation
  @mutation
end

#next_blockObject (readonly)

Returns the value of attribute next_block.



8
9
10
# File 'lib/blockly_interpreter/block.rb', line 8

def next_block
  @next_block
end

#statementsObject (readonly)

Returns the value of attribute statements.



8
9
10
# File 'lib/blockly_interpreter/block.rb', line 8

def statements
  @statements
end

#valuesObject (readonly)

Returns the value of attribute values.



8
9
10
# File 'lib/blockly_interpreter/block.rb', line 8

def values
  @values
end

#xObject (readonly)

Returns the value of attribute x.



8
9
10
# File 'lib/blockly_interpreter/block.rb', line 8

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



8
9
10
# File 'lib/blockly_interpreter/block.rb', line 8

def y
  @y
end

Instance Method Details

#each_block(iterate_subblocks = true) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/blockly_interpreter/block.rb', line 32

def each_block(iterate_subblocks = true)
  return to_enum(:each_block, iterate_subblocks) unless block_given?

  yield self

  if iterate_subblocks
    statements.each do |key, statement|
      statement.each_block(iterate_subblocks) do |block|
        yield block
      end
    end
  end

  if next_block
    next_block.each_block(iterate_subblocks) do |block|
      yield block
    end
  end
end

#execute_statement(execution_context) ⇒ Object



25
26
# File 'lib/blockly_interpreter/block.rb', line 25

def execute_statement(execution_context)
end

#has_comment?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/blockly_interpreter/block.rb', line 56

def has_comment?
  comment.present?
end

#has_position?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/blockly_interpreter/block.rb', line 60

def has_position?
  x || y
end

#to_dslObject



52
53
54
# File 'lib/blockly_interpreter/block.rb', line 52

def to_dsl
  BlocklyInterpreter::GenericBlockDSLGenerator.new(self).dsl
end

#to_xml(options = {}) ⇒ Object



108
109
110
111
112
# File 'lib/blockly_interpreter/block.rb', line 108

def to_xml(options = {})
  doc = Nokogiri::XML::Document.new
  element = to_xml_element(doc)
  element.to_xml(options = {})
end

#to_xml_element(document) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/blockly_interpreter/block.rb', line 64

def to_xml_element(document)
  tag_name = is_shadow ? 'shadow' : 'block'

  Nokogiri::XML::Element.new(tag_name, document).tap do |node|
    node['type'] = block_type
    node['x'] = x if x
    node['y'] = y if y
    node.add_child(mutation.dup) if mutation

    if comment
      comment_node = Nokogiri::XML::Element.new('comment', document)
      comment_node['pinned'] = 'true' if comment_pinned
      node.add_child comment_node
    end

    fields.each do |name, value|
      field_node = Nokogiri::XML::Element.new('field', document)
      field_node['name'] = name
      field_node.add_child Nokogiri::XML::Text.new(value, document)
      node.add_child field_node
    end

    values.each do |name, value|
      value_node = Nokogiri::XML::Element.new('value', document)
      value_node['name'] = name
      value_node.add_child value.to_xml_element(document)
      node.add_child value_node
    end

    statements.each do |name, value|
      statement_node = Nokogiri::XML::Element.new('statement', document)
      statement_node['name'] = name
      statement_node.add_child value.to_xml_element(document)
      node.add_child statement_node
    end

    if next_block
      next_block_node = Nokogiri::XML::Element.new('next_block', document)
      next_block_node.add_child next_block.to_xml_element(document)
      node.add_child next_block_node
    end
  end
end

#value(execution_context) ⇒ Object



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

def value(execution_context)
  nil
end