Skip to content

Commit

Permalink
Add #include_rules to the nanoc compiler DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
bobthecow committed Aug 17, 2012
1 parent 85e02aa commit aaf737f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/nanoc/base/compilation/compiler_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,25 @@ def passthrough(identifier, params={})
@rules_collection.add_item_routing_rule(routing_rule, :before)
end

# Includes an additional rules file in the current rules collection.
#
# @param [String] name The name of the rules file — an ".rb" extension is
# implied if not explicitly given
#
# @return [void]
#
# @example Including two additional rules files, 'rules/assets.rb' and
# 'rules/content.rb'
#
# include_rules 'rules/assets'
# include_rules 'rules/content'
def include_rules(name)
filename = [ "#{name}", "#{name}.rb", "./#{name}", "./#{name}.rb" ].find { |f| File.file?(f) }
raise Nanoc::Errors::NoRulesFileFound.new if filename.nil?

self.instance_eval(File.read(filename), filename)
end

private

# Converts the given identifier, which can contain the '*' or '+'
Expand Down
24 changes: 24 additions & 0 deletions test/base/test_compiler_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@ def test_layout
# TODO implement
end

def test_include_rules
# Create site
Nanoc::CLI.run %w( create_site with_bonus_rules )
FileUtils.cd('with_bonus_rules') do
# Create rep
item = Nanoc::Item.new('foo', { :extension => 'bar' }, '/foo/')
rep = Nanoc::ItemRep.new(item, :default)

# Create a bonus rules file
File.open('more_rules.rb', 'w') { |io| io.write "passthrough '/foo/'" }

# Create other necessary stuff
site = Nanoc::Site.new('.')
site.items << item
dsl = site.compiler.rules_collection.dsl

# Include rules
dsl.include_rules 'more_rules'

# Check that the rule made it into the collection
refute_nil site.compiler.rules_collection.routing_rule_for(rep)
end
end

def test_passthrough
# Create site
Nanoc::CLI.run %w( create_site bar)
Expand Down

0 comments on commit aaf737f

Please sign in to comment.