lib/nanoc/base/compilation/rule.rb in nanoc-4.0.0a1 vs lib/nanoc/base/compilation/rule.rb in nanoc-4.0.0a2
- old
+ new
@@ -3,15 +3,10 @@
module Nanoc::Int
# Contains the processing information for a item.
#
# @api private
class Rule
- # @return [Regexp] The regex that determines which items this rule can be
- # applied to. This rule can be applied to items with a identifier
- # matching this regex.
- attr_reader :identifier_regex
-
# @return [Symbol] The name of the representation that will be compiled
# using this rule
attr_reader :rep_name
# @return [Symbol] The name of the snapshot this rule will apply to.
@@ -22,24 +17,28 @@
# Creates a new item compilation rule with the given identifier regex,
# compiler and block. The block will be called during compilation with the
# item rep as its argument.
#
- # @param [Regexp] identifier_regex A regular expression that will be used
- # to determine whether this rule is applicable to certain items.
+ # @param [Nanoc::Int::Pattern] pattern
#
# @param [String, Symbol] rep_name The name of the item representation
# where this rule can be applied to
#
# @param [Proc] block A block that will be called when matching items are
# compiled
#
# @option params [Symbol, nil] :snapshot (nil) The name of the snapshot
# this rule will apply to. Ignored for compilation rules, but used for
# routing rules.
- def initialize(identifier_regex, rep_name, block, params = {})
- @identifier_regex = identifier_regex
+ def initialize(pattern, rep_name, block, params = {})
+ # TODO: remove me
+ unless pattern.is_a?(Nanoc::Int::StringPattern) || pattern.is_a?(Nanoc::Int::RegexpPattern)
+ raise "Can only create rules with patterns"
+ end
+
+ @pattern = pattern
@rep_name = rep_name.to_sym
@snapshot_name = params[:snapshot_name]
@block = block
end
@@ -47,11 +46,11 @@
# @param [Nanoc::Int::Item] item The item to check
#
# @return [Boolean] true if this rule can be applied to the given item
# rep, false otherwise
def applicable_to?(item)
- item.identifier =~ @identifier_regex
+ @pattern.match?(item.identifier)
end
# Applies this rule to the given item rep.
#
# @param [Nanoc::Int::ItemRep] rep The item representation where this rule
@@ -78,10 +77,9 @@
#
# @param [String] identifier Identifier to capture groups for
#
# @return [nil, Array] Captured groups, if any
def matches(identifier)
- matches = @identifier_regex.match(identifier.to_s)
- matches && matches.captures
+ @pattern.captures(identifier)
end
end
end