Class: BlocklyInterpreter::DSL::BlockBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block_type) ⇒ BlockBuilder

Returns a new instance of BlockBuilder.



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/blockly_interpreter/dsl.rb', line 66

def initialize(block_type)
  @block_type = block_type
  @values = {}
  @statements = {}
  @fields = {}
  @mutation_attrs = {}
  @mutation_child_procs = []
  @comment = nil
  @comment_pinned = nil
  @is_shadow = false
  @x = nil
  @y = nil
end

Instance Attribute Details

#block_typeObject (readonly)

Returns the value of attribute block_type.



64
65
66
# File 'lib/blockly_interpreter/dsl.rb', line 64

def block_type
  @block_type
end

#commentObject (readonly)

Returns the value of attribute comment.



64
65
66
# File 'lib/blockly_interpreter/dsl.rb', line 64

def comment
  @comment
end

#comment_pinnedObject (readonly)

Returns the value of attribute comment_pinned.



64
65
66
# File 'lib/blockly_interpreter/dsl.rb', line 64

def comment_pinned
  @comment_pinned
end

#fieldsObject (readonly)

Returns the value of attribute fields.



64
65
66
# File 'lib/blockly_interpreter/dsl.rb', line 64

def fields
  @fields
end

#is_shadowObject (readonly)

Returns the value of attribute is_shadow.



64
65
66
# File 'lib/blockly_interpreter/dsl.rb', line 64

def is_shadow
  @is_shadow
end

#mutation_attrsObject (readonly)

Returns the value of attribute mutation_attrs.



64
65
66
# File 'lib/blockly_interpreter/dsl.rb', line 64

def mutation_attrs
  @mutation_attrs
end

#mutation_child_procsObject (readonly)

Returns the value of attribute mutation_child_procs.



64
65
66
# File 'lib/blockly_interpreter/dsl.rb', line 64

def mutation_child_procs
  @mutation_child_procs
end

#statementsObject (readonly)

Returns the value of attribute statements.



64
65
66
# File 'lib/blockly_interpreter/dsl.rb', line 64

def statements
  @statements
end

#valuesObject (readonly)

Returns the value of attribute values.



64
65
66
# File 'lib/blockly_interpreter/dsl.rb', line 64

def values
  @values
end

#xObject (readonly)

Returns the value of attribute x.



64
65
66
# File 'lib/blockly_interpreter/dsl.rb', line 64

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



64
65
66
# File 'lib/blockly_interpreter/dsl.rb', line 64

def y
  @y
end

Instance Method Details

#build_subblock(&proc) ⇒ Object



138
139
140
141
142
# File 'lib/blockly_interpreter/dsl.rb', line 138

def build_subblock(&proc)
  BlockContext.new.tap do |builder|
    builder.instance_exec(&proc)
  end
end

#comment_to_xml(document) ⇒ Object



131
132
133
134
135
136
# File 'lib/blockly_interpreter/dsl.rb', line 131

def comment_to_xml(document)
  Nokogiri::XML::Node.new('comment', document).tap do |comment_node|
    comment_node.content = comment
    comment_node['pinned'] = comment_pinned.inspect unless comment_pinned.nil?
  end
end

#field(name, value) ⇒ Object



152
153
154
# File 'lib/blockly_interpreter/dsl.rb', line 152

def field(name, value)
  @fields[name.to_s] = value
end

#field_to_xml(name, value, document) ⇒ Object



124
125
126
127
128
129
# File 'lib/blockly_interpreter/dsl.rb', line 124

def field_to_xml(name, value, document)
  Nokogiri::XML::Node.new('field', document).tap do |field_node|
    field_node['name'] = name
    field_node.add_child Nokogiri::XML::Text.new(value.to_s, document)
  end
end

#mutation_attr(name, value) ⇒ Object



156
157
158
# File 'lib/blockly_interpreter/dsl.rb', line 156

def mutation_attr(name, value)
  @mutation_attrs[name.to_s] = value
end

#mutation_child(tag_name, &proc) ⇒ Object



160
161
162
# File 'lib/blockly_interpreter/dsl.rb', line 160

def mutation_child(tag_name, &proc)
  @mutation_child_procs << [tag_name, proc]
end

#mutations_to_xml(document) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/blockly_interpreter/dsl.rb', line 110

def mutations_to_xml(document)
  Nokogiri::XML::Node.new('mutation', document).tap do |mutation_node|
    mutation_attrs.each do |name, value|
      mutation_node[name] = value.to_s
    end

    mutation_child_procs.each do |(tag_name, proc)|
      child = Nokogiri::XML::Node.new(tag_name.to_s, document)
      proc.call(child)
      mutation_node.add_child child
    end
  end
end

#set_comment(comment, pinned = false) ⇒ Object



164
165
166
167
# File 'lib/blockly_interpreter/dsl.rb', line 164

def set_comment(comment, pinned = false)
  @comment = comment
  @comment_pinned = pinned
end

#set_position!(x, y) ⇒ Object



169
170
171
172
# File 'lib/blockly_interpreter/dsl.rb', line 169

def set_position!(x, y)
  @x = x
  @y = y
end

#shadow!Object



182
183
184
# File 'lib/blockly_interpreter/dsl.rb', line 182

def shadow!
  @is_shadow = true
end

#statement(name, &proc) ⇒ Object



148
149
150
# File 'lib/blockly_interpreter/dsl.rb', line 148

def statement(name, &proc)
  @statements[name.to_s] = build_subblock(&proc)
end

#subblock_to_xml(tag_name, subblock_name, context, doc) ⇒ Object



103
104
105
106
107
108
# File 'lib/blockly_interpreter/dsl.rb', line 103

def subblock_to_xml(tag_name, subblock_name, context, doc)
  Nokogiri::XML::Node.new(tag_name, doc).tap do |node|
    node['name'] = subblock_name
    node.add_child context.to_xml(doc)
  end
end

#tag_nameObject



174
175
176
177
178
179
180
# File 'lib/blockly_interpreter/dsl.rb', line 174

def tag_name
  if is_shadow
    'shadow'
  else
    'block'
  end
end

#to_xml(node) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/blockly_interpreter/dsl.rb', line 80

def to_xml(node)
  node['type'] = block_type
  document = node.document

  node['x'] = x if x
  node['y'] = y if y

  node.add_child(mutations_to_xml(document)) if mutation_attrs.any? || mutation_child_procs.any?
  node.add_child(comment_to_xml(document)) if comment.present?

  values.each do |name, context|
    node.add_child subblock_to_xml('value', name, context, document)
  end

  statements.each do |name, context|
    node.add_child subblock_to_xml('statement', name, context, document)
  end

  fields.each do |name, value|
    node.add_child field_to_xml(name, value, document)
  end
end

#value(name, &proc) ⇒ Object



144
145
146
# File 'lib/blockly_interpreter/dsl.rb', line 144

def value(name, &proc)
  @values[name.to_s] = build_subblock(&proc)
end