class JSONable::Function

Attributes

file_name[R]
function[R]
line_number[R]

Public Class Methods

new(&block) click to toggle source
# File lib/jsonable/function.rb, line 10
def initialize(&block)
  @file_name, @line_number = block.source_location
end

Public Instance Methods

cleanup_function!() click to toggle source
# File lib/jsonable/function.rb, line 46
def cleanup_function!
  function.tr("\n", '').squeeze(' ')
end
scrape_function!() click to toggle source
# File lib/jsonable/function.rb, line 19
def scrape_function!
  @function = +'function'
  open_braces = 0

  catch(:function_ends) do
    File.foreach(file_name).with_index(1) do |line, index|
      next if index < line_number

      scanner = StringScanner.new(line)
      scanner.scan_until(/function/)
      while scanner.scan(/[^{}]+/)
        @function << scanner.matched
        while str = scanner.scan(/{|}/)
          @function << str
          case str
          when '{'
            open_braces += 1
          when '}'
            throw(:function_ends) if open_braces <= 1
            open_braces -= 1
          end
        end
      end
    end
  end
end
to_json(*) click to toggle source
# File lib/jsonable/function.rb, line 14
def to_json(*)
  scrape_function!
  cleanup_function!
end