Skip to content

Commit

Permalink
Merge branch 'release-4.3.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Aug 22, 2016
2 parents d529a87 + 8daed52 commit 87c2499
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 35 deletions.
5 changes: 5 additions & 0 deletions lib/nanoc/base/compilation/dependency_tracker.rb
Expand Up @@ -12,11 +12,14 @@ def bounce(_obj)
end
end

include Nanoc::Int::ContractsSupport

def initialize(dependency_store)
@dependency_store = dependency_store
@stack = []
end

contract C::Or[Nanoc::Int::Item, Nanoc::Int::Layout] => C::Any
def enter(obj)
unless @stack.empty?
Nanoc::Int::NotificationCenter.post(:dependency_created, @stack.last, obj)
Expand All @@ -26,10 +29,12 @@ def enter(obj)
@stack.push(obj)
end

contract C::Or[Nanoc::Int::Item, Nanoc::Int::Layout] => C::Any
def exit(_obj)
@stack.pop
end

contract C::Or[Nanoc::Int::Item, Nanoc::Int::Layout] => C::Any
def bounce(obj)
enter(obj)
exit(obj)
Expand Down
4 changes: 2 additions & 2 deletions lib/nanoc/base/compilation/filter.rb
Expand Up @@ -38,10 +38,10 @@ class Filter < Nanoc::Int::Context
extend Nanoc::Int::PluginRegistry::PluginMethods

class << self
def define(ident)
def define(ident, &block)
filter_class = Class.new(::Nanoc::Filter) { identifier(ident) }
filter_class.send(:define_method, :run) do |content, params|
yield(content, params)
instance_exec(content, params, &block)
end
end

Expand Down
25 changes: 17 additions & 8 deletions lib/nanoc/base/plugin_registry.rb
Expand Up @@ -24,7 +24,8 @@ module PluginMethods
# @return [Array<Symbol>] The identifiers for this plugin
def identifiers(*identifiers)
if identifiers.empty?
Nanoc::Int::PluginRegistry.instance.identifiers_of(superclass, self)
registry = Nanoc::Int::PluginRegistry.instance
registry.identifiers_of(registry.root_class_of(self), self)
else
register(self, *identifiers)
end
Expand All @@ -45,7 +46,8 @@ def identifier(identifier = nil)
if identifier
identifiers(identifier)
else
Nanoc::Int::PluginRegistry.instance.identifiers_of(superclass, self).first
registry = Nanoc::Int::PluginRegistry.instance
registry.identifiers_of(registry.root_class_of(self), self).first
end
end

Expand All @@ -59,13 +61,9 @@ def identifier(identifier = nil)
#
# @return [void]
def register(class_or_name, *identifiers)
# Find plugin class
klass = self
klass = klass.superclass while klass.superclass.respond_to?(:register)

# Register
registry = Nanoc::Int::PluginRegistry.instance
registry.register(klass, class_or_name, *identifiers)
root = registry.root_class_of(self)
registry.register(root, class_or_name, *identifiers)
end

# @return [Hash<Symbol, Class>] All plugins of this type, with keys
Expand Down Expand Up @@ -159,6 +157,17 @@ def find_all(klass)
res
end

# @param [Class] klass
#
# @return [Class]
#
# @api private
def root_class_of(subclass)
root_class = subclass
root_class = root_class.superclass while root_class.superclass.respond_to?(:register)
root_class
end

# Returns a list of all plugins. The returned list of plugins is an array
# with array elements in the following format:
#
Expand Down
11 changes: 6 additions & 5 deletions lib/nanoc/helpers/rendering.rb
Expand Up @@ -13,9 +13,10 @@ module Rendering
# @return [String, nil]
def render(identifier, other_assigns = {}, &block)
# Find layout
layout = @layouts[identifier]
layout ||= @layouts[identifier.__nanoc_cleaned_identifier]
raise Nanoc::Int::Errors::UnknownLayout.new(identifier) if layout.nil?
layout_view = @layouts[identifier]
layout_view ||= @layouts[identifier.__nanoc_cleaned_identifier]
raise Nanoc::Int::Errors::UnknownLayout.new(identifier) if layout_view.nil?
layout = layout_view.unwrap

# Visit
dependency_tracker = @config._context.dependency_tracker
Expand All @@ -30,7 +31,7 @@ def render(identifier, other_assigns = {}, &block)
item: @item,
item_rep: @item_rep,
items: @items,
layout: layout,
layout: layout_view,
layouts: @layouts,
config: @config,
}.merge(other_assigns)
Expand All @@ -51,7 +52,7 @@ def render(identifier, other_assigns = {}, &block)
Nanoc::Int::NotificationCenter.post(:processing_started, layout)

# Layout
content = layout.unwrap.content
content = layout.content
arg = content.binary? ? content.filename : content.string
result = filter.setup_and_run(arg, filter_args)

Expand Down
4 changes: 2 additions & 2 deletions spec/nanoc/base/checksummer_spec.rb
Expand Up @@ -132,9 +132,9 @@
it { is_expected.to eql('Float<3.14>') }
end

context 'Fixnum' do
context 'Fixnum/Integer' do
let(:obj) { 3 }
it { is_expected.to eql('Fixnum<3>') }
it { is_expected.to match(/\A(Integer|Fixnum)<3>\z/) }
end

context 'Nanoc::Identifier' do
Expand Down
8 changes: 4 additions & 4 deletions spec/nanoc/base/entities/rule_memory_spec.rb
Expand Up @@ -96,17 +96,17 @@
subject { rule_memory.serialize }

before do
rule_memory.add_filter(:erb, { awesomeness: 123 })
rule_memory.add_filter(:erb, { awesomeness: 'high' })
rule_memory.add_snapshot(:bar, true, '/foo.md')
rule_memory.add_layout('/default.erb', { somelayoutparam: 444 })
rule_memory.add_layout('/default.erb', { somelayoutparam: 'yes' })
end

example do
expect(subject).to eql(
[
[:filter, :erb, 'y9yyZGXu0J04TcDR9oFI3EJM4Vk='],
[:filter, :erb, 'PeWUm2PtXYtqeHJdTqnY7kkwAow='],
[:snapshot, :bar, true, '/foo.md'],
[:layout, '/default.erb', 'PGQes7wXm3+K06vSBPYUJft57sM='],
[:layout, '/default.erb', '97LAe1pYTLKczxBsu+x4MmvqdkU='],
],
)
end
Expand Down
38 changes: 30 additions & 8 deletions spec/nanoc/base/filter_spec.rb
@@ -1,17 +1,39 @@
describe Nanoc::Filter do
describe '.define' do
before do
Nanoc::Filter.define(:nanoc_filter_define_sample) do |content, _params|
content.upcase
context 'simple filter' do
let(:filter_name) { 'b5355bbb4d772b9853d21be57da614dba521dbbb' }
let(:filter_class) { Nanoc::Filter.named(filter_name) }

before do
Nanoc::Filter.define(filter_name) do |content, _params|
content.upcase
end
end
end

it 'defines a filter' do
expect(Nanoc::Filter.named(:nanoc_filter_define_sample)).not_to be_nil
it 'defines a filter' do
expect(filter_class).not_to be_nil
end

it 'defines a callable filter' do
expect(filter_class.new.run('foo', {})).to eql('FOO')
end
end

it 'defines a callable filter' do
expect(Nanoc::Filter.named(:nanoc_filter_define_sample).new.run('foo', {})).to eql('FOO')
context 'filter that accesses assigns' do
let(:filter_name) { 'd7ed105d460e99a3d38f46af023d9490c140fdd9' }
let(:filter_class) { Nanoc::Filter.named(filter_name) }
let(:filter) { filter_class.new(assigns) }
let(:assigns) { { animal: 'Giraffe' } }

before do
Nanoc::Filter.define(filter_name) do |_content, _params|
@animal
end
end

it 'can access assigns' do
expect(filter.setup_and_run(:__irrelevant__, {})).to eq('Giraffe')
end
end
end
end
29 changes: 29 additions & 0 deletions spec/nanoc/base/plugin_registry_spec.rb
@@ -0,0 +1,29 @@
describe Nanoc::Int::PluginRegistry do
describe '.identifier(s)' do
let(:identifier) { :ce79f6b8ddb22233e9aaf7d8f011689492acf02f }

context 'direct subclass' do
example do
klass =
Class.new(Nanoc::Filter) do
identifier :plugin_registry_spec
end

expect(klass.identifier).to eql(:plugin_registry_spec)
end
end

context 'indirect subclass' do
example do
superclass = Class.new(Nanoc::Filter)

klass =
Class.new(superclass) do
identifier :plugin_registry_spec
end

expect(klass.identifier).to eql(:plugin_registry_spec)
end
end
end
end
23 changes: 18 additions & 5 deletions spec/nanoc/helpers/rendering_spec.rb
Expand Up @@ -2,15 +2,22 @@
describe '#render' do
subject { helper.instance_eval { render('/partial.erb') } }

before do
layout = ctx.create_layout(layout_content, {}, layout_identifier)
ctx.update_rule_memory(layout, rule_memory_for_layout)
end

let(:rule_memory_for_layout) do
[Nanoc::Int::RuleMemoryActions::Filter.new(:erb, {})]
end

let(:layout_view) do
ctx.create_layout(layout_content, {}, layout_identifier)
end

let(:layout) do
layout_view.unwrap
end

before do
ctx.update_rule_memory(layout, rule_memory_for_layout)
end

context 'legacy identifier' do
let(:layout_identifier) { Nanoc::Identifier.new('/partial/', type: :legacy) }

Expand All @@ -19,7 +26,13 @@

context 'layout without instructions' do
let(:layout_content) { 'blah' }

it { is_expected.to eql('blah') }

it 'tracks proper dependencies' do
expect(ctx.dependency_tracker).to receive(:enter).with(layout)
subject
end
end

context 'layout with instructions' do
Expand Down
5 changes: 5 additions & 0 deletions spec/nanoc/regressions/gh_928_spec.rb
@@ -0,0 +1,5 @@
describe 'GH-928', site: true, stdio: true do
example do
expect { Nanoc::CLI.run(%w(check --list)) }.to output(%r{^ css$}).to_stdout
end
end
5 changes: 4 additions & 1 deletion test/base/test_dependency_tracker.rb
Expand Up @@ -87,7 +87,10 @@ def test_objects_outdated_due_to
end

def test_enter_and_exit
items = [mock, mock]
items = [
Nanoc::Int::Item.new('Foo', {}, '/foo.md'),
Nanoc::Int::Item.new('Bar', {}, '/bar.md'),
]

store = Nanoc::Int::DependencyStore.new(items)
tracker = Nanoc::Int::DependencyTracker.new(store)
Expand Down

0 comments on commit 87c2499

Please sign in to comment.