Skip to content

Commit

Permalink
Include helpers in Nanoc::Int::Context
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed May 14, 2017
1 parent b648a4a commit f872077
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/nanoc/base/entities/code_snippet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def initialize(data, filename)
#
# @return [void]
def load
eval('def self.use_helper(mod); Nanoc::Int::Context.instance_eval { include mod }; end', TOPLEVEL_BINDING)
eval(@data, TOPLEVEL_BINDING, @filename)
nil
end
Expand Down
3 changes: 2 additions & 1 deletion lib/nanoc/rule_dsl/compiler_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Nanoc::RuleDSL
# Contains methods that will be executed by the site’s `Rules` file.
#
# @api private
class CompilerDSL
class CompilerDSL < Nanoc::Int::Context
# The current rules filename.
#
# @return [String] The current rules filename.
Expand All @@ -23,6 +23,7 @@ class CompilerDSL
def initialize(rules_collection, config)
@rules_collection = rules_collection
@config = config
super({ config: config })
end

# Creates a preprocessor block that will be executed after all data is
Expand Down
51 changes: 51 additions & 0 deletions spec/nanoc/base/entities/code_snippet_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

describe Nanoc::Int::CodeSnippet do
subject(:code_snippet) { described_class.new(data, 'lib/foo.rb') }

describe '#load' do
subject { code_snippet.load }

describe 'calling #include' do
let(:data) do
<<~EOS
module CodeSnippetSpecHelper1
def fe345b48e4
"fe345b48e4"
end
end
include CodeSnippetSpecHelper1
EOS
end

it 'makes helper functions available in contexts' do
expect { subject }
.to change { [Nanoc::Int::Context.new({}).respond_to?(:fe345b48e4), Complex.respond_to?(:fe345b48e4)] }
.from([false, false])
.to([true, true])
end
end

describe 'calling #use_helper' do
let(:data) do
<<~EOS
module CodeSnippetSpecHelper2
def e0f0c30b5e
"e0f0c30b5e"
end
end
use_helper CodeSnippetSpecHelper2
EOS
end

it 'makes helper functions available everywhere' do
expect { subject }
.to change { [Nanoc::Int::Context.new({}).respond_to?(:e0f0c30b5e), Complex.respond_to?(:e0f0c30b5e)] }
.from([false, false])
.to([true, false])
end
end
end
end
6 changes: 6 additions & 0 deletions spec/nanoc/cli/commands/shell_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
end

it 'can be invoked' do
rules_collection = Nanoc::RuleDSL::RulesCollection.new
config = Nanoc::Int::Configuration.new.with_defaults

dsl = Nanoc::RuleDSL::CompilerDSL.new(rules_collection, config)
allow(Nanoc::RuleDSL::CompilerDSL).to receive(:new).and_return(dsl)

context = Object.new
allow(Nanoc::Int::Context).to receive(:new).with(anything).and_return(context)
expect(context).to receive(:pry)
Expand Down
7 changes: 7 additions & 0 deletions test/rule_dsl/test_compiler_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -438,4 +438,11 @@ def test_config
compiler_dsl.instance_eval { $venetian = @config[:venetian] }
assert_equal 'snares', $venetian
end

def test_config_without_sigil
$venetian = 'unsnares'
compiler_dsl = Nanoc::RuleDSL::CompilerDSL.new(nil, venetian: 'snares')
compiler_dsl.instance_eval { $venetian = config[:venetian] }
assert_equal 'snares', $venetian
end
end

0 comments on commit f872077

Please sign in to comment.