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

Support filtering Items Arrays by Regex #458

Merged
merged 1 commit into from Aug 10, 2014

Conversation

Projects
None yet
5 participants
@jugglinmike
Copy link
Contributor

jugglinmike commented Jul 9, 2014

This is a convenience API for working with the @items array in nanoc's
preprocess block. In that context, it is common to manipulate items
according to a shared part of their identifier.

For example, with this change in place, users could succinctly create a
collection of all authors for a given site's weblog:

@items[%r{^/weblog/authors/}].each { |author|
  # etc...
}
@@ -34,6 +34,8 @@ def freeze
def [](*args)
if 1 == args.size && args.first.is_a?(String)
item_with_identifier(args.first)
elsif 1 == args.size && args.first.is_a?(Regexp)
@items.select { |i| i.identifier[args.first] }

This comment has been minimized.

@bobthecow

bobthecow Jul 9, 2014

Member

ewww. that's possibly the ugliest way of checking for a regex match :P

This comment has been minimized.

@jugglinmike

jugglinmike Jul 9, 2014

Author Contributor

I don't write nearly enough Ruby to know any better :P What would you recommend?

This comment has been minimized.

@bobthecow

bobthecow Jul 9, 2014

Member
i.identifier =~ args.first

This comment has been minimized.

@jugglinmike

jugglinmike Jul 9, 2014

Author Contributor

Ah, okay. Fixed.

I'm curious: is this purely stylistic or is there some performance consideration as well?

This comment has been minimized.

@bobthecow

bobthecow Jul 10, 2014

Member

I don't know, I've never benchmarked it.

rb_str_aref_m(int argc, VALUE *argv, VALUE str)
{
    if (argc == 2) {
        if (RB_TYPE_P(argv[0], T_REGEXP)) {
            return rb_str_subpat(str, argv[0], argv[1]);
        }
        return rb_str_substr(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]));
    }
    rb_check_arity(argc, 1, 2);
    return rb_str_aref(str, argv[0]);
}

I imagine it's ever so slightly slower, since it has to check the number of arguments and check whether the first is a regexp before calling it, but mostly it's because it doesn't look right.

Support filtering Items Arrays by Regex
This is a convenience API for working with the `@Items` array in nanoc's
`preprocess` block. In that context, it is common to manipulate items
according to a shared part of their identifier.

For example, with this change in place, users could succinctly create a
collection of all authors for a given site's weblog:

    @Items[%r{^/weblog/authors/}].each { |author|
      # etc...
    }
@coveralls

This comment has been minimized.

Copy link

coveralls commented Jul 10, 2014

Coverage Status

Coverage increased (+0.71%) when pulling bb4a6c0 on jugglinmike:items-by-regex into db1db13 on nanoc:master.

@gpakosz

This comment has been minimized.

Copy link
Member

gpakosz commented Jul 22, 2014

LGTM

ddfreyne added a commit that referenced this pull request Aug 10, 2014

Merge pull request #458 from jugglinmike/items-by-regex
Support filtering Items Arrays by Regex

@ddfreyne ddfreyne merged commit 93d5499 into nanoc:master Aug 10, 2014

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details
@ddfreyne

This comment has been minimized.

Copy link
Member

ddfreyne commented Aug 10, 2014

Sweet!

@ddfreyne ddfreyne removed the to review label Mar 7, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.