poser 0.0.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -1,6 +1,7 @@
1
1
  *.gem
2
2
  *.rbc
3
3
  .rspec
4
+ .rvmrc
4
5
  .bundle
5
6
  .config
6
7
  .yardoc
@@ -2,6 +2,8 @@ module Poser
2
2
  end
3
3
 
4
4
  require "poser/version"
5
- require 'poser/enumerable_presenter'
6
5
  require 'poser/presenter'
7
- require 'poser/presenter/helper'
6
+ require 'poser/enumerable_presenter'
7
+ require 'poser/presentable'
8
+ require 'poser/context'
9
+ require 'poser/util'
@@ -0,0 +1,11 @@
1
+ require 'poser/presenter'
2
+
3
+ module Poser
4
+ module Context
5
+
6
+ def present(object, context = self)
7
+ Presenter.present object, context
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ require 'poser/util'
2
+
3
+ module Poser
4
+ module Presentable
5
+
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ def presenter_class
12
+ Util.first_available_class [
13
+ "#{self}::Presenter",
14
+ "#{self}Presenter",
15
+ "Poser::Presenter"
16
+ ]
17
+ end
18
+ end
19
+
20
+ def to_presenter(context)
21
+ self.class.presenter_class.new self, context
22
+ end
23
+
24
+ end
25
+ end
@@ -10,23 +10,7 @@ module Poser
10
10
  elsif object.respond_to? :to_presenter
11
11
  object.to_presenter context
12
12
  else
13
- determine_presenter_class(object).new object, context
14
- end
15
- end
16
-
17
- def presents(name)
18
- alias_method name, :__getobj__
19
- end
20
-
21
- private
22
-
23
- def determine_presenter_class(object)
24
- object.class::Presenter
25
- rescue NameError
26
- if object.is_a?(Enumerable) || (defined?(ActiveRecord) && object.is_a?(ActiveRecord::Relation))
27
- EnumerablePresenter
28
- else
29
- self
13
+ new object, context
30
14
  end
31
15
  end
32
16
  end
@@ -0,0 +1,28 @@
1
+ module Poser
2
+ class Util
3
+
4
+ class << self
5
+ def first_available_class(class_names)
6
+ if class_name = class_names.shift
7
+ constantize_string class_name
8
+ end
9
+ rescue NameError
10
+ retry
11
+ end
12
+
13
+ private
14
+
15
+ def constantize_string(camel_cased_word)
16
+ camel_cased_word.constantize if camel_cased_word.respond_to?(:constantize)
17
+
18
+ names = camel_cased_word.split('::')
19
+ names.shift if names.empty? || names.first.empty?
20
+
21
+ names.inject(Object) do |constant, name|
22
+ constant.const_get(name, false)
23
+ end
24
+ end
25
+ end
26
+
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module Poser
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Poser::Context do
4
+ subject { Class.new { include Poser::Context }.new }
5
+
6
+ describe "instance methods" do
7
+ describe "present" do
8
+ before do
9
+ @object = Object.new
10
+ @result = Object.new
11
+ end
12
+
13
+ it "returns the result of telling Poser::Presenter to present the given object in the given context" do
14
+ cntxt = Object.new
15
+ Poser::Presenter.stub(:present) do |object, context|
16
+ @result if object == @object && context == cntxt
17
+ end
18
+
19
+ subject.present(@object, cntxt).should == @result
20
+ end
21
+
22
+ 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
23
+ Poser::Presenter.stub(:present) do |object, context|
24
+ @result if object == @object && context == subject
25
+ end
26
+
27
+ subject.present(@object).should == @result
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe Poser::Presentable do
4
+ before(:all) { C = Class.new { include Poser::Presentable } }
5
+ after(:all) { Object.send :remove_const, :C }
6
+
7
+ describe "class methods" do
8
+ subject { C }
9
+
10
+ describe "presenter_class" do
11
+ after :all do
12
+ Object.send :remove_const, :CPresenter
13
+ C.send :remove_const, :Presenter
14
+ end
15
+
16
+ it "returns Poser::Presenter when no presenter classes are available for this class" do
17
+ subject.presenter_class.should == Poser::Presenter
18
+ end
19
+
20
+ it "returns CPresenter if it exists" do
21
+ CPresenter = Class.new
22
+ subject.presenter_class.should == CPresenter
23
+ end
24
+
25
+ it "returns the Presenter class namespaced under this class if it exists" do
26
+ C::Presenter = Class.new
27
+ subject.presenter_class.should == C::Presenter
28
+ end
29
+ end
30
+ end
31
+
32
+ describe "instance methods" do
33
+ subject { C.new }
34
+
35
+ describe "to_presenter" do
36
+ it "presents itself within the context given using the appropriate class" do
37
+ context = Object.new
38
+ c = Class.new Poser::Presenter
39
+ C.stub(:presenter_class) { c }
40
+ subject.to_presenter(context).should == c.new(subject, context)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -20,6 +20,10 @@ describe Poser::Presenter do
20
20
  @object.stub(:to_presenter) { result }
21
21
  subject.present(@object, @context).should == result
22
22
  end
23
+
24
+ it "otherwise returns an instance of itself" do
25
+ subject.present(@object, @context).should == subject.new(@object, @context)
26
+ end
23
27
  end
24
28
  end
25
29
 
@@ -31,6 +35,9 @@ describe Poser::Presenter do
31
35
  @context = Object.new
32
36
  end
33
37
 
38
+ it { should be_presented }
39
+ its(:context) { should == @context }
40
+
34
41
  describe "==" do
35
42
  it "is true given a #{described_class} instantiated with the same presentee and context" do
36
43
  subject.should == described_class.new(@presentee, @context)
@@ -49,8 +56,6 @@ describe Poser::Presenter do
49
56
  end
50
57
  end
51
58
 
52
- it { should be_presented }
53
-
54
59
  describe "present" do
55
60
  it "returns the result of telling the #{described_class} to present the given object with the same context" do
56
61
  result = Object.new
@@ -63,7 +68,5 @@ describe Poser::Presenter do
63
68
  subject.present(object).should == result
64
69
  end
65
70
  end
66
-
67
- its(:context) { should == @context }
68
71
  end
69
72
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: poser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -40,11 +40,15 @@ files:
40
40
  - README.md
41
41
  - Rakefile
42
42
  - lib/poser.rb
43
+ - lib/poser/context.rb
43
44
  - lib/poser/enumerable_presenter.rb
45
+ - lib/poser/presentable.rb
44
46
  - lib/poser/presenter.rb
45
- - lib/poser/presenter/helper.rb
47
+ - lib/poser/util.rb
46
48
  - lib/poser/version.rb
47
49
  - poser.gemspec
50
+ - spec/poser/context_spec.rb
51
+ - spec/poser/presentable_spec.rb
48
52
  - spec/poser/presenter_spec.rb
49
53
  - spec/poser_spec.rb
50
54
  - spec/spec_helper.rb
@@ -73,6 +77,8 @@ signing_key:
73
77
  specification_version: 3
74
78
  summary: A minimal implementation of the presenter pattern.
75
79
  test_files:
80
+ - spec/poser/context_spec.rb
81
+ - spec/poser/presentable_spec.rb
76
82
  - spec/poser/presenter_spec.rb
77
83
  - spec/poser_spec.rb
78
84
  - spec/spec_helper.rb
@@ -1,13 +0,0 @@
1
- require 'poser/presenter'
2
-
3
- module Poser
4
- class Presenter
5
- module Helper
6
-
7
- def present(object, context = self)
8
- Presenter.present object, context
9
- end
10
-
11
- end
12
- end
13
- end