Skip to content

Commit

Permalink
made @config available in rules DSL (fixes nanoc#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Jul 26, 2011
1 parent db02d3b commit 6a97a74
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
9 changes: 6 additions & 3 deletions lib/nanoc3/base/compilation/compiler_dsl.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/nanoc3/base/compilation/rules_collection.rb
Expand Up @@ -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

Expand Down
23 changes: 15 additions & 8 deletions test/base/test_compiler_dsl.rb
Expand Up @@ -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/$}
Expand All @@ -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/$}
Expand All @@ -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/$}
Expand All @@ -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{^/(.*?)$}
Expand All @@ -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{^/$}
Expand All @@ -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/(.*?)/$}
Expand All @@ -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/(.+?)/$}
Expand All @@ -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

0 comments on commit 6a97a74

Please sign in to comment.