Class: BlocklyInterpreter::DSL::BinaryOperationDSLGenerator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BlocklyInterpreter::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

Constructor Details

#initialize(block, dsl_method_name) ⇒ BinaryOperationDSLGenerator

Returns a new instance of BinaryOperationDSLGenerator.



223
224
225
226
# File 'lib/blockly_interpreter/dsl.rb', line 223

def initialize(block, dsl_method_name)
  @block = block
  @dsl_method_name = dsl_method_name
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



221
222
223
# File 'lib/blockly_interpreter/dsl.rb', line 221

def block
  @block
end

#dsl_method_nameObject (readonly)

Returns the value of attribute dsl_method_name.



221
222
223
# File 'lib/blockly_interpreter/dsl.rb', line 221

def dsl_method_name
  @dsl_method_name
end

Instance Method Details

#block_contentsObject



248
249
250
251
252
253
254
# File 'lib/blockly_interpreter/dsl.rb', line 248

def block_contents
  cast_a, cast_b = cast_operands
  [
    value_block('A', cast_a),
    value_block('B', cast_b)
  ].compact
end

#cast_operandsObject



236
237
238
239
240
241
# File 'lib/blockly_interpreter/dsl.rb', line 236

def cast_operands
  @cast_operands ||= begin
    a, b = ['A', 'B'].map { |value_name| block.values[value_name] }
    [a, b].map { |value| castable_static_value value }
  end
end

#castable_static_value(value) ⇒ Object



228
229
230
231
232
233
234
# File 'lib/blockly_interpreter/dsl.rb', line 228

def castable_static_value(value)
  case value
  when BlocklyInterpreter::CoreBlocks::NumberBlock then value.fields['NUM'].to_i
  when BlocklyInterpreter::CoreBlocks::BooleanBlock then value.to_bool
  when BlocklyInterpreter::CoreBlocks::TextBlock then value.fields['TEXT']
  end
end

#dslObject



266
267
268
# File 'lib/blockly_interpreter/dsl.rb', line 266

def dsl
  method_call_with_possible_block(dsl_method_name, method_args.join(', '), block_contents)
end

#method_argsObject



256
257
258
259
260
261
262
263
264
# File 'lib/blockly_interpreter/dsl.rb', line 256

def method_args
  args = [block.fields['OP'].to_sym.inspect]
  cast_a, cast_b = cast_operands

  args << cast_a.inspect if cast_a || cast_b
  args << cast_b.inspect if cast_b

  args
end

#value_block(value_name, cast_operand) ⇒ Object



243
244
245
246
# File 'lib/blockly_interpreter/dsl.rb', line 243

def value_block(value_name, cast_operand)
  return if cast_operand
  method_call_with_block_or_nothing(value_name.downcase, '', block.values[value_name])
end