From 6a97a7406213ab23f0932f815924c945491a7765 Mon Sep 17 00:00:00 2001 From: Denis Defreyne Date: Tue, 26 Jul 2011 20:56:44 +0200 Subject: [PATCH] made @config available in rules DSL (fixes #37) --- lib/nanoc3/base/compilation/compiler_dsl.rb | 9 +++++--- .../base/compilation/rules_collection.rb | 2 +- test/base/test_compiler_dsl.rb | 23 ++++++++++++------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/nanoc3/base/compilation/compiler_dsl.rb b/lib/nanoc3/base/compilation/compiler_dsl.rb index c92a32edae..4bea251b6a 100644 --- a/lib/nanoc3/base/compilation/compiler_dsl.rb +++ b/lib/nanoc3/base/compilation/compiler_dsl.rb @@ -9,10 +9,13 @@ class CompilerDSL # # @api private # - # @param [Nanoc3::RulesCollection] rules_collection The collection of rules - # to modify when loading this DSL - def initialize(rules_collection) + # @param [Nanoc3::RulesCollection] rules_collection The collection of + # rules to modify when loading this DSL + # + # @param [Hash] config The site configuration + def initialize(rules_collection, config) @rules_collection = rules_collection + @config = config end # Creates a preprocessor block that will be executed after all data is diff --git a/lib/nanoc3/base/compilation/rules_collection.rb b/lib/nanoc3/base/compilation/rules_collection.rb index a2f833a9d8..8b6027dcbf 100644 --- a/lib/nanoc3/base/compilation/rules_collection.rb +++ b/lib/nanoc3/base/compilation/rules_collection.rb @@ -165,7 +165,7 @@ def filter_for_layout(layout) # Returns the Nanoc3::CompilerDSL that should be used for this site. def dsl - Nanoc3::CompilerDSL.new(self) + Nanoc3::CompilerDSL.new(self, @compiler.site.config) end memoize :dsl diff --git a/test/base/test_compiler_dsl.rb b/test/base/test_compiler_dsl.rb index b308d1333e..af4a311245 100644 --- a/test/base/test_compiler_dsl.rb +++ b/test/base/test_compiler_dsl.rb @@ -46,7 +46,7 @@ def test_passthrough def test_identifier_to_regex_without_wildcards # Create compiler DSL - compiler_dsl = Nanoc3::CompilerDSL.new(nil) + compiler_dsl = Nanoc3::CompilerDSL.new(nil, {}) actual = compiler_dsl.instance_eval { identifier_to_regex('foo') } expected = %r{^/foo/$} @@ -60,7 +60,7 @@ def test_identifier_to_regex_without_wildcards def test_identifier_to_regex_with_one_wildcard # Create compiler DSL - compiler_dsl = Nanoc3::CompilerDSL.new(nil) + compiler_dsl = Nanoc3::CompilerDSL.new(nil, {}) actual = compiler_dsl.instance_eval { identifier_to_regex('foo/*/bar') } expected = %r{^/foo/(.*?)/bar/$} @@ -74,7 +74,7 @@ def test_identifier_to_regex_with_one_wildcard def test_identifier_to_regex_with_two_wildcards # Create compiler DSL - compiler_dsl = Nanoc3::CompilerDSL.new(nil) + compiler_dsl = Nanoc3::CompilerDSL.new(nil, {}) actual = compiler_dsl.instance_eval { identifier_to_regex('foo/*/bar/*/qux') } expected = %r{^/foo/(.*?)/bar/(.*?)/qux/$} @@ -88,7 +88,7 @@ def test_identifier_to_regex_with_two_wildcards def test_identifier_to_regex_with_just_one_wildcard # Create compiler DSL - compiler_dsl = Nanoc3::CompilerDSL.new(nil) + compiler_dsl = Nanoc3::CompilerDSL.new(nil, {}) actual = compiler_dsl.instance_eval { identifier_to_regex('*') } expected = %r{^/(.*?)$} @@ -102,7 +102,7 @@ def test_identifier_to_regex_with_just_one_wildcard def test_identifier_to_regex_with_root # Create compiler DSL - compiler_dsl = Nanoc3::CompilerDSL.new(nil) + compiler_dsl = Nanoc3::CompilerDSL.new(nil, {}) actual = compiler_dsl.instance_eval { identifier_to_regex('/') } expected = %r{^/$} @@ -116,7 +116,7 @@ def test_identifier_to_regex_with_root def test_identifier_to_regex_with_only_children # Create compiler DSL - compiler_dsl = Nanoc3::CompilerDSL.new(nil) + compiler_dsl = Nanoc3::CompilerDSL.new(nil, {}) actual = compiler_dsl.instance_eval { identifier_to_regex('/foo/*/') } expected = %r{^/foo/(.*?)/$} @@ -130,7 +130,7 @@ def test_identifier_to_regex_with_only_children def test_identifier_to_regex_with_plus_wildcard # Create compiler DSL - compiler_dsl = Nanoc3::CompilerDSL.new(nil) + compiler_dsl = Nanoc3::CompilerDSL.new(nil, {}) actual = compiler_dsl.instance_eval { identifier_to_regex('/foo/+') } expected = %r{^/foo/(.+?)/$} @@ -145,10 +145,17 @@ def test_identifier_to_regex_with_plus_wildcard end def test_dsl_has_no_access_to_compiler - compiler_dsl = Nanoc3::CompilerDSL.new(nil) + compiler_dsl = Nanoc3::CompilerDSL.new(nil, {}) assert_raises(NameError) do compiler_dsl.instance_eval { compiler } end end + def test_config + $venetian = 'unsnares' + compiler_dsl = Nanoc3::CompilerDSL.new(nil, { :venetian => 'snares' }) + compiler_dsl.instance_eval { $venetian = @config[:venetian] } + assert_equal 'snares', $venetian + end + end