Scorched icon indicating copy to clipboard operation
Scorched copied to clipboard

Extract private compile method to helper method?

Open tmilker opened this issue 9 years ago • 3 comments

What do you think about extracting the private compile method in controller.rb to a public helper method? The reason I ask is that I need an "except" before filter that will exclude certain paths. Using scorched's compile method for the comparison is the best way to make sure this stays in sync with the way scorched compiles path matching regular expressions.

tmilker avatar Jun 08 '15 14:06 tmilker

Can you explain how you would use the compile method to achieve this, or maybe give me an example of how you'd implement your except filter. I just want to better understand what you're trying to achieve.

Wardrop avatar Jun 11 '15 23:06 Wardrop

This is what I'm currently using it for:

Scorched::Controller.conditions << {
  except: proc { |path|
    if path.is_a? String
      path = [path]
    end

    if !path.is_a? Array
      raise ArgumentError, "Expected a String or an Array"
    end

    not_matched = true

    path.each do |p|
      not_matched = false and break if ScorchedHelper.compile(p).match request.path_info
    end

    not_matched
  }
}

tmilker avatar Jun 11 '15 23:06 tmilker

Ah yeah, that seems like a pretty valid use case for compile; I can imagine other related use cases. I suppose it shouldn't be a problem making it public, it's a pretty simple method definition that should be pretty stable. I'll leave this one open and will make sure it makes it into the next release.

Wardrop avatar Jun 11 '15 23:06 Wardrop