-
Notifications
You must be signed in to change notification settings - Fork 35
Description
Proposed API:
jexp = match({
id: match(Fixnum).capture_as(:user_id),
username: match(String).and(->(username){ /a-z0-9/i =~ username }),
first_name: String,
last_name: String,
full_name: ->(full_name, json){ full_name == "#{json['first_name']} #{json['last_name']}" },
password: match(nothing),
homepage: /\Ahttps?\:\/\/.*\z/i,
created_at: match.not(nil),
updated_at: match.not(nil),
posts: match({
id: Fixnum,
author_id: :user_id,
title: String,
tags: match(String).repeated(0..Infinity).or(nil)
}).fuzzy.repeated(0..Infinity)
})
some_json.should match_json_expression(jexp)
jexp[:user_id] # => 1
match
(perhaps need a better name, not sure if this conflicts with other gems) is a factory method that always returns aJsonExpressions::Matcher
. Passing aJsonExpressions::Matcher
tomatch
is a no-op, otherwise it would wrap that with the appropriate subclass ofJsonExpression::Matcher
.JsonExpressions::Matcher
defines#and
,#or
which does what you would expectJsonExpressions::Matcher
also defines#repeated
which takes an integer or a integer range which turns its receiver into aJsonExpression::RepetitionMatcher
and does what you would expectanything
returns an instance ofJsonExpressions::WildcardMatcher
which matches anything (including nil), which is basicallynothing
returns an instance of `JsonExpression::NothingMatcher, which asserts that key doesn't exists- Calling
match
,JsonExpressions::Matcher#and
andJsonExpressions::Matcher#or
with no arguments returns aJsonExpressions::NotProxy
which defines a#not
method, and that wraps its argument in aJsonExpressions::NotMatcher
which negates its results, and then passes this to the original receiver + method JsonExpression::ArrayMatcher
,JsonExpression::ObjectMatcher
defines#fuzzy
,#exact
,#ordered
and#unordered
, so no more monkey-patchingArray
andHash
- This would probably be a backwards-incompatible rewrite, We are pre-1.0 after all, so I get to do whatever I want :)
Would like some feedback on this, especially on the naming (fuzzy/exact vs forgiving/strict, etc) and any use cases that I missed.
*Also, please let me know if you expect the match
method to conflict with a library you are using today. I'm also open to other suggestions for the top level method that we expose/inject (pattern
, expression
, expr
?)
No ETA. This is probably gonna take some time and tbh it won't be on the top of my priority list for the next while, but I'll do what I can. But now that the spec is here, if you really want it now you can always implement this spec yourself :) (If you do, please pick a different name to avoid confusion)
Thank you for sticking around after I ignored you pull request for so long! (To be clear, I did not abandon the gem – I've been actively using it on Rails 4 / Ruby 2.0 myself, and I'll fix any bugs that prevents it from working correctly, I just haven't been needing these schema oriented functionalities.)
/cc @pda @tdumitrescu @martinstreicher @iangreenleaf @matthew-bb @sricc