Class: JSONable::Function
- Inherits:
-
Numeric
- Object
- Numeric
- JSONable::Function
- Defined in:
- lib/jsonable/function.rb
Instance Attribute Summary collapse
-
#file_name ⇒ Object
readonly
Returns the value of attribute file_name.
-
#function ⇒ Object
readonly
Returns the value of attribute function.
-
#line_number ⇒ Object
readonly
Returns the value of attribute line_number.
Instance Method Summary collapse
- #cleanup_function! ⇒ Object
-
#initialize(&block) ⇒ Function
constructor
A new instance of Function.
- #scrape_function! ⇒ Object
- #to_json ⇒ Object
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_name ⇒ Object (readonly)
Returns the value of attribute file_name.
7 8 9 |
# File 'lib/jsonable/function.rb', line 7 def file_name @file_name end |
#function ⇒ Object (readonly)
Returns the value of attribute function.
7 8 9 |
# File 'lib/jsonable/function.rb', line 7 def function @function end |
#line_number ⇒ Object (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_json ⇒ Object
13 14 15 16 |
# File 'lib/jsonable/function.rb', line 13 def to_json(*) scrape_function! cleanup_function! end |