model_presenter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
data/.rvmrc.template ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.3@model_presenter
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ script: "bundle exec rake test"
3
+ rvm:
4
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in model_presenter.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,14 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'minitest' do
5
+ # with Minitest::Unit
6
+ watch(%r|^test/(.*)\/?(.*)_test\.rb|)
7
+ watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| [ "lib/#{m[1]}#{m[2]}.rb", "test/#{m[1]}#{m[2]}_test.rb" ] }
8
+ watch(%r|^test/test_helper\.rb|) { "test" }
9
+
10
+ # with Minitest::Spec
11
+ # watch(%r|^spec/(.*)_spec\.rb|)
12
+ # watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
13
+ # watch(%r|^spec/spec_helper\.rb|) { "spec" }
14
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Yi Wen
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.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # ModelPresenter
2
+
3
+ [![Build Status](https://secure.travis-ci.org/ywen/model_presenter.png)](http://travis-ci.org/ywen/model_presenter)
4
+
5
+ ModelPresenter provides basic framework in creating a presenter. The goal of the gem is that the presenter can be used in any Ruby projects that feel it needs to use this pattern, not limited to Rails projects.
6
+
7
+ The presenter puts more emphasis on JSON representation of a business model object. But it can be used in classic HTML view just as easy.
8
+
9
+ The gem is not trying to provide a automagic way to look cool. Rather, it focuses on explicitly expressing the intent of a Presenter class. Please read the usage for the details
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'model_presenter'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install model_presenter
24
+
25
+ ## Usage
26
+
27
+ An example presenter that uses the ```ModelPresenter::Base```
28
+
29
+ ```ruby
30
+
31
+ class User
32
+ include ModelPresenter::Base
33
+ forward_from_model :last_name, :first_name, :email
34
+ json_properties :name, :gender, :email
35
+
36
+ def name
37
+ "#{first_name} #{last_name}"
38
+ end
39
+
40
+ def gender
41
+ model.gender == "M" ? "Male" : "Female"
42
+ end
43
+ end
44
+ ```
45
+
46
+ The ```forward_from_model``` defines methods ```attr1``` and ```attr2``` which calls the ```model.attr1``` ```model.attr2``` respectively.
47
+
48
+ The ```json_properties``` defines methods ```as_json``` which returns a hash in which the keys are the properties being passed in the ```json_properties``` and the value of a given property the ```presenter_object.property```
49
+
50
+ An example output of ```as_json``` returns:
51
+
52
+ ```json
53
+
54
+ {
55
+ name: "John Smith",
56
+ gender: "Male",
57
+ email: "jsmith@example.com"
58
+ }
59
+ ```
60
+
61
+ Then one can use ```presenter_object.to_json``` to serilize it into a JSON string.
62
+
63
+ A presenter instance can be initialized with
64
+
65
+ ```ruby
66
+
67
+ user = User.new(user_model)
68
+ user.to_json
69
+ ```
70
+
71
+ It always takes a model object as the only argument in the initializer. The model object is referred from within the presenter as ```presenter.model```. It is a private attribute reader.
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require File.expand_path(File.dirname(__FILE__))+'/tasks/test'
@@ -0,0 +1,5 @@
1
+ require "model_presenter/version"
2
+ require 'model_presenter/base'
3
+
4
+ module ModelPresenter
5
+ end
@@ -0,0 +1,11 @@
1
+ module ModelPresenter
2
+ module AsJson
3
+ def json_properties(*properties)
4
+ self.send :define_method, :as_json do |options = {}|
5
+ properties.inject({}) do |results, property|
6
+ results.merge! property => send(property)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ require 'json/ext'
2
+ require 'model_presenter/forward_from_model'
3
+ require 'model_presenter/as_json'
4
+ module ModelPresenter
5
+ module Base
6
+ attr_reader :model
7
+ private :model
8
+ def self.included(mod)
9
+ mod.extend ForwardFromModel
10
+ mod.extend AsJson
11
+ end
12
+
13
+ def initialize(model)
14
+ @model = model
15
+ end
16
+
17
+ def to_json(options={})
18
+ JSON(as_json(options))
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ module ModelPresenter
2
+ module ForwardFromModel
3
+ def forward_from_model(*attributes)
4
+ attributes.each do |attr|
5
+ self.send :define_method, attr do
6
+ model.send(attr)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module ModelPresenter
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/model_presenter/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Yi Wen"]
6
+ gem.email = ["hayafirst@gmail.com"]
7
+ gem.description = %q{Provides an implementation of Presenter pattern, but without needs fo Rails}
8
+ gem.summary = %q{Provides an implementation of 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 = "model_presenter"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ModelPresenter::VERSION
17
+ gem.add_runtime_dependency(%q<json>)
18
+ gem.add_development_dependency(%q<minitest>)
19
+ gem.add_development_dependency(%q<minitest-growl>)
20
+ gem.add_development_dependency(%q<guard-minitest>)
21
+ gem.add_development_dependency(%q<minitest-wscolor>)
22
+ gem.add_development_dependency(%q<cane>)
23
+ gem.add_development_dependency(%q<rake>)
24
+ gem.add_development_dependency(%q<mocha>)
25
+ gem.add_development_dependency(%q<minitest-spec-context>)
26
+ end
data/tasks/test.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.pattern = "test/**/*_test.rb"
5
+ end
@@ -0,0 +1,33 @@
1
+ require_relative "../test_helper"
2
+
3
+ module ModelPresenter
4
+ describe AsJson do
5
+ let(:klass) {
6
+ ModelPresenter::TestHelpers.mock_base_class.tap do |c|
7
+ c.send :extend, AsJson
8
+ c.send :json_properties, :attr1, :attr2
9
+ c.send :define_method, :attr1 do
10
+ "value1"
11
+ end
12
+ c.send :define_method, :attr2 do
13
+ "value2"
14
+ end
15
+ end
16
+ }
17
+ let(:object) { Object.new }
18
+ subject {klass.new object}
19
+ describe ".json_properties" do
20
+ it "generates as_json method" do
21
+ subject.must_respond_to :as_json
22
+ end
23
+ end
24
+ describe "#as_json" do
25
+ let(:result) { subject.as_json }
26
+ { :attr1 => "value1", :attr2 => "value2" }.each do |key, value|
27
+ it "generates a JSON contains the key ''#{key}' with the value '#{value}' " do
28
+ result[key].must_equal value
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,35 @@
1
+ require_relative '../test_helper'
2
+
3
+ module ModelPresenter
4
+ describe Base do
5
+ let(:klass) {
6
+ Class.new do
7
+ include Base
8
+ json_properties :attr1, :attr2
9
+ end
10
+ }
11
+
12
+ let(:object) { ModelPresenter::TestHelpers.mock_model_object }
13
+ subject { klass.new object }
14
+ describe ".forward_from_model" do
15
+ it "responds to .forward_from_model" do
16
+ klass.must_respond_to :forward_from_model
17
+ end
18
+ end
19
+ describe ".json_properties" do
20
+ it "responds to .json_properties" do
21
+ klass.must_respond_to :json_properties
22
+ end
23
+ end
24
+
25
+ describe "#to_json" do
26
+ before(:each) do
27
+ subject.stubs(:as_json).returns(:attr1 => "value1", :attr2 => "value2")
28
+ end
29
+
30
+ it "convert the as_json hash to JSON string" do
31
+ subject.to_json.must_equal JSON(:attr1 => "value1", :attr2 => "value2")
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,33 @@
1
+ require_relative '../test_helper'
2
+
3
+ module ModelPresenter
4
+ describe ForwardFromModel do
5
+ let(:klass) {
6
+ ModelPresenter::TestHelpers.mock_base_class.tap do |c|
7
+ c.send :extend, ForwardFromModel
8
+ c.send :forward_from_model, :attr1, :attr2
9
+ end
10
+ }
11
+ let(:object) { ModelPresenter::TestHelpers.mock_model_object }
12
+
13
+ subject { klass.new object }
14
+
15
+ describe ".forward_from_model" do
16
+ { :attr1 => "value1", :attr2 => "value2" }.each do |key, value|
17
+ it "generates the method #{key} and returns #{value}" do
18
+ subject.send(key).must_equal(value)
19
+ end
20
+ end
21
+
22
+ context "when passing in a method that doesn't exist in the model object" do
23
+ before(:each) do
24
+ klass.send :forward_from_model, :attr3
25
+ end
26
+
27
+ it "raises an exception" do
28
+ lambda{subject.attr3}.must_raise(NoMethodError)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,13 @@
1
+ module ModelPresenter
2
+ module TestHelpers
3
+ def self.mock_base_class
4
+ Class.new do
5
+ attr_reader :model
6
+ private :model
7
+ def initialize(model)
8
+ @model = model
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module ModelPresenter
2
+ module TestHelpers
3
+ def self.mock_model_object
4
+ Class.new do
5
+ def attr1
6
+ "value1"
7
+ end
8
+ def attr2
9
+ "value2"
10
+ end
11
+ end.new
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ gem 'minitest'
3
+ require 'minitest/autorun'
4
+ require 'minitest/wscolor'
5
+ require 'minitest-spec-context'
6
+ require 'mocha'
7
+ require 'model_presenter'
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
metadata ADDED
@@ -0,0 +1,217 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: model_presenter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Yi Wen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
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: minitest
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
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: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: minitest-growl
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '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: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: guard-minitest
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
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: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: minitest-wscolor
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: cane
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: rake
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
+ - !ruby/object:Gem::Dependency
127
+ name: mocha
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: minitest-spec-context
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ description: Provides an implementation of Presenter pattern, but without needs fo
159
+ Rails
160
+ email:
161
+ - hayafirst@gmail.com
162
+ executables: []
163
+ extensions: []
164
+ extra_rdoc_files: []
165
+ files:
166
+ - .gitignore
167
+ - .rvmrc.template
168
+ - .travis.yml
169
+ - Gemfile
170
+ - Guardfile
171
+ - LICENSE
172
+ - README.md
173
+ - Rakefile
174
+ - lib/model_presenter.rb
175
+ - lib/model_presenter/as_json.rb
176
+ - lib/model_presenter/base.rb
177
+ - lib/model_presenter/forward_from_model.rb
178
+ - lib/model_presenter/version.rb
179
+ - model_presenter.gemspec
180
+ - tasks/test.rb
181
+ - test/model_presenter/as_json_test.rb
182
+ - test/model_presenter/base_test.rb
183
+ - test/model_presenter/forward_from_model_test.rb
184
+ - test/support/mock_base_class.rb
185
+ - test/support/mock_model_object.rb
186
+ - test/test_helper.rb
187
+ homepage: ''
188
+ licenses: []
189
+ post_install_message:
190
+ rdoc_options: []
191
+ require_paths:
192
+ - lib
193
+ required_ruby_version: !ruby/object:Gem::Requirement
194
+ none: false
195
+ requirements:
196
+ - - ! '>='
197
+ - !ruby/object:Gem::Version
198
+ version: '0'
199
+ required_rubygems_version: !ruby/object:Gem::Requirement
200
+ none: false
201
+ requirements:
202
+ - - ! '>='
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ requirements: []
206
+ rubyforge_project:
207
+ rubygems_version: 1.8.21
208
+ signing_key:
209
+ specification_version: 3
210
+ summary: Provides an implementation of Presenter pattern
211
+ test_files:
212
+ - test/model_presenter/as_json_test.rb
213
+ - test/model_presenter/base_test.rb
214
+ - test/model_presenter/forward_from_model_test.rb
215
+ - test/support/mock_base_class.rb
216
+ - test/support/mock_model_object.rb
217
+ - test/test_helper.rb