Skip to content

Commit 1e25da5

Browse files
committed
Handle invalid function
1 parent 3075a5f commit 1e25da5

File tree

5 files changed

+17
-55
lines changed

5 files changed

+17
-55
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99

1010
# rspec failure tracking
1111
.rspec_status
12+
Gemfile.lock

Gemfile.lock

Lines changed: 0 additions & 49 deletions
This file was deleted.

bin/console

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ require 'functionable/json'
88
# with your gem easier. You can also use a different console, if you like.
99

1010
# (If you use this, don't forget to add pry to your Gemfile!)
11-
# require "pry"
12-
# Pry.start
11+
require "pry"
12+
Pry.start
1313

14-
require 'irb'
15-
IRB.start(__FILE__)
14+
#require 'irb'
15+
#IRB.start(__FILE__)

lib/jsonable/function.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ def to_json(*)
1717
end
1818

1919
def scrape_function!
20+
@file_name ? valid_function : invalid_function
21+
end
22+
23+
def cleanup_function!
24+
function.tr("\n", '').squeeze(' ')
25+
end
26+
27+
def valid_function
2028
@function = +'function'
2129
open_braces = 0
2230

@@ -43,8 +51,8 @@ def scrape_function!
4351
end
4452
end
4553

46-
def cleanup_function!
47-
function.tr("\n", '').squeeze(' ')
54+
def invalid_function
55+
@function = 'undefined'
4856
end
4957
end
5058
end

spec/jsonable/function_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
return 0
2222
}
2323
}
24+
INVALID_FUNCTION = function(val, &method(:to_s))
2425

2526
it 'is a kind of Numeric' do
2627
expect(FUNCTION1).to be_a Numeric
@@ -40,6 +41,7 @@
4041
"}"
4142
)
4243
expect({a: FUNCTION2}.to_json).to eq "{\"a\":function(arg) { return arg + 'km' }}"
44+
expect({a: INVALID_FUNCTION}.to_json).to eq "{\"a\":undefined}"
4345
end
4446
end
4547
end

0 commit comments

Comments
 (0)