class CompileToGo

Attributes

gettable[RW]
settable[RW]
sheet_names[RW]

Public Class Methods

rewrite(*args) click to toggle source
# File src/compile/go/compile_to_go.rb, line 12
def self.rewrite(*args)
  self.new.rewrite(*args)
end

Public Instance Methods

getter_method_name(ref) click to toggle source
# File src/compile/go/compile_to_go.rb, line 62
def getter_method_name(ref)
  v = variable_name(ref)
  if gettable.call(ref)
    v[0] = v[0].upcase!
  else
    v[0] = v[0].downcase!
  end
  v
end
rewrite(formulae, sheet_names, output) click to toggle source
# File src/compile/go/compile_to_go.rb, line 16
  def rewrite(formulae, sheet_names, output)
    self.settable ||= lambda { |ref| false }
    self.gettable ||= lambda { |ref| true }
    self.sheet_names = sheet_names

    m = MapValuesToGo.new

    # The struct
    output.puts "type #{struct_type} struct {"
    formulae.each do |ref, _|
      output.puts "  #{variable_name(ref)} cachedValue"
    end
    output.puts "}"

    # The initializer
    output.puts <<~END

    func New() #{struct_type} {
      return #{struct_type}{}
    }

    END

    formulae.each do |ref, ast|
      v = variable_name(ref)
      output.puts <<~END
        func (s *#{struct_type}) #{getter_method_name(ref)}() (interface{}, error) {
          if !s.#{v}.isCached() {
            s.#{v}.set(#{m.map(ast)})
          }
          return s.#{v}.get()
        }

      END
      if settable.call(ref)
        output.puts <<~END

        func (s *#{struct_type}) #{setter_method_name(ref)}(v interface{}) {
            s.#{v}.set(v)
        }

        END
      end
    end
  end
setter_method_name(ref) click to toggle source
# File src/compile/go/compile_to_go.rb, line 72
def setter_method_name(ref)
  v = variable_name(ref)
  v[0].upcase!
  "Set#{v}"
end
struct_type() click to toggle source
# File src/compile/go/compile_to_go.rb, line 8
def struct_type
  "spreadsheet"
end
variable_name(ref) click to toggle source
# File src/compile/go/compile_to_go.rb, line 78
def variable_name(ref)
  worksheet = ref.first
  cell = ref.last
  worksheet_name = sheet_names[worksheet.to_s] || worksheet.to_s
  return worksheet_name.length > 0 ? "#{worksheet_name.downcase}#{cell.upcase}" : cell.downcase
end