presentability 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c0b10ef3f8fba6959cfd51db2efee95b4df669863b1ef433656da03573d61f07
4
- data.tar.gz: beefa821c8f69d2038a992d4064b08429202100a6a9b139bc4427247f31f47c1
3
+ metadata.gz: f5b0aae4df8ff0ef130bbcc9704f2f44a9b109dee48cc530db6abdfd0733cbd4
4
+ data.tar.gz: d149966c13d352f89c2f437c8ed251d63520ec33287c920e8ff7c1cd43eeb0e4
5
5
  SHA512:
6
- metadata.gz: 5b2974973651bc54624dbca03bd89d841dd4aab00dcdc72f38ada1a40bb626b7096d78c2fd917b31c45af99d5771d0d935ce751da473ec8ef93e9dff2b01876d
7
- data.tar.gz: 874444891db074bd26600f61bdacea937b75553616e541c6b50f0c69f0e888c2c5f437895da5d4bb5df5ca0dec849f73848f27230076e8883aec1ef30378393b
6
+ metadata.gz: 906172259081cb4c871975c6e8096bfcd7d8a2434903cc8bef9562b9b18c11af889ef1733372d46c94420008baf5821499d19e6708c75224abd962febabc0515
7
+ data.tar.gz: 5d2c5691ee59ba2526755a3a2eab49547d093fa1d2fbedad4163a78ea35b8acc4dc74d20f0e16a90d7b9af3cbedd9a043c68f3b402f8197ea40031984fc4773b
checksums.yaml.gz.sig CHANGED
Binary file
data/History.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  ---
4
4
 
5
+ ## v0.2.0 [2022-12-06] Michael Granger <ged@faeriemud.org>
6
+
7
+ Improvements:
8
+
9
+ - Add collection presentation.
10
+
11
+
5
12
  ## v0.1.0 [2022-08-11] Michael Granger <ged@faeriemud.org>
6
13
 
7
14
  Initial release.
@@ -54,6 +54,13 @@ require 'loggability'
54
54
  # (the "subject"), and then applying it:
55
55
  #
56
56
  # ```ruby
57
+ # acme_widget = Acme::Widget.new(
58
+ # sku: "FF-2237H455",
59
+ # name: "Throbbing Frobnulator",
60
+ # unit_price: 299,
61
+ # inventory_count: 301,
62
+ # wholesale_cost: 39
63
+ # )
57
64
  # presenter = Acme::Presenters.present( acme_widget )
58
65
  # presenter.apply
59
66
  # # => { :sku => "FF-2237H455", :name => "Throbbing Frobnulator", :unit_price => 299 }
@@ -65,7 +72,7 @@ module Presentability
65
72
 
66
73
 
67
74
  # Package version
68
- VERSION = '0.1.0'
75
+ VERSION = '0.2.0'
69
76
 
70
77
 
71
78
  # Automatically load subordinate components
@@ -104,6 +111,13 @@ module Presentability
104
111
  end
105
112
 
106
113
 
114
+ ### Return an Array of all representations of the members of the
115
+ ### +collection+ by applying a declared presentation.
116
+ def present_collection( collection, **options )
117
+ return collection.map {|object| self.present(object, **options) }
118
+ end
119
+
120
+
107
121
  #########
108
122
  protected
109
123
  #########
@@ -3,6 +3,7 @@
3
3
 
4
4
  require_relative 'spec_helper'
5
5
 
6
+ require 'faker'
6
7
  require 'rspec'
7
8
  require 'presentability'
8
9
 
@@ -15,17 +16,35 @@ RSpec.describe Presentability do
15
16
  return 'Acme::Entity'
16
17
  end
17
18
 
18
- def initialize
19
- @foo = 1
20
- @bar = 'two'
21
- @baz = :three
19
+ def initialize( foo: 1, bar: 'two', baz: :three )
20
+ @foo = foo
21
+ @bar = bar
22
+ @baz = baz
22
23
  end
23
24
 
24
25
  attr_accessor :foo, :bar, :baz
25
26
  end
26
27
  end
27
28
 
29
+ let( :other_entity_class ) do
30
+ Class.new do
31
+ def self::name
32
+ return 'Acme::User'
33
+ end
34
+
35
+ def initialize( firstname, lastname, email, password )
36
+ @firstname = firstname
37
+ @lastname = lastname
38
+ @email = email
39
+ @password = password
40
+ end
41
+
42
+ attr_accessor :firstname, :lastname, :email, :password
43
+ end
44
+ end
45
+
28
46
  let( :entity_instance ) { entity_class.new }
47
+ let( :other_entity_instance ) { entity_class.new }
29
48
 
30
49
 
31
50
  describe "an extended module" do
@@ -103,6 +122,87 @@ RSpec.describe Presentability do
103
122
  }.to raise_error( NoMethodError, /can't expose :id -- no such attribute exists/i )
104
123
  end
105
124
 
125
+
126
+ describe "collection handling" do
127
+
128
+ it "can present a collection" do
129
+ extended_module.presenter_for( entity_class ) do
130
+ expose :foo
131
+ expose :bar
132
+ end
133
+ collection = 5.times.map do
134
+ entity_class.new( foo: rand(100) )
135
+ end
136
+
137
+ results = extended_module.present_collection( collection )
138
+
139
+ expect( results ).to have_attributes( length: 5 )
140
+ results.each do |representation|
141
+ expect( representation ).to include( :foo, :bar )
142
+ expect( representation ).to_not include( :baz )
143
+ end
144
+ end
145
+
146
+
147
+ it "can present a mixed collection" do
148
+ extended_module.presenter_for( entity_class ) do
149
+ expose :foo
150
+ expose :bar
151
+ end
152
+ extended_module.presenter_for( other_entity_class ) do
153
+ expose :firstname
154
+ expose :lastname
155
+ expose :email
156
+ end
157
+
158
+ collection = 5.times.flat_map do
159
+ [
160
+ entity_class.new( foo: rand(100) ),
161
+ other_entity_class.new(
162
+ Faker::Name.first_name,
163
+ Faker::Name.last_name,
164
+ Faker::Internet.email,
165
+ Faker::Internet.password
166
+ )
167
+ ]
168
+ end
169
+
170
+ results = extended_module.present_collection( collection )
171
+
172
+ expect( results ).to have_attributes( length: 10 )
173
+ results.each do |representation|
174
+ expect( representation ).to include( :foo, :bar ).
175
+ or( include(:firstname, :lastname, :email) )
176
+ expect( representation ).to_not include( :baz )
177
+ expect( representation ).to_not include( :password )
178
+ end
179
+ end
180
+
181
+
182
+ it "passes options to the individual presenters" do
183
+ extended_module.presenter_for( entity_class ) do
184
+ expose :foo
185
+ expose :bar, if: :include_bar
186
+ end
187
+ collection = 5.times.map do
188
+ entity_class.new( foo: rand(100) )
189
+ end
190
+
191
+ results = extended_module.present_collection( collection )
192
+ results.each do |representation|
193
+ expect( representation ).to include( :foo )
194
+ expect( representation ).to_not include( :bar, :baz )
195
+ end
196
+
197
+ results = extended_module.present_collection( collection, include_bar: true )
198
+ results.each do |representation|
199
+ expect( representation ).to include( :foo, :bar )
200
+ expect( representation ).to_not include( :baz )
201
+ end
202
+ end
203
+
204
+ end
205
+
106
206
  end
107
207
 
108
208
  end
data/spec/spec_helper.rb CHANGED
@@ -4,6 +4,11 @@
4
4
  require 'simplecov' if ENV['COVERAGE']
5
5
 
6
6
  require 'rspec'
7
+ require 'i18n'
8
+ require 'faker'
9
+
10
+ Faker::Config.locale = 'en'
11
+ I18n.reload!
7
12
 
8
13
  require 'loggability/spechelpers'
9
14
 
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: presentability
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Granger
@@ -33,7 +33,7 @@ cert_chain:
33
33
  P5YP5BAbNW+gvd3QHRiWTTuhgHrdDnGdXg93N2M5KHn1ug8BtPLQwlcFwEpKnlLn
34
34
  btEP+7EplFuoiMfd
35
35
  -----END CERTIFICATE-----
36
- date: 2022-08-11 00:00:00.000000000 Z
36
+ date: 2022-12-06 00:00:00.000000000 Z
37
37
  dependencies:
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: loggability
@@ -49,6 +49,20 @@ dependencies:
49
49
  - - "~>"
50
50
  - !ruby/object:Gem::Version
51
51
  version: '0.18'
52
+ - !ruby/object:Gem::Dependency
53
+ name: faker
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - "~>"
57
+ - !ruby/object:Gem::Version
58
+ version: '3.0'
59
+ type: :development
60
+ prerelease: false
61
+ version_requirements: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - "~>"
64
+ - !ruby/object:Gem::Version
65
+ version: '3.0'
52
66
  - !ruby/object:Gem::Dependency
53
67
  name: rake-deveiate
54
68
  requirement: !ruby/object:Gem::Requirement
metadata.gz.sig CHANGED
Binary file