model_presenter 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -70,6 +70,50 @@ user.to_json
70
70
 
71
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
72
 
73
+ ## Rspec Macros
74
+
75
+ 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:
76
+
77
+ ```ruby
78
+
79
+ require 'model_presenter/spec_support'
80
+ RSpec.configure do |config|
81
+ ModelPresenter::SpecSupport.new(config).register
82
+ end
83
+ ```
84
+
85
+ And in a presenter test, you can do:
86
+
87
+ ```ruby
88
+
89
+ describe User do
90
+ forward_from_model_attributes :first_name, :last_name, :email
91
+ as_json_attributes :first_name, :gender
92
+ end
93
+ ```
94
+
95
+ The ```forward_from_model_attributes``` macro will generate the following tests:
96
+
97
+ ```rspec
98
+
99
+ User
100
+ #first_name
101
+ returns the model.first_name
102
+ #last_name
103
+ returns the model.last_name
104
+ #email
105
+ returns the model.email
106
+ ```
107
+
108
+ and ```as_json_attributes``` macro will generate the following tests:
109
+
110
+ ```rspec
111
+
112
+ #as_json
113
+ has the key first_name with value set to presenter.first_name
114
+ has the key gender with value set to presenter.gender
115
+ ```
116
+
73
117
  ## Contributing
74
118
 
75
119
  1. Fork it
@@ -0,0 +1,15 @@
1
+ Dir["#{File.dirname(__FILE__)}/../../spec/support/**/*.rb"].each {|f| require f}
2
+
3
+ module ModelPresenter
4
+ class SpecSupport
5
+ attr_reader :config
6
+ private :config
7
+ def initialize(config)
8
+ @config = config
9
+ end
10
+
11
+ def register
12
+ config.extend ModelPresenter::Spec::Macros
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module ModelPresenter
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1 @@
1
+ Dir["#{File.dirname(__FILE__)}/../../support/**/*.rb"].each {|f| require f}
@@ -0,0 +1,18 @@
1
+ module ModelPresenter
2
+ module Spec
3
+ module Macros
4
+ def as_json_attributes(*keys)
5
+ describe "#as_json" do
6
+ let(:result) { subject.as_json }
7
+ keys.each do |key|
8
+ context "having the key #{key}"
9
+ it "has the key #{key} with value set to presenter.#{key}" do
10
+ result[key].should eq(subject.send(key))
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,15 @@
1
+ module ModelPresenter
2
+ module Spec
3
+ module Macros
4
+ def forward_from_model_attributes(*attributes)
5
+ attributes.each do |attribute|
6
+ describe "##{attribute}" do
7
+ it "returns the model.#{attribute}" do
8
+ subject.send(attribute).should eq(model.send(attribute))
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ 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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -175,8 +175,12 @@ files:
175
175
  - lib/model_presenter/as_json.rb
176
176
  - lib/model_presenter/base.rb
177
177
  - lib/model_presenter/forward_from_model.rb
178
+ - lib/model_presenter/spec_support.rb
178
179
  - lib/model_presenter/version.rb
180
+ - lib/spec_support.rb
179
181
  - model_presenter.gemspec
182
+ - spec/support/macros/as_json.rb
183
+ - spec/support/macros/forward_from_model.rb
180
184
  - tasks/test.rb
181
185
  - test/model_presenter/as_json_test.rb
182
186
  - test/model_presenter/base_test.rb
@@ -209,6 +213,8 @@ signing_key:
209
213
  specification_version: 3
210
214
  summary: Provides an implementation of Presenter pattern
211
215
  test_files:
216
+ - spec/support/macros/as_json.rb
217
+ - spec/support/macros/forward_from_model.rb
212
218
  - test/model_presenter/as_json_test.rb
213
219
  - test/model_presenter/base_test.rb
214
220
  - test/model_presenter/forward_from_model_test.rb