modest_presenter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "minitest", ">= 0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.1.0"
12
+ gem "jeweler", "~> 1.8.3"
13
+ gem "simplecov"
14
+ gem "guard"
15
+ gem "guard-minitest"
16
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,38 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ ffi (1.0.11)
5
+ git (1.2.5)
6
+ guard (1.0.2)
7
+ ffi (>= 0.5.0)
8
+ thor (~> 0.14.6)
9
+ guard-minitest (0.5.0)
10
+ guard (>= 0.4)
11
+ jeweler (1.8.3)
12
+ bundler (~> 1.0)
13
+ git (>= 1.2.5)
14
+ rake
15
+ rdoc
16
+ json (1.7.0)
17
+ minitest (2.12.1)
18
+ multi_json (1.3.4)
19
+ rake (0.9.2.2)
20
+ rdoc (3.12)
21
+ json (~> 1.4)
22
+ simplecov (0.6.2)
23
+ multi_json (~> 1.3)
24
+ simplecov-html (~> 0.5.3)
25
+ simplecov-html (0.5.3)
26
+ thor (0.14.6)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ bundler (~> 1.1.0)
33
+ guard
34
+ guard-minitest
35
+ jeweler (~> 1.8.3)
36
+ minitest
37
+ rdoc (~> 3.12)
38
+ simplecov
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'minitest' do
5
+ # with Minitest::Spec
6
+ watch(%r|^spec/(.*)_spec\.rb|)
7
+ watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
8
+ watch(%r|^spec/spec_helper\.rb|) { "spec" }
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Evan Light
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,99 @@
1
+ = A modest presenter
2
+
3
+ modest_presenter is a fairly simple base class for objects that you want to act as a Presenter for an arbitrary templating language. It should be fairly templating language agnostic--though the supplied example works in ERB.
4
+
5
+ I extracted this from a couple of projects where I've found myself writing this very same thing.
6
+
7
+ == Example
8
+
9
+ Person = Struct.new(:first_name, :last_name, :age)
10
+ InsanePerson = Class.new(Person)
11
+ Nerd = Class.new(Person)
12
+
13
+ evan = Nerd.new("Evan", "Light", 38)
14
+ ed = Nerd.new("Ed", "Kim", 27)
15
+ # Seems a bit generous calling this guy a Person
16
+ w = InsanePerson.new("George W.", "Bush", 66)
17
+
18
+ class PersonPresenter < ModestPresenters::ModestPresenter
19
+ def name
20
+ "#{first_name} #{last_name}"
21
+ end
22
+
23
+ def title
24
+ end
25
+ end
26
+
27
+ evan = Nerd.new("Evan", "Light", 38)
28
+ ed = Nerd.new("Ed", "Kim", 27)
29
+
30
+ # Seems a bit generous calling this guy a Person
31
+ w = InsanePerson.new("George W.", "Bush", 66)
32
+
33
+ class PersonPresenter < ModestPresenters::ModestPresenter
34
+ def name
35
+ "#{first_name} #{last_name}"
36
+ end
37
+
38
+ def title
39
+ end
40
+ end
41
+
42
+ class NerdPresenter < PersonPresenter
43
+ def name
44
+ "#{super} is a nerd."
45
+ end
46
+ end
47
+
48
+ class InsanePersonPresenter < PersonPresenter
49
+ def name
50
+ "#{super} wastes oxygen better used by nerds."
51
+ end
52
+
53
+ def title
54
+ "President "
55
+ end
56
+ end
57
+
58
+ def present(model, context, &block)
59
+ Object.const_get("#{model.class}Presenter")
60
+ .new(model, context)
61
+ .present(&block)
62
+ end
63
+
64
+ people = [evan, ed, w]
65
+
66
+ ERB_TEMPLATE = <<-ERB
67
+ <ul>
68
+ <% people.each do |person| %>
69
+ <% present(person, self) do %>
70
+ <li>
71
+ <%= title %><%= name %>
72
+ </li>
73
+ <% end %>
74
+ <% end %>
75
+ </ul>
76
+ <p>Yes, only insane people are President</p>
77
+ ERB
78
+
79
+ require 'erb'
80
+ puts ERB.new(ERB_TEMPLATE).result(binding)
81
+
82
+ # OUTPUTS:
83
+ # <ul>
84
+ # <li>
85
+ # Evan Light is a nerd.
86
+ # </li>
87
+ # <li>
88
+ # Ed Kim is a nerd.
89
+ # </li>
90
+ # <li>
91
+ # President George W. Bush wastes oxygen better used by nerds.
92
+ # </li>
93
+ # </ul>
94
+ # <p>Yes, only insane people are President</p>
95
+
96
+ == Copyright
97
+
98
+ Copyright (c) 2012 Evan Light. MIT Licensed.
99
+
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "modest_presenter"
18
+ gem.homepage = "http://github.com/elight/modest_presenter"
19
+ gem.license = "MIT"
20
+ gem.summary = "A simple Presenter for use with arbitrary templating languages"
21
+ gem.description = "A simple Presenter for use with arbitrary templating languages"
22
+ gem.email = "evan.light@tripledogdare.net"
23
+ gem.authors = ["Evan Light"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'spec'
31
+ test.pattern = 'spec/**/*_spec.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rdoc/task'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "modest_presenter #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,76 @@
1
+ $LOAD_PATH << "../lib"
2
+
3
+ require 'modest_presenter'
4
+
5
+ Person = Struct.new(:first_name, :last_name, :age)
6
+ InsanePerson = Class.new(Person)
7
+ Nerd = Class.new(Person)
8
+
9
+ evan = Nerd.new("Evan", "Light", 38)
10
+ ed = Nerd.new("Ed", "Kim", 27)
11
+
12
+ # Seems a bit generous calling this guy a Person
13
+ w = InsanePerson.new("George W.", "Bush", 66)
14
+
15
+ class PersonPresenter < ModestPresenters::ModestPresenter
16
+ def name
17
+ "#{first_name} #{last_name}"
18
+ end
19
+
20
+ def title
21
+ end
22
+ end
23
+
24
+ class NerdPresenter < PersonPresenter
25
+ def name
26
+ "#{super} is a nerd."
27
+ end
28
+ end
29
+
30
+ class InsanePersonPresenter < PersonPresenter
31
+ def name
32
+ "#{super} wastes oxygen better used by nerds."
33
+ end
34
+
35
+ def title
36
+ "President "
37
+ end
38
+ end
39
+
40
+ def present(model, context, &block)
41
+ Object.const_get("#{model.class}Presenter")
42
+ .new(model, context)
43
+ .present(&block)
44
+ end
45
+
46
+ people = [evan, ed, w]
47
+
48
+ ERB_TEMPLATE = <<-ERB
49
+ <ul>
50
+ <% people.each do |person| %>
51
+ <% present(person, self) do %>
52
+ <li>
53
+ <%= title %><%= name %>
54
+ </li>
55
+ <% end %>
56
+ <% end %>
57
+ </ul>
58
+ <p>Yes, only insane people are President</p>
59
+ ERB
60
+
61
+ require 'erb'
62
+ puts ERB.new(ERB_TEMPLATE).result(binding)
63
+
64
+ # OUTPUTS:
65
+ # <ul>
66
+ # <li>
67
+ # Evan Light is a nerd.
68
+ # </li>
69
+ # <li>
70
+ # Ed Kim is a nerd.
71
+ # </li>
72
+ # <li>
73
+ # President George W. Bush wastes oxygen better used by nerds.
74
+ # </li>
75
+ # </ul>
76
+ # <p>Yes, only insane people are President</p>
@@ -0,0 +1,23 @@
1
+ module ModestPresenters
2
+ class ModestPresenter
3
+ attr_reader :model, :context
4
+
5
+ def initialize(model, context, args = {})
6
+ @model, @context = model, context
7
+ end
8
+
9
+ def method_missing(*args, &block)
10
+ target =
11
+ if model.respond_to?(args.first)
12
+ model
13
+ else
14
+ context
15
+ end
16
+ target.send(*args, &block)
17
+ end
18
+
19
+ def present(&block)
20
+ instance_eval(&block)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,58 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "modest_presenter"
3
+ s.version = "0.1.0"
4
+
5
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
+ s.authors = ["Evan Light"]
7
+ s.date = "2012-05-03"
8
+ s.description = "A simple Presenter for use with arbitrary templating languages"
9
+ s.email = "sleight42@gmail.com"
10
+ s.extra_rdoc_files = [
11
+ "LICENSE.txt",
12
+ "README.rdoc"
13
+ ]
14
+ s.files = [
15
+ "LICENSE.txt",
16
+ "README.rdoc",
17
+ "Rakefile",
18
+ "VERSION",
19
+ "examples/rendering_presenter.rb",
20
+ "lib/modest_presenter.rb"
21
+ ]
22
+ s.homepage = "http://github.com/elight/modest_presenter"
23
+ s.licenses = ["MIT"]
24
+ s.require_paths = ["lib"]
25
+ s.rubygems_version = "1.8.21"
26
+ s.summary = "A simple Presenter for use with arbitrary templating languages"
27
+
28
+ if s.respond_to? :specification_version then
29
+ s.specification_version = 3
30
+
31
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
32
+ s.add_development_dependency(%q<minitest>, [">= 0"])
33
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
34
+ s.add_development_dependency(%q<bundler>, ["~> 1.1.0"])
35
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
36
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
37
+ s.add_development_dependency(%q<guard>, [">= 0"])
38
+ s.add_development_dependency(%q<guard-minitest>, [">= 0"])
39
+ else
40
+ s.add_dependency(%q<minitest>, [">= 0"])
41
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
42
+ s.add_dependency(%q<bundler>, ["~> 1.1.0"])
43
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
44
+ s.add_dependency(%q<simplecov>, [">= 0"])
45
+ s.add_dependency(%q<guard>, [">= 0"])
46
+ s.add_dependency(%q<guard-minitest>, [">= 0"])
47
+ end
48
+ else
49
+ s.add_dependency(%q<minitest>, [">= 0"])
50
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
51
+ s.add_dependency(%q<bundler>, ["~> 1.1.0"])
52
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
53
+ s.add_dependency(%q<simplecov>, [">= 0"])
54
+ s.add_dependency(%q<guard>, [">= 0"])
55
+ s.add_dependency(%q<guard-minitest>, [">= 0"])
56
+ end
57
+ end
58
+
data/spec/helper.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+
11
+ require 'simplecov'
12
+ SimpleCov.start
13
+
14
+ require 'minitest/spec'
15
+
16
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
17
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
18
+ require 'modest_presenter'
19
+
20
+ require 'minitest/autorun'
@@ -0,0 +1,64 @@
1
+ require 'helper'
2
+
3
+ class ModestPresenterSpec < MiniTest::Spec
4
+ include ModestPresenters
5
+
6
+ class Person < Struct.new(:first_name, :last_name)
7
+ end
8
+
9
+ class Student < Person
10
+ attr_accessor :enrolled_courses
11
+ end
12
+
13
+ class Teacher < Person
14
+ attr_accessor :department, :professorship, :teaching_courses
15
+ end
16
+
17
+ class PersonPresenter < ModestPresenter
18
+ def full_name
19
+ "#{first_name} #{last_name}"
20
+ end
21
+ end
22
+
23
+ class StudentPresenter < PersonPresenter
24
+ end
25
+
26
+ class TeacherPresenter < PersonPresenter
27
+ end
28
+
29
+ it "expects a model and a context" do
30
+ student = Student.new("Ed", "Kim")
31
+ StudentPresenter.new(student, Object.new)
32
+ end
33
+
34
+ it "accepts a Hash as a third argument" do
35
+ student = Student.new("Ed", "Kim")
36
+ StudentPresenter.new(student, Object.new, :is_learning_ruby => true)
37
+ end
38
+
39
+ describe "A modest presenter" do
40
+ before do
41
+ @student = Student.new("Ed", "Kim")
42
+ @context = Struct.new(:render).new("you called render")
43
+ @presenter = StudentPresenter.new(@student, @context)
44
+ end
45
+
46
+ it "first delegates calls to its model" do
47
+ @student.first_name.must_equal @presenter.first_name
48
+ end
49
+
50
+ it "delegates messages unsupported by its model to its context" do
51
+ @context.render.must_equal @presenter.render
52
+ end
53
+
54
+ describe "when presenting" do
55
+ it "evaluates a provided block in the context of the presenter" do
56
+ @presenter.present do
57
+ self.must_be_instance_of StudentPresenter
58
+ @model = "Ohai the block was called"
59
+ end
60
+ @presenter.model.must_equal "Ohai the block was called"
61
+ end
62
+ end
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: modest_presenter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Evan Light
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '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: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rdoc
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.12'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.12'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.1.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.1.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.3
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.3
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: guard
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: guard-minitest
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: A simple Presenter for use with arbitrary templating languages
127
+ email: evan.light@tripledogdare.net
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files:
131
+ - LICENSE.txt
132
+ - README.rdoc
133
+ files:
134
+ - Gemfile
135
+ - Gemfile.lock
136
+ - Guardfile
137
+ - LICENSE.txt
138
+ - README.rdoc
139
+ - Rakefile
140
+ - VERSION
141
+ - examples/rendering_presenter.rb
142
+ - lib/modest_presenter.rb
143
+ - modest_presenter.gemspec
144
+ - spec/helper.rb
145
+ - spec/modest_presenter_spec.rb
146
+ homepage: http://github.com/elight/modest_presenter
147
+ licenses:
148
+ - MIT
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ! '>='
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ segments:
160
+ - 0
161
+ hash: -2766740440239994718
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ none: false
164
+ requirements:
165
+ - - ! '>='
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ requirements: []
169
+ rubyforge_project:
170
+ rubygems_version: 1.8.24
171
+ signing_key:
172
+ specification_version: 3
173
+ summary: A simple Presenter for use with arbitrary templating languages
174
+ test_files: []