Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added light weight matches? function #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/grok-pure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ def discover(input)
return @discover.discover(input)
end # def discover

public
# Light weight method to check match without creating any objects
# returns nil if does not match
def matches?(text)
text =~ @regexp
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about calling it matches?

if grok.matches?(text) reads nicely to me, vs if grok.is_match?(text)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

end

private
def init_discover
require "grok/pure/discovery"
Expand Down
12 changes: 12 additions & 0 deletions test/pure-ruby/general/basic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,16 @@ def test_grok_expanded_unknown_pattern_embedded
@grok.compile("%{test} bar %{foo} baz")
end
end

def test_matches
path = "#{File.dirname(__FILE__)}/../../../patterns/pure-ruby/base"
@grok.add_patterns_from_file(path)
@grok.compile("%{NUMBER}", true)
assert(@grok.matches?("1234"))
assert([email protected]?("abc"))

@grok.compile("^%{NUMBER} %{TIME}", true)
assert(@grok.matches?("120913 12:04:33 abc foo"))
assert([email protected]?("foo 120913 12:04:33 abc foo"))
end
end