-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from procore/support_json_array
Support json array
- Loading branch information
Showing
10 changed files
with
213 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
module Brita | ||
class ValueParser | ||
attr_accessor :value, :supports_boolean, :supports_json, :supports_ranges | ||
def initialize(value:, options: {}) | ||
@value = value | ||
@supports_boolean = options.fetch(:supports_boolean, false) | ||
@supports_ranges = options.fetch(:supports_ranges, false) | ||
@supports_json = options.fetch(:supports_json, false) | ||
end | ||
|
||
def parse | ||
@_result ||= | ||
if parse_as_range? | ||
range_value | ||
elsif parse_as_boolean? | ||
boolean_value | ||
elsif parse_as_json? | ||
array_from_json | ||
else | ||
value | ||
end | ||
end | ||
|
||
def array_from_json | ||
result = JSON.parse(value) | ||
if result.is_a?(Array) | ||
result | ||
else | ||
value | ||
end | ||
rescue JSON::ParserError | ||
value | ||
end | ||
|
||
private | ||
|
||
def parse_as_range? | ||
supports_ranges && value.to_s.include?("...") | ||
end | ||
|
||
def range_value | ||
Range.new(*value.split("...")) | ||
end | ||
|
||
def parse_as_json? | ||
supports_json && value.is_a?(String) | ||
end | ||
|
||
def parse_as_boolean? | ||
supports_boolean | ||
end | ||
|
||
def boolean_value | ||
if Rails.version.starts_with?("5") | ||
ActiveRecord::Type::Boolean.new.cast(value) | ||
else | ||
ActiveRecord::Type::Boolean.new.type_cast_from_user(value) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
require "test_helper" | ||
|
||
class FilterTest < ActiveSupport::TestCase | ||
test "returns the original value without options" do | ||
parser = Brita::ValueParser.new(value: "hi") | ||
|
||
assert_equal "hi", parser.parse | ||
end | ||
|
||
test "With options an array of integers results in an array of integers" do | ||
options = { | ||
supports_ranges: true, | ||
supports_json: true | ||
} | ||
parser = Brita::ValueParser.new(value: [1,2,3]) | ||
|
||
assert_equal [1,2,3], parser.parse | ||
end | ||
|
||
test "With options a json string array of integers results in an array of integers" do | ||
options = { | ||
supports_ranges: true, | ||
supports_json: true | ||
} | ||
parser = Brita::ValueParser.new(value: "[1,2,3]", options: options) | ||
|
||
assert_equal [1,2,3], parser.parse | ||
end | ||
|
||
test "with invalid json returns original value" do | ||
options = { | ||
supports_ranges: true, | ||
supports_json: true | ||
} | ||
parser = Brita::ValueParser.new(value: "[1,2,3", options: options) | ||
|
||
assert_equal "[1,2,3", parser.parse | ||
end | ||
|
||
test "JSON parsing only supports arrays" do | ||
options = { | ||
supports_json: true | ||
} | ||
json_string = "{\"a\":4}" | ||
parser = Brita::ValueParser.new(value: json_string, options: options) | ||
|
||
assert_equal json_string, parser.parse | ||
end | ||
|
||
test "With options a range string of integers results in a range" do | ||
options = { | ||
supports_ranges: true, | ||
supports_json: true | ||
} | ||
parser = Brita::ValueParser.new(value: "1...3", options: options) | ||
|
||
assert_instance_of Range, parser.parse | ||
end | ||
|
||
test "parses true from 1" do | ||
options = { | ||
supports_boolean: true | ||
} | ||
parser = Brita::ValueParser.new(value: 1, options: options) | ||
|
||
assert_equal true, parser.parse | ||
end | ||
|
||
test "parses false from 0" do | ||
options = { | ||
supports_boolean: true | ||
} | ||
parser = Brita::ValueParser.new(value: 0, options: options) | ||
|
||
assert_equal false, parser.parse | ||
end | ||
end |