Skip to content
Permalink
Browse files

Merge pull request #619 from nanoc/document-mixins

Create mutable document view
  • Loading branch information...
ddfreyne committed Jun 6, 2015
2 parents 78aa8a7 + 5ca94b8 commit 0f7cfecc91d36d18819167456ee10d4cffec89d1
@@ -1,5 +1,7 @@
require_relative 'views/mixins/document'
require_relative 'views/mixins/mutable_document'

require_relative 'views/config'
require_relative 'views/document'
require_relative 'views/identifiable_collection'
require_relative 'views/item'
require_relative 'views/item_collection'
@@ -1,5 +1,7 @@
module Nanoc
class ItemView < ::Nanoc::DocumentView
class ItemView
include Nanoc::DocumentViewMixin

# Returns the compiled content.
#
# @option params [String] :rep (:default) The name of the representation
@@ -1,4 +1,5 @@
module Nanoc
class LayoutView < ::Nanoc::DocumentView
class LayoutView
include Nanoc::DocumentViewMixin
end
end
@@ -1,5 +1,5 @@
module Nanoc
class DocumentView
module DocumentViewMixin
# @api private
NONE = Object.new

@@ -0,0 +1,22 @@
module Nanoc
module MutableDocumentViewMixin
# Sets the value for the given attribute.
#
# @param [Symbol] key
#
# @see Hash#[]=
def []=(key, value)
unwrap.attributes[key] = value
end

# Updates the attributes based on the given hash.
#
# @param [Hash] hash
#
# @return [self]
def update_attributes(hash)
hash.each { |k, v| unwrap.attributes[k] = v }
self
end
end
end
@@ -1,22 +1,5 @@
module Nanoc
class MutableItemView < Nanoc::ItemView
# Sets the value for the given attribute.
#
# @param [Symbol] key
#
# @see Hash#[]=
def []=(key, value)
unwrap.attributes[key] = value
end

# Updates the attributes based on the given hash.
#
# @param [Hash] hash
#
# @return [self]
def update_attributes(hash)
hash.each { |k, v| unwrap.attributes[k] = v }
self
end
include Nanoc::MutableDocumentViewMixin
end
end
@@ -1,12 +1,5 @@
module Nanoc
class MutableLayoutView < Nanoc::LayoutView
# Sets the value for the given attribute.
#
# @param [Symbol] key
#
# @see Hash#[]=
def []=(key, value)
unwrap.attributes[key] = value
end
include Nanoc::MutableDocumentViewMixin
end
end
@@ -0,0 +1,28 @@
shared_examples 'a mutable document view' do
describe '#[]=' do
let(:item) { entity_class.new('content', {}, '/asdf/') }
let(:view) { described_class.new(item) }

it 'sets attributes' do
view[:title] = 'Donkey'
expect(view[:title]).to eq('Donkey')
end
end

describe '#update_attributes' do
let(:item) { entity_class.new('content', {}, '/asdf/') }
let(:view) { described_class.new(item) }

let(:update) { { friend: 'Giraffe' } }

subject { view.update_attributes(update) }

it 'sets attributes' do
expect { subject }.to change { view[:friend] }.from(nil).to('Giraffe')
end

it 'returns self' do
expect(subject).to equal(view)
end
end
end
@@ -1,28 +1,4 @@
describe Nanoc::MutableItemView do
describe '#[]=' do
let(:item) { Nanoc::Int::Item.new('content', {}, '/asdf/') }
let(:view) { described_class.new(item) }

it 'sets attributes' do
view[:title] = 'Donkey'
expect(view[:title]).to eq('Donkey')
end
end

describe '#update_attributes' do
let(:item) { Nanoc::Int::Item.new('content', {}, '/asdf/') }
let(:view) { described_class.new(item) }

let(:update) { { friend: 'Giraffe' } }

subject { view.update_attributes(update) }

it 'sets attributes' do
expect { subject }.to change { view[:friend] }.from(nil).to('Giraffe')
end

it 'returns self' do
expect(subject).to equal(view)
end
end
let(:entity_class) { Nanoc::Int::Item }
it_behaves_like 'a mutable document view'
end
@@ -1,11 +1,4 @@
describe Nanoc::MutableLayoutView do
describe '#[]=' do
let(:layout) { Nanoc::Int::Layout.new('content', {}, '/asdf/') }
let(:view) { described_class.new(layout) }

it 'sets attributes' do
view[:title] = 'Donkey'
expect(view[:title]).to eq('Donkey')
end
end
let(:entity_class) { Nanoc::Int::Layout }
it_behaves_like 'a mutable document view'
end

0 comments on commit 0f7cfec

Please sign in to comment.
You can’t perform that action at this time.