forked from prawnpdf/prawn
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the simplest possible extension system to prawn.
- Loading branch information
Showing
5 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# coding: utf-8 | ||
|
||
module Extendable | ||
def extensions | ||
@extensions ||= [ ] | ||
end | ||
|
||
def new(*args, &block) | ||
o = super | ||
o.extend(*extensions.reverse) unless extensions.empty? | ||
o | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# encoding: utf-8 | ||
|
||
require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper") | ||
|
||
class ExampleClass | ||
extend Extendable | ||
|
||
def example | ||
:example | ||
end | ||
end | ||
|
||
module ExampleExtension | ||
def example | ||
"extended #{super}" | ||
end | ||
end | ||
|
||
ExampleClass.extensions << ExampleExtension | ||
|
||
describe "Extenable" do | ||
it "should add extension management to a class" do | ||
assert_kind_of(Extendable, ExampleClass) | ||
assert_instance_of(Array, ExampleClass.extensions) | ||
end | ||
|
||
it "should mix all extensions into new instances" do | ||
assert(!ExampleClass.extensions.empty?, "There were no extensions added") | ||
instance = ExampleClass.new | ||
ExampleClass.extensions.each do |extension| | ||
assert_kind_of(extension, instance) | ||
end | ||
end | ||
|
||
it "should allow for the normal use of super" do | ||
ExampleClass.new.example.should == "extended example" | ||
end | ||
end |