Skip to content

Commit

Permalink
Merge pull request #50 from joshbuddy/conditional_operators
Browse files Browse the repository at this point in the history
Closes #49
  • Loading branch information
Skarlso committed May 7, 2017
2 parents 2d5ada5 + 839d943 commit 6bec446
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,28 @@ You can optionally prevent eval from being called on sub-expressions by passing

For more usage examples and variations on paths, please visit the tests. There are some more complex ones as well.

### Conditional Operators Are Also Supported

```ruby
def test_or_operator
assert_equal [@object['store']['book'][1], @object['store']['book'][3]], JsonPath.new("$..book[?(@['price'] == 13 || @['price'] == 23)]").on(@object)
end

def test_and_operator
assert_equal [], JsonPath.new("$..book[?(@['price'] == 13 && @['price'] == 23)]").on(@object)
end

def test_and_operator_with_more_results
assert_equal [@object['store']['book'][1]], JsonPath.new("$..book[?(@['price'] < 23 && @['price'] > 9)]").on(@object)
end
```

### Running an individual test

```ruby
ruby -Ilib:../lib test/test_jsonpath.rb --name test_wildcard_on_intermediary_element_v6
```

### Manipulation

If you'd like to do substitution in a json object, you can use `#gsub` or `#gsub!` to modify the object in place.
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonpath/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class JsonPath
VERSION = '0.7.0'.freeze
VERSION = '0.7.1'.freeze
end
14 changes: 10 additions & 4 deletions test/test_jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,16 @@ def test_recognize_filters
assert_equal [@object['store']['book'][3]], JsonPath.new("$..book[?(@['price'] > 20)]").on(@object)
end

if RUBY_VERSION[/^1\.9/]
def test_recognize_filters_on_val
assert_equal [@object['store']['book'][1]['price'], @object['store']['book'][3]['price'], @object['store']['bicycle']['price']], JsonPath.new('$..price[?(@ > 10)]').on(@object)
end
def test_or_operator
assert_equal [@object['store']['book'][1], @object['store']['book'][3]], JsonPath.new("$..book[?(@['price'] == 13 || @['price'] == 23)]").on(@object)
end

def test_and_operator
assert_equal [], JsonPath.new("$..book[?(@['price'] == 13 && @['price'] == 23)]").on(@object)
end

def test_and_operator_with_more_results
assert_equal [@object['store']['book'][1]], JsonPath.new("$..book[?(@['price'] < 23 && @['price'] > 9)]").on(@object)
end

def test_no_eval
Expand Down

0 comments on commit 6bec446

Please sign in to comment.