model_presenter 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ ### Version 0.1.0
2
+
3
+ 1. Add ```moneyize``` helper
4
+ 1. Add ```it_moneyize``` rspec test helper
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # ModelPresenter
2
2
 
3
3
  [![Build Status](https://secure.travis-ci.org/ywen/model_presenter.png)](http://travis-ci.org/ywen/model_presenter)
4
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/ywen/model_presenter)
4
5
 
5
6
  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
 
@@ -80,6 +81,24 @@ user.to_json
80
81
 
81
82
  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.
82
83
 
84
+ ## Moneyize
85
+
86
+ The presenter comes with a small helper for format the money. The usage:
87
+
88
+ ```ruby
89
+ class User
90
+ include ModelPresenter::Base
91
+ forward_from_model :amount_remains
92
+ moneyize :amount_remains
93
+ end
94
+ ```
95
+
96
+ What ```moneyize``` does it to define a new method ```formatted_amount_remains``` and return a formatted dollar display string.
97
+
98
+ NOTE: the ```amount_remains``` is supposed to be the amount in cents
99
+
100
+ So assuming ```amount_remains``` is ```46780```. The ```formatted_amount_remains``` returns ```$ 467.80```
101
+
83
102
  ## Rspec Macros
84
103
 
85
104
  The gem provides some rspec macros for speeding up your test effort for your presenters. To use it, in your ```spec_helper.rb``` among your other setup:
@@ -99,6 +118,7 @@ And in a presenter test, you can do:
99
118
  describe User do
100
119
  forward_from_model_attributes :first_name, :last_name, :email
101
120
  as_json_attributes :first_name, :gender
121
+ it_moneyize :amount_remains, :some_other_money_field
102
122
  end
103
123
  ```
104
124
 
@@ -115,7 +135,7 @@ User
115
135
  returns the model.email
116
136
  ```
117
137
 
118
- and ```as_json_attributes``` macro will generate the following tests:
138
+ ```as_json_attributes``` macro will generate the following tests:
119
139
 
120
140
  ```rspec
121
141
 
@@ -124,6 +144,15 @@ and ```as_json_attributes``` macro will generate the following tests:
124
144
  has the key gender with value set to presenter.gender
125
145
  ```
126
146
 
147
+ ```it_moneyize``` macro will generate the following tests:
148
+
149
+ ```rspec
150
+ #formatted_amount_remains
151
+ returns the formatted money in dollar
152
+ #formatted_some_other_money_field
153
+ returns the formatted money in dollar
154
+ ```
155
+
127
156
  ## Contributing
128
157
 
129
158
  1. Fork it
@@ -1,6 +1,8 @@
1
1
  require 'json/ext'
2
2
  require 'model_presenter/forward_from_model'
3
3
  require 'model_presenter/as_json'
4
+ require 'model_presenter/money'
5
+ require 'model_presenter/moneyize'
4
6
  module ModelPresenter
5
7
  module Base
6
8
  attr_reader :model
@@ -8,6 +10,7 @@ module ModelPresenter
8
10
  def self.included(mod)
9
11
  mod.extend ForwardFromModel
10
12
  mod.extend AsJson
13
+ mod.extend Moneyize
11
14
  end
12
15
 
13
16
  def initialize(model)
@@ -0,0 +1,14 @@
1
+ module ModelPresenter
2
+ class Money
3
+ attr_reader :amount_in_cents
4
+
5
+ def initialize(amount_in_cents)
6
+ @amount_in_cents = amount_in_cents
7
+ end
8
+
9
+ def to_dollar
10
+ amount_in_dollar = amount_in_cents.to_f / 100
11
+ '%.2f' % amount_in_dollar
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ module ModelPresenter
2
+ module Moneyize
3
+ def moneyize(*fields)
4
+ fields.each do |field|
5
+ define_method "formatted_#{field}" do
6
+ "$ #{Money.new(send(field)).to_dollar}"
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module ModelPresenter
2
- VERSION = "0.0.6"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,5 +1,2 @@
1
1
  require "model_presenter/version"
2
2
  require 'model_presenter/base'
3
-
4
- module ModelPresenter
5
- end
@@ -0,0 +1,15 @@
1
+ module Macros
2
+ module System
3
+ def it_moneyize(*fields)
4
+ fields.each do |field|
5
+ describe "#formatted_#{field}" do
6
+ let(:expected) { subject.send("formatted_#{field}") }
7
+ let(:actual) { "$ #{subject.send(field).to_f/100}" }
8
+ it "returns the formatted money in dollar" do
9
+ expect(expected).to eq( actual )
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -16,12 +16,19 @@ module ModelPresenter
16
16
  klass.must_respond_to :forward_from_model
17
17
  end
18
18
  end
19
+
19
20
  describe ".json_properties" do
20
21
  it "responds to .json_properties" do
21
22
  klass.must_respond_to :json_properties
22
23
  end
23
24
  end
24
25
 
26
+ describe ".moneyize" do
27
+ it "responds to .moneyize" do
28
+ klass.must_respond_to :moneyize
29
+ end
30
+ end
31
+
25
32
  describe "#to_json" do
26
33
  before(:each) do
27
34
  subject.stubs(:as_json).returns(:attr1 => "value1", :attr2 => "value2")
@@ -0,0 +1,31 @@
1
+ require_relative "../test_helper"
2
+
3
+ module ModelPresenter
4
+ describe Money do
5
+ describe "#to_dollar" do
6
+ subject { Money.new 56789 }
7
+
8
+ context "cents: 56789" do
9
+ it "returns dollor amount" do
10
+ subject.to_dollar.must_equal("567.89")
11
+ end
12
+ end
13
+
14
+ context "cents: 56700" do
15
+ subject { Money.new 56700 }
16
+
17
+ it "returns dollor amount" do
18
+ subject.to_dollar.must_equal("567.00")
19
+ end
20
+ end
21
+
22
+ context "cents: 56760" do
23
+ subject { Money.new 56760 }
24
+
25
+ it "returns dollor amount" do
26
+ subject.to_dollar.must_equal("567.60")
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,24 @@
1
+ require_relative "../test_helper"
2
+
3
+ module ModelPresenter
4
+ describe Moneyize do
5
+ let(:presenter_class) do
6
+ Class.new do
7
+ extend Moneyize
8
+ moneyize :field1, :field2
9
+
10
+ def field1; 45678; end
11
+ def field2; 76590; end
12
+ end
13
+ end
14
+ subject { presenter_class.new }
15
+
16
+ describe ".moneyize" do
17
+ it "generates method to return the formmated dollar amount" do
18
+ subject.formatted_field1.must_equal("$ 456.78")
19
+ subject.formatted_field2.must_equal("$ 765.90")
20
+ end
21
+ end
22
+
23
+ end
24
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_presenter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-27 00:00:00.000000000 Z
12
+ date: 2012-08-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -151,6 +151,7 @@ files:
151
151
  - .rbenv-version
152
152
  - .rvmrc.template
153
153
  - .travis.yml
154
+ - CHANGELOG.md
154
155
  - Gemfile
155
156
  - Guardfile
156
157
  - LICENSE
@@ -160,16 +161,21 @@ files:
160
161
  - lib/model_presenter/as_json.rb
161
162
  - lib/model_presenter/base.rb
162
163
  - lib/model_presenter/forward_from_model.rb
164
+ - lib/model_presenter/money.rb
165
+ - lib/model_presenter/moneyize.rb
163
166
  - lib/model_presenter/spec_support.rb
164
167
  - lib/model_presenter/version.rb
165
168
  - lib/spec_support.rb
166
169
  - model_presenter.gemspec
167
170
  - spec/support/macros/as_json.rb
168
171
  - spec/support/macros/forward_from_model.rb
172
+ - spec/support/macros/it_moneyize.rb
169
173
  - tasks/test.rb
170
174
  - test/model_presenter/as_json_test.rb
171
175
  - test/model_presenter/base_test.rb
172
176
  - test/model_presenter/forward_from_model_test.rb
177
+ - test/model_presenter/money_test.rb
178
+ - test/model_presenter/moneyize_test.rb
173
179
  - test/support/mock_base_class.rb
174
180
  - test/support/mock_model_object.rb
175
181
  - test/test_helper.rb
@@ -200,9 +206,12 @@ summary: Provides an implementation of Presenter pattern
200
206
  test_files:
201
207
  - spec/support/macros/as_json.rb
202
208
  - spec/support/macros/forward_from_model.rb
209
+ - spec/support/macros/it_moneyize.rb
203
210
  - test/model_presenter/as_json_test.rb
204
211
  - test/model_presenter/base_test.rb
205
212
  - test/model_presenter/forward_from_model_test.rb
213
+ - test/model_presenter/money_test.rb
214
+ - test/model_presenter/moneyize_test.rb
206
215
  - test/support/mock_base_class.rb
207
216
  - test/support/mock_model_object.rb
208
217
  - test/test_helper.rb