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

Create mutable document view #619

Merged
merged 1 commit into from
Jun 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/nanoc/base/views.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
4 changes: 3 additions & 1 deletion lib/nanoc/base/views/item.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/nanoc/base/views/layout.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module Nanoc
class LayoutView < ::Nanoc::DocumentView
class LayoutView
include Nanoc::DocumentViewMixin
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Nanoc
class DocumentView
module DocumentViewMixin
# @api private
NONE = Object.new

Expand Down
22 changes: 22 additions & 0 deletions lib/nanoc/base/views/mixins/mutable_document.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 1 addition & 18 deletions lib/nanoc/base/views/mutable_item.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 1 addition & 8 deletions lib/nanoc/base/views/mutable_layout.rb
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions spec/nanoc/base/views/mutable_document_view_spec.rb
Original file line number Diff line number Diff line change
@@ -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
28 changes: 2 additions & 26 deletions spec/nanoc/base/views/mutable_item_spec.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 2 additions & 9 deletions spec/nanoc/base/views/mutable_layout_spec.rb
Original file line number Diff line number Diff line change
@@ -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