Skip to content

Commit

Permalink
Merge 223a527 into dcd1aa2
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebergen committed Apr 27, 2019
2 parents dcd1aa2 + 223a527 commit c2ca8b8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/shortcode/presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ def self.register(configuration, presenter)

def self.validate(presenter)
raise ArgumentError, "The presenter must define the class method #for" unless presenter.respond_to?(:for)
unless presenter.private_instance_methods(false).include?(:initialize)
raise ArgumentError, "The presenter must define an initialize method"
end
raise ArgumentError, "The presenter must define an initialize method" unless init_defined?(presenter)

%w[content attributes].each do |method|
unless presenter.method_defined?(method.to_sym)
raise ArgumentError, "The presenter must define the method ##{method}"
end
end
end

def self.init_defined?(presenter)
return false if presenter == Object

presenter.private_instance_methods(false).include?(:initialize) || init_defined?(presenter.superclass)
end

def initialize(name, configuration, attributes, content, additional_attributes)
@configuration = configuration
@attributes = attributes
Expand Down
8 changes: 8 additions & 0 deletions spec/shortcode/presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require "support/presenters/multiple_presenter"
require "support/presenters/missing_for_presenter"
require "support/presenters/missing_initialize_presenter"
require "support/presenters/child_presenter"
require "support/presenters/missing_content_presenter"
require "support/presenters/missing_attributes_presenter"

Expand Down Expand Up @@ -101,6 +102,13 @@
end
end

context "when initialize exists on non-Object ancestor class" do
it "does not raise an exception" do
expect { shortcode.register_presenter ChildPresenter }
.not_to raise_error
end
end

context "when missing #content method" do
it "raises an exception" do
expect { shortcode.register_presenter MissingContentPresenter }
Expand Down
11 changes: 11 additions & 0 deletions spec/support/presenters/child_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "support/presenters/parent_presenter"

class ChildPresenter < ParentPresenter

def self.for; end

def content; end

def attributes; end

end
5 changes: 5 additions & 0 deletions spec/support/presenters/parent_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ParentPresenter

def initialize; end

end

0 comments on commit c2ca8b8

Please sign in to comment.