Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

v 1

  • Loading branch information...
commit 668d807cde3300ee79996571e1bb4dd07569b119 1 parent 4e44391
@austinthecoder authored
View
1  .gitignore
@@ -1,6 +1,7 @@
*.gem
*.rbc
.rspec
+.rvmrc
.bundle
.config
.yardoc
View
6 lib/poser.rb
@@ -2,6 +2,8 @@ module Poser
end
require "poser/version"
-require 'poser/enumerable_presenter'
require 'poser/presenter'
-require 'poser/presenter/helper'
+require 'poser/enumerable_presenter'
+require 'poser/presentable'
+require 'poser/context'
+require 'poser/util'
View
11 lib/poser/context.rb
@@ -0,0 +1,11 @@
+require 'poser/presenter'
+
+module Poser
+ module Context
+
+ def present(object, context = self)
+ Presenter.present object, context
+ end
+
+ end
+end
View
25 lib/poser/presentable.rb
@@ -0,0 +1,25 @@
+require 'poser/util'
+
+module Poser
+ module Presentable
+
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+ def presenter_class
+ Util.first_available_class [
+ "#{self}::Presenter",
+ "#{self}Presenter",
+ "Poser::Presenter"
+ ]
+ end
+ end
+
+ def to_presenter(context)
+ self.class.presenter_class.new self, context
+ end
+
+ end
+end
View
18 lib/poser/presenter.rb
@@ -10,23 +10,7 @@ def present(object, context)
elsif object.respond_to? :to_presenter
object.to_presenter context
else
- determine_presenter_class(object).new object, context
- end
- end
-
- def presents(name)
- alias_method name, :__getobj__
- end
-
- private
-
- def determine_presenter_class(object)
- object.class::Presenter
- rescue NameError
- if object.is_a?(Enumerable) || (defined?(ActiveRecord) && object.is_a?(ActiveRecord::Relation))
- EnumerablePresenter
- else
- self
+ new object, context
end
end
end
View
13 lib/poser/presenter/helper.rb
@@ -1,13 +0,0 @@
-require 'poser/presenter'
-
-module Poser
- class Presenter
- module Helper
-
- def present(object, context = self)
- Presenter.present object, context
- end
-
- end
- end
-end
View
28 lib/poser/util.rb
@@ -0,0 +1,28 @@
+module Poser
+ class Util
+
+ class << self
+ def first_available_class(class_names)
+ if class_name = class_names.shift
+ constantize_string class_name
+ end
+ rescue NameError
+ retry
+ end
+
+ private
+
+ def constantize_string(camel_cased_word)
+ camel_cased_word.constantize if camel_cased_word.respond_to?(:constantize)
+
+ names = camel_cased_word.split('::')
+ names.shift if names.empty? || names.first.empty?
+
+ names.inject(Object) do |constant, name|
+ constant.const_get(name, false)
+ end
+ end
+ end
+
+ end
+end
View
2  lib/poser/version.rb
@@ -1,3 +1,3 @@
module Poser
- VERSION = "0.0.1"
+ VERSION = "1.0.0"
end
View
31 spec/poser/context_spec.rb
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+describe Poser::Context do
+ subject { Class.new { include Poser::Context }.new }
+
+ describe "instance methods" do
+ describe "present" do
+ before do
+ @object = Object.new
+ @result = Object.new
+ end
+
+ it "returns the result of telling Poser::Presenter to present the given object in the given context" do
+ cntxt = Object.new
+ Poser::Presenter.stub(:present) do |object, context|
+ @result if object == @object && context == cntxt
+ end
+
+ subject.present(@object, cntxt).should == @result
+ end
+
+ it "returns the result of telling Poser::Presenter to present the given object, using itself as the context when a context isn't given" do
+ Poser::Presenter.stub(:present) do |object, context|
+ @result if object == @object && context == subject
+ end
+
+ subject.present(@object).should == @result
+ end
+ end
+ end
+end
View
44 spec/poser/presentable_spec.rb
@@ -0,0 +1,44 @@
+require 'spec_helper'
+
+describe Poser::Presentable do
+ before(:all) { C = Class.new { include Poser::Presentable } }
+ after(:all) { Object.send :remove_const, :C }
+
+ describe "class methods" do
+ subject { C }
+
+ describe "presenter_class" do
+ after :all do
+ Object.send :remove_const, :CPresenter
+ C.send :remove_const, :Presenter
+ end
+
+ it "returns Poser::Presenter when no presenter classes are available for this class" do
+ subject.presenter_class.should == Poser::Presenter
+ end
+
+ it "returns CPresenter if it exists" do
+ CPresenter = Class.new
+ subject.presenter_class.should == CPresenter
+ end
+
+ it "returns the Presenter class namespaced under this class if it exists" do
+ C::Presenter = Class.new
+ subject.presenter_class.should == C::Presenter
+ end
+ end
+ end
+
+ describe "instance methods" do
+ subject { C.new }
+
+ describe "to_presenter" do
+ it "presents itself within the context given using the appropriate class" do
+ context = Object.new
+ c = Class.new Poser::Presenter
+ C.stub(:presenter_class) { c }
+ subject.to_presenter(context).should == c.new(subject, context)
+ end
+ end
+ end
+end
View
11 spec/poser/presenter_spec.rb
@@ -20,6 +20,10 @@
@object.stub(:to_presenter) { result }
subject.present(@object, @context).should == result
end
+
+ it "otherwise returns an instance of itself" do
+ subject.present(@object, @context).should == subject.new(@object, @context)
+ end
end
end
@@ -31,6 +35,9 @@
@context = Object.new
end
+ it { should be_presented }
+ its(:context) { should == @context }
+
describe "==" do
it "is true given a #{described_class} instantiated with the same presentee and context" do
subject.should == described_class.new(@presentee, @context)
@@ -49,8 +56,6 @@
end
end
- it { should be_presented }
-
describe "present" do
it "returns the result of telling the #{described_class} to present the given object with the same context" do
result = Object.new
@@ -63,7 +68,5 @@
subject.present(object).should == result
end
end
-
- its(:context) { should == @context }
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.