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.



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

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

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name.



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

def file_name
  @file_name
end

#functionObject (readonly)

Returns the value of attribute function.



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

def function
  @function
end

#line_numberObject (readonly)

Returns the value of attribute line_number.



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

def line_number
  @line_number
end

Instance Method Details

#cleanup_function!Object



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

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

#scrape_function!Object



18
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
# File 'lib/jsonable/function.rb', line 18

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



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

def to_json(*)
  scrape_function!
  cleanup_function!
end