tarvit-helpers 0.0.15 → 0.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +53 -0
- data/VERSION +1 -1
- data/lib/tarvit-helpers/modules/hash_presenter.rb +28 -13
- data/spec/modules/hash_presenter_spec.rb +53 -2
- data/tarvit-helpers.gemspec +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3444b7e3aa260a574ae016d797eaca0b76cc3a3a
|
4
|
+
data.tar.gz: 6f4bf42a2f01ea7fcc2748f8429d799d2498eacf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3bde109522e483e5c094f6290ffce1b57ed882ad7073c0db90875b54312e6b792dabe285d155f69e66ede6b461465386db6199de4111ce872f78f48efb1a5c5e
|
7
|
+
data.tar.gz: b1c0c435db896eaf62dd96b7208bdbe8c3b31b9908a94c6f258d64136821064418fc6fc333e79d71688768c3d989437456f52e182be10764bab531b52c9c0b6b
|
data/README.md
CHANGED
@@ -102,6 +102,59 @@ presenter.user.age
|
|
102
102
|
presenter.user.posts[0].title
|
103
103
|
=> "Some title"
|
104
104
|
|
105
|
+
|
106
|
+
@hash = {
|
107
|
+
accounts: [
|
108
|
+
{
|
109
|
+
:id => 1,
|
110
|
+
:name => :director,
|
111
|
+
collections: [
|
112
|
+
{
|
113
|
+
:id => 42,
|
114
|
+
:name => :test_collection,
|
115
|
+
}
|
116
|
+
]
|
117
|
+
}
|
118
|
+
]
|
119
|
+
}
|
120
|
+
|
121
|
+
class AccountsPresenter < HashPresenter::CustomHashPresenter
|
122
|
+
|
123
|
+
def _init_rules
|
124
|
+
rules = _rules
|
125
|
+
|
126
|
+
rules.when([:accounts, :name]) do |value|
|
127
|
+
value.to_s
|
128
|
+
end
|
129
|
+
|
130
|
+
rules.when([:accounts, :website]) do |value, object|
|
131
|
+
'www.johndoe.com/' + object.name.to_s
|
132
|
+
end
|
133
|
+
|
134
|
+
rules.when([:accounts, :collections, :name]) do |value|
|
135
|
+
value.to_s.camelize
|
136
|
+
end
|
137
|
+
|
138
|
+
rules.when([:accounts, :collections, :folder]) do |value, object|
|
139
|
+
"folders/#{object.name}"
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
@presenter = AccountsPresenter.new(@hash)
|
145
|
+
|
146
|
+
account = @presenter.accounts.first
|
147
|
+
account.id
|
148
|
+
=> 1
|
149
|
+
account.name
|
150
|
+
=> 'director'
|
151
|
+
account.website
|
152
|
+
=> 'www.johndoe.com/director'
|
153
|
+
account.collections[0].name
|
154
|
+
=> 'TestCollection'
|
155
|
+
account.collections[0].folder
|
156
|
+
=> 'folders/TestCollection'
|
157
|
+
|
105
158
|
```
|
106
159
|
|
107
160
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.16
|
@@ -21,15 +21,18 @@ module TarvitHelpers
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def method_missing(m, *args)
|
24
|
-
return
|
24
|
+
return _value(m) if _accessor_method?(m)
|
25
25
|
super
|
26
26
|
end
|
27
27
|
|
28
28
|
protected
|
29
29
|
|
30
|
-
def
|
31
|
-
|
32
|
-
|
30
|
+
def _value(method_name)
|
31
|
+
_transform_value(method_name, _hash_value(method_name))
|
32
|
+
end
|
33
|
+
|
34
|
+
def _hash_value(method_name)
|
35
|
+
_hash[method_name]
|
33
36
|
end
|
34
37
|
|
35
38
|
def _transform_value(method_name, value)
|
@@ -63,13 +66,12 @@ module TarvitHelpers
|
|
63
66
|
end
|
64
67
|
|
65
68
|
class CachedHashPresenter < SimpleHashPresenter
|
66
|
-
|
67
69
|
def initialize(hash, levels=[])
|
68
70
|
super
|
69
71
|
@cache = {}
|
70
72
|
end
|
71
73
|
|
72
|
-
def
|
74
|
+
def _value(method_name)
|
73
75
|
@cache[method_name] ||= super
|
74
76
|
end
|
75
77
|
end
|
@@ -90,25 +92,39 @@ module TarvitHelpers
|
|
90
92
|
|
91
93
|
def initialize(hash, levels=[], rules_holder=nil, &rules)
|
92
94
|
super(hash, levels)
|
93
|
-
@_rules_holder = rules_holder ||
|
95
|
+
@_rules_holder = rules_holder || _init_rules_holder
|
96
|
+
_init_rules
|
94
97
|
rules.call(_rules_holder) if rules
|
95
98
|
end
|
96
99
|
|
97
|
-
def _transform_value(method_name, value)
|
98
|
-
rule = _rules_holder.rule_for(_path(method_name))
|
99
|
-
rule ? rule.value_transformer.call(value) : super
|
100
|
-
end
|
101
|
-
|
102
100
|
def _current_path(method_name)
|
103
101
|
_levels + [ method_name ]
|
104
102
|
end
|
105
103
|
|
106
104
|
protected
|
107
105
|
|
106
|
+
def _hash_value(method_name)
|
107
|
+
value = super
|
108
|
+
rule = _rules_holder.rule_for(_path(method_name))
|
109
|
+
rule ? rule.value_transformer.call(value, self) : value
|
110
|
+
end
|
111
|
+
|
108
112
|
def _new_level_presenter(value, method_name)
|
109
113
|
self.class.new(value, _path(method_name), _rules_holder)
|
110
114
|
end
|
111
115
|
|
116
|
+
def _init_rules; end
|
117
|
+
|
118
|
+
def _init_rules_holder
|
119
|
+
RulesHolder.new
|
120
|
+
end
|
121
|
+
|
122
|
+
def _accessor_method?(method_name)
|
123
|
+
super(method_name) || _rules_holder.rules.map{|r| r.path.last }.include?(method_name)
|
124
|
+
end
|
125
|
+
|
126
|
+
alias_method :_rules, :_rules_holder
|
127
|
+
|
112
128
|
class RulesHolder
|
113
129
|
attr_reader :rules
|
114
130
|
|
@@ -134,4 +150,3 @@ module TarvitHelpers
|
|
134
150
|
end
|
135
151
|
end
|
136
152
|
end
|
137
|
-
|
@@ -177,10 +177,61 @@ describe HashPresenter::CustomHashPresenter do
|
|
177
177
|
expect(presenter.user.posts[0].title).to eq('SOME TITLE')
|
178
178
|
end
|
179
179
|
|
180
|
-
|
180
|
+
context 'Subclass' do
|
181
|
+
before :all do
|
182
|
+
@hash = {
|
183
|
+
accounts: [
|
184
|
+
{
|
185
|
+
:id => 1,
|
186
|
+
:name => :director,
|
187
|
+
collections: [
|
188
|
+
{
|
189
|
+
:id => 42,
|
190
|
+
:name => :test_collection,
|
191
|
+
}
|
192
|
+
]
|
193
|
+
}
|
194
|
+
]
|
195
|
+
}
|
196
|
+
|
197
|
+
class AccountsPresenter < HashPresenter::CustomHashPresenter
|
198
|
+
|
199
|
+
def _init_rules
|
200
|
+
rules = _rules
|
201
|
+
|
202
|
+
rules.when([:accounts, :name]) do |value|
|
203
|
+
value.to_s
|
204
|
+
end
|
181
205
|
|
182
|
-
|
206
|
+
rules.when([:accounts, :website]) do |value, object|
|
207
|
+
'www.johndoe.com/' + object.name.to_s
|
208
|
+
end
|
183
209
|
|
210
|
+
rules.when([:accounts, :collections, :name]) do |value|
|
211
|
+
value.to_s.camelize
|
212
|
+
end
|
213
|
+
|
214
|
+
rules.when([:accounts, :collections, :folder]) do |value, object|
|
215
|
+
"folders/#{object.name}"
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
@presenter = AccountsPresenter.new(@hash)
|
221
|
+
end
|
222
|
+
|
223
|
+
it 'should work as subclass' do
|
224
|
+
account = @presenter.accounts.first
|
225
|
+
expect(account.id).to eq(1)
|
226
|
+
expect(account.name).to eq('director')
|
227
|
+
expect(account.website).to eq('www.johndoe.com/director')
|
228
|
+
expect(account.collections[0].name).to eq('TestCollection')
|
229
|
+
expect(account.collections[0].folder).to eq('folders/TestCollection')
|
230
|
+
end
|
231
|
+
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
184
235
|
|
185
236
|
describe HashPresenter do
|
186
237
|
|
data/tarvit-helpers.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: tarvit-helpers 0.0.
|
5
|
+
# stub: tarvit-helpers 0.0.16 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "tarvit-helpers"
|
9
|
-
s.version = "0.0.
|
9
|
+
s.version = "0.0.16"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|