Class: Divine::RubyGenerator

Inherits:
RubyHelperMethods show all
Defined in:
lib/divine/code_generators/ruby.rb

Overview

Responsible for generating Divine and structs classes

Instance Method Summary collapse

Methods inherited from RubyHelperMethods

#get_header_comment, #ruby_base_class_template_str, #ruby_class_template, #ruby_deserialize_internal, #ruby_get_empty_declaration, #ruby_serialize_internal

Methods inherited from BabelHelperMethods

#camelize, #format_src, #get_fresh_variable_name, #get_header_comment_text, #sanity_check

Instance Method Details

#generate_code(structs, opts) ⇒ Object

  • opts -> Dictionary that contains generation params [file, module, parent_class, target_dir]



571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/divine/code_generators/ruby.rb', line 571

def generate_code(structs, opts)
  base_template = Erubis::Eruby.new(ruby_base_class_template_str)
  keys = structs.keys.sort
  src = keys.map do |k|
    ss = structs[k]
    # Check different aspects the the structs
    vss = sanity_check(ss)
    ruby_class_template(StructHandler.new(vss))
  end

  # User defined super class?
  toplevel = opts[:parent_class] || nil
  toplevel = " < #{toplevel}" if toplevel 
  return [{file: opts[:file], src: "#{get_header_comment}\n#{ruby_get_begin_module(opts)}#{base_template.result({ toplevel_class: toplevel, this: self })}\n\n#{src.join("\n\n")}#{ruby_get_end_module(opts)}"}]
end

#ruby_get_begin_module(opts) ⇒ Object

Build module name

  • Args :

    • opts -> Dictionary contains module name in key module



592
593
594
595
596
597
598
# File 'lib/divine/code_generators/ruby.rb', line 592

def ruby_get_begin_module(opts)
  if opts[:module]
    "module #{opts[:module]}\n\n"
  else 
    nil
  end
end

#ruby_get_end_module(opts) ⇒ Object

Ending the module



601
602
603
604
605
606
607
# File 'lib/divine/code_generators/ruby.rb', line 601

def ruby_get_end_module(opts)
  if opts[:module]
    "\n\nend\n"
  else
    nil
  end
end