Class: Libgeo::Formatter::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/libgeo/formatter/compiler.rb

Overview

Class: template compiler

Tooks the sprintf-like pattern string and compiles it to the ruby code

Example:

Compiler.new('%d:%2m').compile

Constant Summary collapse

KEYWORDS =

Constant: set of keywords

Defines keywords and corresponding ruby methods where c is a given coordinate object

{
  'd' => 'c.deg',
  'm' => 'c.mins',
  'M' => 'c.minutes_only',
  's' => 'c.secs.to_i',
  'S' => 'c.secs',
  'h' => 'c.hemi.to_s.downcase',
  'H' => 'c.hemi'
}.freeze
TOKEN_REG =
/%\d?\w/.freeze
STRING_REG =
/./.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#resultObject (readonly)

Resulted ruby expression



42
43
44
# File 'lib/libgeo/formatter/compiler.rb', line 42

def result
  @result
end

Instance Method Details

#compileObject

Compile the template

Returns: String ruby expression



49
50
51
52
53
54
55
56
57
# File 'lib/libgeo/formatter/compiler.rb', line 49

def compile
  return result if result

  scan until scanner.eos?

  finalize_string

  @result = parts.join(' << ').freeze
end