Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve helper handling in Nanoc::Int::Context #1170

Merged
merged 1 commit into from May 14, 2017
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Include helpers in Nanoc::Int::Context

  • Loading branch information...
ddfreyne committed Apr 22, 2017
commit f872077335732b258f8479549ef7ef065289f74f
@@ -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
@@ -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.
@@ -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
@@ -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
@@ -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)
@@ -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
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.