poser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .rspec
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in poser.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Austin Schneider
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ # Poser
2
+
3
+ A minimal implementation of the presenter pattern.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'poser'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install poser
18
+
19
+ ## Usage
20
+
21
+ ```
22
+ class Battle
23
+ end
24
+
25
+ class Warrior < Poser::Presenter
26
+ def fighting?
27
+ true
28
+ end
29
+ end
30
+
31
+ class Person
32
+ def initialize(name)
33
+ @name = name
34
+ end
35
+
36
+ attr_reader :name
37
+
38
+ def to_presenter(context)
39
+ Warrior.new(self, context) if context.is_a?(Battle)
40
+ end
41
+
42
+ def fighting?
43
+ false
44
+ end
45
+ end
46
+
47
+ person = Person.new 'Austin'
48
+ person.name # 'Austin'
49
+ person.fighting? # false
50
+
51
+ battle = Battle.new
52
+
53
+ warrior = person.to_presenter battle
54
+ warrior.name # 'Austin'
55
+ warrior.fighting? # true
56
+ ```
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,7 @@
1
+ module Poser
2
+ end
3
+
4
+ require "poser/version"
5
+ require 'poser/enumerable_presenter'
6
+ require 'poser/presenter'
7
+ require 'poser/presenter/helper'
@@ -0,0 +1,11 @@
1
+ require 'poser/presenter'
2
+
3
+ module Poser
4
+ class EnumerablePresenter < Presenter
5
+
6
+ def each
7
+ __getobj__.each { |object| yield present(object) }
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,54 @@
1
+ require 'delegate'
2
+
3
+ module Poser
4
+ class Presenter < SimpleDelegator
5
+
6
+ class << self
7
+ def present(object, context)
8
+ if object.respond_to?(:presented?) && object.presented?
9
+ object
10
+ elsif object.respond_to? :to_presenter
11
+ object.to_presenter context
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
30
+ end
31
+ end
32
+ end
33
+
34
+ def initialize(presentee, context)
35
+ super presentee
36
+ @context = context
37
+ end
38
+
39
+ attr_reader :context
40
+
41
+ def present(object)
42
+ self.class.present object, context
43
+ end
44
+
45
+ def presented?
46
+ true
47
+ end
48
+
49
+ def ==(other)
50
+ other.is_a?(self.class) && context == other.context && __getobj__ == other.__getobj__
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,13 @@
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
@@ -0,0 +1,3 @@
1
+ module Poser
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/poser/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Austin Schneider"]
6
+ gem.email = ["austinthecoder@gmail.com"]
7
+ gem.description = 'A minimal implementation of the presenter pattern.'
8
+ gem.summary = 'A minimal implementation of the presenter pattern.'
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "poser"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Poser::VERSION
17
+
18
+ gem.add_development_dependency "rspec", ">= 2.9.0"
19
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe Poser::Presenter do
4
+ describe "class methods" do
5
+ subject { described_class }
6
+
7
+ describe "present" do
8
+ before do
9
+ @object = mock Object
10
+ @context = Object.new
11
+ end
12
+
13
+ it "returns the object if it's presented" do
14
+ @object.stub(:presented?) { true }
15
+ subject.present(@object, @context).should == @object
16
+ end
17
+
18
+ it "returns the result of the object presenting itself if it knows how" do
19
+ result = Object.new
20
+ @object.stub(:to_presenter) { result }
21
+ subject.present(@object, @context).should == result
22
+ end
23
+ end
24
+ end
25
+
26
+ describe "instance methods" do
27
+ subject { described_class.new @presentee, @context }
28
+
29
+ before do
30
+ @presentee = Object.new
31
+ @context = Object.new
32
+ end
33
+
34
+ describe "==" do
35
+ it "is true given a #{described_class} instantiated with the same presentee and context" do
36
+ subject.should == described_class.new(@presentee, @context)
37
+ end
38
+
39
+ it "is not true given a non-#{described_class}" do
40
+ subject.should_not == Object.new
41
+ end
42
+
43
+ it "is not true given a #{described_class} with a different presentee" do
44
+ subject.should_not == described_class.new(Object.new, @context)
45
+ end
46
+
47
+ it "is not true given a #{described_class} with a different context" do
48
+ subject.should_not == described_class.new(@presentee, Object.new)
49
+ end
50
+ end
51
+
52
+ it { should be_presented }
53
+
54
+ describe "present" do
55
+ it "returns the result of telling the #{described_class} to present the given object with the same context" do
56
+ result = Object.new
57
+ object = Object.new
58
+
59
+ described_class.stub :present do |presentee, context|
60
+ result if presentee == object && context == @context
61
+ end
62
+
63
+ subject.present(object).should == result
64
+ end
65
+ end
66
+
67
+ its(:context) { should == @context }
68
+ end
69
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe Poser do
4
+ end
@@ -0,0 +1,6 @@
1
+ require 'rspec'
2
+ require 'poser'
3
+
4
+ RSpec.configure do |c|
5
+ c.mock_with :rspec
6
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: poser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Austin Schneider
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.9.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.9.0
30
+ description: A minimal implementation of the presenter pattern.
31
+ email:
32
+ - austinthecoder@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE
40
+ - README.md
41
+ - Rakefile
42
+ - lib/poser.rb
43
+ - lib/poser/enumerable_presenter.rb
44
+ - lib/poser/presenter.rb
45
+ - lib/poser/presenter/helper.rb
46
+ - lib/poser/version.rb
47
+ - poser.gemspec
48
+ - spec/poser/presenter_spec.rb
49
+ - spec/poser_spec.rb
50
+ - spec/spec_helper.rb
51
+ homepage: ''
52
+ licenses: []
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 1.8.21
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: A minimal implementation of the presenter pattern.
75
+ test_files:
76
+ - spec/poser/presenter_spec.rb
77
+ - spec/poser_spec.rb
78
+ - spec/spec_helper.rb