Skip to content

Commit

Permalink
Adding the simplest possible extension system to prawn.
Browse files Browse the repository at this point in the history
  • Loading branch information
JEG2 committed Sep 14, 2009
1 parent 849f959 commit 7e25baf
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/prawn/core.rb
Expand Up @@ -65,6 +65,7 @@ def configuration(*args)

require "prawn/compatibility"
require "prawn/errors"
require "prawn/extendable"
require "prawn/pdf_object"
require "prawn/object_store"
require "prawn/snapshot"
Expand Down
3 changes: 2 additions & 1 deletion lib/prawn/document.rb
Expand Up @@ -50,7 +50,8 @@ module Prawn
#
# See the new and generate methods for further details on the above.
class Document


extend Extendable
include Text
include PageGeometry
include Internals
Expand Down
13 changes: 13 additions & 0 deletions lib/prawn/extendable.rb
@@ -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
6 changes: 6 additions & 0 deletions spec/document_spec.rb
Expand Up @@ -3,6 +3,12 @@

require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")

describe "Prawn Document" do
it "should be Extendable" do
assert_kind_of(Extendable, Prawn::Document)
end
end

describe "The cursor" do
it "should equal pdf.y - bounds.absolute_bottom" do
pdf = Prawn::Document.new
Expand Down
38 changes: 38 additions & 0 deletions spec/extendable_spec.rb
@@ -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

0 comments on commit 7e25baf

Please sign in to comment.