Class: JSONable::Function

Inherits:
Numeric
  • Object
show all
Defined in:
lib/jsonable/function.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Function

Returns a new instance of Function.



10
11
12
# File 'lib/jsonable/function.rb', line 10

def initialize(&block)
  @file_name, @line_number = block.source_location
end

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name.



8
9
10
# File 'lib/jsonable/function.rb', line 8

def file_name
  @file_name
end

#functionObject (readonly)

Returns the value of attribute function.



8
9
10
# File 'lib/jsonable/function.rb', line 8

def function
  @function
end

#line_numberObject (readonly)

Returns the value of attribute line_number.



8
9
10
# File 'lib/jsonable/function.rb', line 8

def line_number
  @line_number
end

Instance Method Details

#cleanup_function!Object



46
47
48
# File 'lib/jsonable/function.rb', line 46

def cleanup_function!
  function.tr("\n", '').squeeze(' ')
end

#scrape_function!Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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_jsonObject



14
15
16
17
# File 'lib/jsonable/function.rb', line 14

def to_json(*)
  scrape_function!
  cleanup_function!
end