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

Minor changes to allow for subclassing #2

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Minor changes to allow for subclassing #2

wants to merge 9 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Oct 15, 2018

This there are a few minor changes to lib/regexify.rb to allow for easier use of the Regexify class as a base class. There is a new file called examples/example.rb to demonstrate. examples/example.rb contains three different subclasses of Regexify of varying complexity. The monkey-patch of Regexify is just for the convenience of the test cases at the bottom.

I hope you will consider merging this into the master branch of your repository.

Thanks for letting me contribute.

-- Steve Witten

Steve Witten added 8 commits October 14, 2018 15:27
Several examples of Regexify subclasses are included
Added accessor methods with validated input for 'options' in RegexifyWithOptions
Added accessor methods for 'options' in examples/example.rb
Removed some redundant code
@ghost ghost closed this Oct 15, 2018
@jusleg
Copy link
Owner

jusleg commented Oct 15, 2018

Hey Steve, is there a reason why you closed this PR? It looks really promising 😄

@ghost
Copy link
Author

ghost commented Oct 15, 2018 via email

@ghost ghost reopened this Oct 15, 2018
@jusleg
Copy link
Owner

jusleg commented Oct 15, 2018

@stevewi All good. I like what I see. Perhaps we could introduce your variations as options in the main gem. What do you think?

@ghost
Copy link
Author

ghost commented Oct 15, 2018

There are very few changes in your code itself:

Logic changes

  • Saving the constant PATTERNS into an instance variable and using the instance variable instead of the constant in the extract_regex method.
  • You might consider splitting PATTERNS into a module of it's own with methods to manage it's contents (if you did this, you wouldn't need to store PATTERNS in an instance variable so subclasses could manage it...I considered doing this but opted for the quick/dirty method I used for this).

Visibility changes

  • Making the instance variables protected instead of private so subclasses can access them without methods.
  • Making the Error class public so users and subclasses of Regexify can throw it.
  • All other methods retain their original visibility.

The real action is in the example (examples/example.rb)...a demonstration of the power that these changes can provide (the monkey patch addition of to_s was purely for convenience since it will output something different than Regexp.to_s):

  • RegexifyWithOptions allows you to initialize/change the options of the Regexp (see http://www.rubyguides.com/2015/06/ruby-regex/ ... down at the bottom) to change how the Regexp works.
  • RegexifyWithAnchors adds methods to insert anchors (see http://www.rexegg.com/regex-anchors.html) to the generated Regexp.
  • RegexifyANSI causes the generated Regexp to use ANSI notation for various common patterns: [[:upper:]] for [A-Z], [[:digit:]] for [0-9] etc. It also demonstrates how a subclass can add new named patterns; e.g., :hexdigit for [a-fA-F0-9] or [[:xdigit:]].

I have an application that scans in-/out-bound mail for certain sets of regexps and sets a tag header in the message depending on what it finds. I'm certainly no expert at writing regexps and most of the ones I have are fairly simple from a guru's point of view (they do, however, use the \b anchor and the imx options a lot 😉).

I'm looking for a way to build/edit regexps more easily (and in symbols that my pea-brain understands) -- perhaps using Regexify's simple grammar for a rudimentary file representation using YAML or JSON such that reading/parsing a file would result in an array of Regexify objects.

Anyway, that's my motivation...

-- sw

P.S. I would also like any file format to include a minimal amount of reuse of expressions using Boolean logic (similar to SpamAssassin rules: https://wiki.apache.org/spamassassin/WritingRules) and some rudimentary amount of forward chaining on substrings using Regexp capture groups (see https://stackoverflow.com/questions/9303984/ruby-regexp-group-matching-assign-variables-on-1-line#9304049).

@ghost
Copy link
Author

ghost commented Oct 15, 2018 via email

@ghost
Copy link
Author

ghost commented Oct 15, 2018 via email

Copy link
Author

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Removed a case...when construct & replaced with 3 equivalent <statement> if <condition> constructs...less code and possibly faster...and possible solves one of the codebeat issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant