Skip to content

Commit

Permalink
Mark views as frozen
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Jan 10, 2016
1 parent b541616 commit c116fa4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/nanoc/base/views/item_without_reps_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def parent

parent = @context.items[parent_identifier]

parent && self.class.new(parent, @context).freeze
parent && self.class.new(parent, @context)
end

# @return [Boolean] True if the item is binary, false otherwise
Expand Down
14 changes: 14 additions & 0 deletions lib/nanoc/base/views/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,19 @@ def initialize(context)
def _context
@context
end

# @api private
def unwrap
raise NotImplementedError
end

# True if the wrapped object is frozen; false otherwise.
#
# @return [Boolean]
#
# @see Object#frozen?
def frozen?
unwrap.frozen?
end
end
end
12 changes: 11 additions & 1 deletion spec/nanoc/base/views/item_view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,17 @@
expect(subject._context).to equal(view_context)
end

it { is_expected.to be_frozen }
context 'frozen parent' do
before do
parent_item.freeze
end

it { is_expected.to be_frozen }
end

context 'non-frozen parent' do
it { is_expected.not_to be_frozen }
end

context 'with root parent' do
let(:parent_item) { Nanoc::Int::Item.new('parent', {}, '/') }
Expand Down
19 changes: 19 additions & 0 deletions spec/nanoc/regressions/gh_795_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
describe 'GH-795', site: true, stdio: true do
before do
File.write('content/items.md', 'Frozen? <%= @items.unwrap.frozen? %>!')
File.write('content/items-view.md', 'Frozen? <%= @items.frozen? %>!')
File.write('Rules', <<EOS)
compile '/**/*' do
filter :erb
write item.identifier.without_ext + '.html'
end
EOS
end

it 'freezes @items' do
Nanoc::CLI.run(['compile'])

expect(File.read('output/items.html')).to eql('Frozen? true!')
expect(File.read('output/items-view.html')).to eql('Frozen? true!')
end
end

0 comments on commit c116fa4

Please sign in to comment.