tarvit-helpers 0.0.19 → 0.0.20
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 +1 -3
- data/VERSION +1 -1
- data/lib/tarvit-helpers/modules/hash_presenter/{cached_hash_presenter.rb → cached.rb} +0 -0
- data/lib/tarvit-helpers/modules/hash_presenter/{custom_hash_presenter.rb → custom.rb} +2 -4
- data/lib/tarvit-helpers/modules/hash_presenter/{observable_hash_presenter.rb → observable.rb} +0 -0
- data/lib/tarvit-helpers/modules/hash_presenter/{simple_hash_presenter.rb → simple.rb} +0 -0
- data/lib/tarvit-helpers/modules/hash_presenter/{with_rules_hash_presenter.rb → with_rules.rb} +2 -2
- data/lib/tarvit-helpers/modules/hash_presenter.rb +5 -5
- data/spec/modules/hash_presenter/cached_spec.rb +24 -0
- data/spec/modules/hash_presenter/custom_spec.rb +129 -0
- data/spec/modules/hash_presenter/observable_spec.rb +32 -0
- data/spec/modules/hash_presenter/simple_spec.rb +24 -0
- data/spec/modules/hash_presenter_spec.rb +0 -251
- data/spec/spec_helper.rb +1 -1
- data/spec/support/base_tests/base_hash_presenter_test.rb +33 -0
- data/tarvit-helpers.gemspec +13 -8
- metadata +12 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd1b908ccf653e1909cdc051761ee3faaef7558a
|
4
|
+
data.tar.gz: 779cf74315f93e08e58977d354ceeb7449e00e0a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4321541963b3e2b869e42572fa53461b82d32aee90fe561ab6a82eab81ff0a284c92801f67cf79c264f56fbe703e2d66c1a6493ba0cee4229464ee1324203295
|
7
|
+
data.tar.gz: 503e67c0d0a8bd853ed4b536180059eb7c767daf2b25b26964d0998d94d3e8157ee63f8ad3d3f72eebfe34c4ae9784e357f8835ce9a5429d2f6699188654ccbd
|
data/README.md
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.20
|
File without changes
|
@@ -36,11 +36,9 @@ module TarvitHelpers
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def _path_presenter(path)
|
39
|
-
res
|
40
|
-
|
41
|
-
res = res.is_a?(Array) ? res[level] : res.send(level)
|
39
|
+
path.inject(self) do |res, level|
|
40
|
+
res.is_a?(Array) ? res[level] : res.send(level)
|
42
41
|
end
|
43
|
-
res
|
44
42
|
end
|
45
43
|
|
46
44
|
end
|
data/lib/tarvit-helpers/modules/hash_presenter/{observable_hash_presenter.rb → observable.rb}
RENAMED
File without changes
|
File without changes
|
data/lib/tarvit-helpers/modules/hash_presenter/{with_rules_hash_presenter.rb → with_rules.rb}
RENAMED
@@ -7,7 +7,7 @@ module TarvitHelpers
|
|
7
7
|
def initialize(hash, levels=[], parent=nil, rules_holder=nil, &rules)
|
8
8
|
super(hash, levels, parent)
|
9
9
|
@_rules_holder = rules_holder || RulesHolder.new
|
10
|
-
|
10
|
+
_add_rules(@_rules_holder) if _rules_holder.rules.empty?
|
11
11
|
rules.call(_rules_holder) if rules
|
12
12
|
end
|
13
13
|
|
@@ -27,7 +27,7 @@ module TarvitHelpers
|
|
27
27
|
self.class.new(value, _path(method_name), self, _rules_holder)
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
30
|
+
def _add_rules(rules_holder); end
|
31
31
|
|
32
32
|
def _accessor_method?(method_name)
|
33
33
|
super(method_name) || _rules_holder.rules.map{|r| r.path.last }.include?(method_name)
|
@@ -1,10 +1,10 @@
|
|
1
1
|
module TarvitHelpers
|
2
2
|
module HashPresenter
|
3
|
-
require_relative '../modules/hash_presenter/
|
4
|
-
require_relative '../modules/hash_presenter/
|
5
|
-
require_relative '../modules/hash_presenter/
|
6
|
-
require_relative '../modules/hash_presenter/
|
7
|
-
require_relative '../modules/hash_presenter/
|
3
|
+
require_relative '../modules/hash_presenter/simple'
|
4
|
+
require_relative '../modules/hash_presenter/cached'
|
5
|
+
require_relative '../modules/hash_presenter/observable'
|
6
|
+
require_relative '../modules/hash_presenter/with_rules'
|
7
|
+
require_relative '../modules/hash_presenter/custom'
|
8
8
|
|
9
9
|
def self.present(hash, option = :cached )
|
10
10
|
raise ArgumentError.new("#{ hash.class } is not a Hash") unless hash.is_a?(Hash)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
describe HashPresenter::Cached do
|
2
|
+
|
3
|
+
extend BaseHashPresenterTest
|
4
|
+
test_presenter(HashPresenter::Cached)
|
5
|
+
|
6
|
+
context 'Special Behavior' do
|
7
|
+
|
8
|
+
it 'should not depend on an attribute object' do
|
9
|
+
original = { a: 2 }
|
10
|
+
hp = HashPresenter::Cached.new(original)
|
11
|
+
|
12
|
+
expect(hp.a).to eq(2)
|
13
|
+
|
14
|
+
original[:a] = 3
|
15
|
+
|
16
|
+
expect(hp.a).to eq(2)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should not calculate result for a nested hash twice' do
|
20
|
+
hp = HashPresenter::Cached.new(a: { b: 1 })
|
21
|
+
expect(hp.a.object_id).to eq(hp.a.object_id)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
describe HashPresenter::Custom do
|
2
|
+
extend BaseHashPresenterTest
|
3
|
+
test_presenter(HashPresenter::Custom)
|
4
|
+
|
5
|
+
context 'Special Behavior' do
|
6
|
+
before :each do
|
7
|
+
@hash = {
|
8
|
+
user: {
|
9
|
+
date: '11/11/2015',
|
10
|
+
age: '11',
|
11
|
+
address: [
|
12
|
+
'USA', 'NY', 'Ba Street'
|
13
|
+
],
|
14
|
+
posts: [
|
15
|
+
{ id: '1', title: 'some title' },
|
16
|
+
{ id: '2', title: 'the other title' },
|
17
|
+
],
|
18
|
+
}
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should customize presenter' do
|
23
|
+
presenter = HashPresenter::Custom.new(@hash) do |rules|
|
24
|
+
rules.when([ :user, :date ]) do |value|
|
25
|
+
Date.parse(value)
|
26
|
+
end
|
27
|
+
|
28
|
+
rules.when([ :user, :age ]){|age| age.to_i }
|
29
|
+
rules.when([ :user, :posts, :title ]){|title| title.capitalize }
|
30
|
+
rules.when([ :user, :address ]){|list| list.join(?/) }
|
31
|
+
end
|
32
|
+
|
33
|
+
expect(presenter.user.date).to eq(Date.new(2015, 11, 11))
|
34
|
+
expect(presenter.user.age).to eq(11)
|
35
|
+
expect(presenter.user.address).to eq('USA/NY/Ba Street')
|
36
|
+
expect(presenter.user.posts[0].title).to eq('Some title')
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should declare nested presenters' do
|
40
|
+
presenter = HashPresenter::Custom.new(@hash) do |rules|
|
41
|
+
|
42
|
+
rules.when([ :user, :date ]) do |value|
|
43
|
+
Date.parse(value)
|
44
|
+
end
|
45
|
+
|
46
|
+
rules.when([ :user, :posts ])do |posts|
|
47
|
+
posts.map do |post|
|
48
|
+
HashPresenter::Custom.new(post) do |post_rules|
|
49
|
+
post_rules.when([ :title ]){|title| title.upcase }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
expect(presenter.user.date).to eq(Date.new(2015, 11, 11))
|
56
|
+
expect(presenter.user.posts[0].title).to eq('SOME TITLE')
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'Subclass' do
|
60
|
+
before :all do
|
61
|
+
@hash = {
|
62
|
+
accounts: [
|
63
|
+
{
|
64
|
+
:id => 1,
|
65
|
+
:name => :director,
|
66
|
+
collections: [
|
67
|
+
{ :id => 42, :name => :test_collection },
|
68
|
+
{ :id => 24, :name => :best_collection },
|
69
|
+
]
|
70
|
+
}
|
71
|
+
]
|
72
|
+
}
|
73
|
+
|
74
|
+
class AccountsPresenter < HashPresenter::Custom
|
75
|
+
|
76
|
+
def _add_rules(rules)
|
77
|
+
rules.when([:accounts, :name]) do |value|
|
78
|
+
value.to_s
|
79
|
+
end
|
80
|
+
|
81
|
+
rules.when([:accounts, :website]) do |value, object|
|
82
|
+
'www.johndoe.com/' + object.name.to_s
|
83
|
+
end
|
84
|
+
|
85
|
+
rules.when([:accounts, :collections, :name]) do |value|
|
86
|
+
value.to_s.camelize
|
87
|
+
end
|
88
|
+
|
89
|
+
rules.when([:accounts, :collections, :folder]) do |value, object|
|
90
|
+
"folders/#{object.name}"
|
91
|
+
end
|
92
|
+
|
93
|
+
rules.when([:accounts, :collections, :global_folder]) do |value, object|
|
94
|
+
"#{object._parent.name}/folders/#{object.name}"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
@presenter = AccountsPresenter.new(@hash)
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'should work as subclass' do
|
103
|
+
account = @presenter.accounts.first
|
104
|
+
expect(account.id).to eq(1)
|
105
|
+
expect(account.name).to eq('director')
|
106
|
+
expect(account.website).to eq('www.johndoe.com/director')
|
107
|
+
expect(account.collections[0].name).to eq('TestCollection')
|
108
|
+
expect(account.collections[0].folder).to eq('folders/TestCollection')
|
109
|
+
expect(account.collections[1].name).to eq('BestCollection')
|
110
|
+
expect(account.collections[1].folder).to eq('folders/BestCollection')
|
111
|
+
expect(account.collections[1].global_folder).to eq('director/folders/BestCollection')
|
112
|
+
|
113
|
+
expect(@presenter._custom_hash).to eq({
|
114
|
+
:accounts => [
|
115
|
+
{
|
116
|
+
:id=>1,
|
117
|
+
:name=>'director',
|
118
|
+
:collections=>[
|
119
|
+
{:id=>42, :name=>'TestCollection', :folder=>'folders/TestCollection', :global_folder=>'director/folders/TestCollection'},
|
120
|
+
{:id=>24, :name=>'BestCollection', :folder=>'folders/BestCollection', :global_folder=>'director/folders/BestCollection'},
|
121
|
+
],
|
122
|
+
:website=>'www.johndoe.com/director'
|
123
|
+
}
|
124
|
+
]
|
125
|
+
})
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
describe HashPresenter::Observable do
|
2
|
+
|
3
|
+
extend BaseHashPresenterTest
|
4
|
+
test_presenter(HashPresenter::Observable)
|
5
|
+
|
6
|
+
context 'Special Behavior' do
|
7
|
+
|
8
|
+
it 'should observe an attribute object' do
|
9
|
+
original = { a: 2 }
|
10
|
+
hp = HashPresenter::Observable.new(original)
|
11
|
+
|
12
|
+
expect(hp.a).to eq(2)
|
13
|
+
|
14
|
+
original[:a] = 3
|
15
|
+
|
16
|
+
expect(hp.a).to eq(3)
|
17
|
+
|
18
|
+
original[:a] = { b: 1 }
|
19
|
+
|
20
|
+
expect(hp.a.b).to eq(1)
|
21
|
+
|
22
|
+
original[:c] = 4
|
23
|
+
|
24
|
+
expect(hp.c).to eq(4)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should regenerate method result for a nested hash' do
|
28
|
+
hp = HashPresenter::Observable.new(a: { b: 1 })
|
29
|
+
expect(hp.a.object_id).to_not eq(hp.a.object_id)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
describe HashPresenter::Simple do
|
2
|
+
|
3
|
+
extend BaseHashPresenterTest
|
4
|
+
test_presenter(HashPresenter::Simple)
|
5
|
+
|
6
|
+
context 'Special Behavior' do
|
7
|
+
|
8
|
+
it 'should not depend on an attribute object' do
|
9
|
+
original = { a: 2 }
|
10
|
+
hp = HashPresenter::Simple.new(original)
|
11
|
+
|
12
|
+
expect(hp.a).to eq(2)
|
13
|
+
|
14
|
+
original[:a] = 3
|
15
|
+
|
16
|
+
expect(hp.a).to eq(2)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should regenerate method result for a nested hash' do
|
20
|
+
hp = HashPresenter::Simple.new(a: { b: 1 })
|
21
|
+
expect(hp.a.object_id).to_not eq(hp.a.object_id)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -1,256 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
module BaseHashPresenterTest
|
4
|
-
|
5
|
-
def test_presenter(hash_presenter)
|
6
|
-
|
7
|
-
it 'should present a flat hash' do
|
8
|
-
hp = hash_presenter.new(a: 1, b: 2, 'c' => [ 3 ] )
|
9
|
-
expect(hp.a).to eq(1)
|
10
|
-
expect(hp.b).to eq(2)
|
11
|
-
expect(hp.c).to eq([ 3 ])
|
12
|
-
expect(->{ hp.d }).to raise_error(NoMethodError)
|
13
|
-
expect(hp._hash.values).to eq([1, 2, [3]])
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'should transform complex keys' do
|
17
|
-
hp = hash_presenter.new('A very big key' => :value)
|
18
|
-
expect(hp.a_very_big_key).to eq(:value)
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'should present nested hashes' do
|
22
|
-
hp = hash_presenter.new(a: { b: 1, c: { d: 2 } })
|
23
|
-
expect(hp.a).to be_a(hash_presenter)
|
24
|
-
expect(hp.a.b).to eq(1)
|
25
|
-
expect(hp.a.c.d).to eq(2)
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'should present arrays with hashes' do
|
29
|
-
hp = hash_presenter.new(a: [ { b: 1 }, { c: 2 }, 3 ])
|
30
|
-
expect(hp.a[0].b).to eq(1)
|
31
|
-
expect(hp.a[1].c).to eq(2)
|
32
|
-
expect(hp.a[2]).to eq(3)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
describe HashPresenter::Simple do
|
38
|
-
|
39
|
-
extend BaseHashPresenterTest
|
40
|
-
test_presenter(HashPresenter::Simple)
|
41
|
-
|
42
|
-
context 'Special Behavior' do
|
43
|
-
|
44
|
-
it 'should not depend on an attribute object' do
|
45
|
-
original = { a: 2 }
|
46
|
-
hp = HashPresenter::Simple.new(original)
|
47
|
-
|
48
|
-
expect(hp.a).to eq(2)
|
49
|
-
|
50
|
-
original[:a] = 3
|
51
|
-
|
52
|
-
expect(hp.a).to eq(2)
|
53
|
-
end
|
54
|
-
|
55
|
-
it 'should regenerate method result for a nested hash' do
|
56
|
-
hp = HashPresenter::Simple.new(a: { b: 1 })
|
57
|
-
expect(hp.a.object_id).to_not eq(hp.a.object_id)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
describe HashPresenter::Cached do
|
63
|
-
|
64
|
-
extend BaseHashPresenterTest
|
65
|
-
test_presenter(HashPresenter::Cached)
|
66
|
-
|
67
|
-
context 'Special Behavior' do
|
68
|
-
|
69
|
-
it 'should not depend on an attribute object' do
|
70
|
-
original = { a: 2 }
|
71
|
-
hp = HashPresenter::Cached.new(original)
|
72
|
-
|
73
|
-
expect(hp.a).to eq(2)
|
74
|
-
|
75
|
-
original[:a] = 3
|
76
|
-
|
77
|
-
expect(hp.a).to eq(2)
|
78
|
-
end
|
79
|
-
|
80
|
-
it 'should not calculate result for a nested hash twice' do
|
81
|
-
hp = HashPresenter::Cached.new(a: { b: 1 })
|
82
|
-
expect(hp.a.object_id).to eq(hp.a.object_id)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
|
88
|
-
describe HashPresenter::Observable do
|
89
|
-
|
90
|
-
extend BaseHashPresenterTest
|
91
|
-
test_presenter(HashPresenter::Observable)
|
92
|
-
|
93
|
-
context 'Special Behavior' do
|
94
|
-
|
95
|
-
it 'should observe an attribute object' do
|
96
|
-
original = { a: 2 }
|
97
|
-
hp = HashPresenter::Observable.new(original)
|
98
|
-
|
99
|
-
expect(hp.a).to eq(2)
|
100
|
-
|
101
|
-
original[:a] = 3
|
102
|
-
|
103
|
-
expect(hp.a).to eq(3)
|
104
|
-
|
105
|
-
original[:a] = { b: 1 }
|
106
|
-
|
107
|
-
expect(hp.a.b).to eq(1)
|
108
|
-
|
109
|
-
original[:c] = 4
|
110
|
-
|
111
|
-
expect(hp.c).to eq(4)
|
112
|
-
end
|
113
|
-
|
114
|
-
it 'should regenerate method result for a nested hash' do
|
115
|
-
hp = HashPresenter::Observable.new(a: { b: 1 })
|
116
|
-
expect(hp.a.object_id).to_not eq(hp.a.object_id)
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
|
122
|
-
describe HashPresenter::Custom do
|
123
|
-
extend BaseHashPresenterTest
|
124
|
-
test_presenter(HashPresenter::Custom)
|
125
|
-
|
126
|
-
context 'Special Behavior' do
|
127
|
-
before :each do
|
128
|
-
@hash = {
|
129
|
-
user: {
|
130
|
-
date: '11/11/2015',
|
131
|
-
age: '11',
|
132
|
-
address: [
|
133
|
-
'USA', 'NY', 'Ba Street'
|
134
|
-
],
|
135
|
-
posts: [
|
136
|
-
{ id: '1', title: 'some title' },
|
137
|
-
{ id: '2', title: 'the other title' },
|
138
|
-
],
|
139
|
-
}
|
140
|
-
}
|
141
|
-
end
|
142
|
-
|
143
|
-
it 'should customize presenter' do
|
144
|
-
presenter = HashPresenter::Custom.new(@hash) do |rules|
|
145
|
-
rules.when([ :user, :date ]) do |value|
|
146
|
-
Date.parse(value)
|
147
|
-
end
|
148
|
-
|
149
|
-
rules.when([ :user, :age ]){|age| age.to_i }
|
150
|
-
rules.when([ :user, :posts, :title ]){|title| title.capitalize }
|
151
|
-
rules.when([ :user, :address ]){|list| list.join(?/) }
|
152
|
-
end
|
153
|
-
|
154
|
-
expect(presenter.user.date).to eq(Date.new(2015, 11, 11))
|
155
|
-
expect(presenter.user.age).to eq(11)
|
156
|
-
expect(presenter.user.address).to eq('USA/NY/Ba Street')
|
157
|
-
expect(presenter.user.posts[0].title).to eq('Some title')
|
158
|
-
end
|
159
|
-
|
160
|
-
it 'should declare nested presenters' do
|
161
|
-
presenter = HashPresenter::Custom.new(@hash) do |rules|
|
162
|
-
|
163
|
-
rules.when([ :user, :date ]) do |value|
|
164
|
-
Date.parse(value)
|
165
|
-
end
|
166
|
-
|
167
|
-
rules.when([ :user, :posts ])do |posts|
|
168
|
-
posts.map do |post|
|
169
|
-
HashPresenter::Custom.new(post) do |post_rules|
|
170
|
-
post_rules.when([ :title ]){|title| title.upcase }
|
171
|
-
end
|
172
|
-
end
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
expect(presenter.user.date).to eq(Date.new(2015, 11, 11))
|
177
|
-
expect(presenter.user.posts[0].title).to eq('SOME TITLE')
|
178
|
-
end
|
179
|
-
|
180
|
-
context 'Subclass' do
|
181
|
-
before :all do
|
182
|
-
@hash = {
|
183
|
-
accounts: [
|
184
|
-
{
|
185
|
-
:id => 1,
|
186
|
-
:name => :director,
|
187
|
-
collections: [
|
188
|
-
{ :id => 42, :name => :test_collection },
|
189
|
-
{ :id => 24, :name => :best_collection },
|
190
|
-
]
|
191
|
-
}
|
192
|
-
]
|
193
|
-
}
|
194
|
-
|
195
|
-
class AccountsPresenter < HashPresenter::Custom
|
196
|
-
|
197
|
-
def _init_rules
|
198
|
-
rules = _rules
|
199
|
-
|
200
|
-
rules.when([:accounts, :name]) do |value|
|
201
|
-
value.to_s
|
202
|
-
end
|
203
|
-
|
204
|
-
rules.when([:accounts, :website]) do |value, object|
|
205
|
-
'www.johndoe.com/' + object.name.to_s
|
206
|
-
end
|
207
|
-
|
208
|
-
rules.when([:accounts, :collections, :name]) do |value|
|
209
|
-
value.to_s.camelize
|
210
|
-
end
|
211
|
-
|
212
|
-
rules.when([:accounts, :collections, :folder]) do |value, object|
|
213
|
-
"folders/#{object.name}"
|
214
|
-
end
|
215
|
-
|
216
|
-
rules.when([:accounts, :collections, :global_folder]) do |value, object|
|
217
|
-
"#{object._parent.name}/folders/#{object.name}"
|
218
|
-
end
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
@presenter = AccountsPresenter.new(@hash)
|
223
|
-
end
|
224
|
-
|
225
|
-
it 'should work as subclass' do
|
226
|
-
account = @presenter.accounts.first
|
227
|
-
expect(account.id).to eq(1)
|
228
|
-
expect(account.name).to eq('director')
|
229
|
-
expect(account.website).to eq('www.johndoe.com/director')
|
230
|
-
expect(account.collections[0].name).to eq('TestCollection')
|
231
|
-
expect(account.collections[0].folder).to eq('folders/TestCollection')
|
232
|
-
expect(account.collections[1].name).to eq('BestCollection')
|
233
|
-
expect(account.collections[1].folder).to eq('folders/BestCollection')
|
234
|
-
expect(account.collections[1].global_folder).to eq('director/folders/BestCollection')
|
235
|
-
|
236
|
-
expect(@presenter._custom_hash).to eq({
|
237
|
-
:accounts => [
|
238
|
-
{
|
239
|
-
:id=>1,
|
240
|
-
:name=>'director',
|
241
|
-
:collections=>[
|
242
|
-
{:id=>42, :name=>'TestCollection', :folder=>'folders/TestCollection', :global_folder=>'director/folders/TestCollection'},
|
243
|
-
{:id=>24, :name=>'BestCollection', :folder=>'folders/BestCollection', :global_folder=>'director/folders/BestCollection'},
|
244
|
-
],
|
245
|
-
:website=>'www.johndoe.com/director'
|
246
|
-
}
|
247
|
-
]
|
248
|
-
})
|
249
|
-
end
|
250
|
-
end
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
3
|
describe HashPresenter do
|
255
4
|
|
256
5
|
it 'should present hashes' do
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,33 @@
|
|
1
|
+
module BaseHashPresenterTest
|
2
|
+
|
3
|
+
def test_presenter(hash_presenter)
|
4
|
+
|
5
|
+
it 'should present a flat hash' do
|
6
|
+
hp = hash_presenter.new(a: 1, b: 2, 'c' => [ 3 ] )
|
7
|
+
expect(hp.a).to eq(1)
|
8
|
+
expect(hp.b).to eq(2)
|
9
|
+
expect(hp.c).to eq([ 3 ])
|
10
|
+
expect(->{ hp.d }).to raise_error(NoMethodError)
|
11
|
+
expect(hp._hash.values).to eq([1, 2, [3]])
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should transform complex keys' do
|
15
|
+
hp = hash_presenter.new('A very big key' => :value)
|
16
|
+
expect(hp.a_very_big_key).to eq(:value)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should present nested hashes' do
|
20
|
+
hp = hash_presenter.new(a: { b: 1, c: { d: 2 } })
|
21
|
+
expect(hp.a).to be_a(hash_presenter)
|
22
|
+
expect(hp.a.b).to eq(1)
|
23
|
+
expect(hp.a.c.d).to eq(2)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should present arrays with hashes' do
|
27
|
+
hp = hash_presenter.new(a: [ { b: 1 }, { c: 2 }, 3 ])
|
28
|
+
expect(hp.a[0].b).to eq(1)
|
29
|
+
expect(hp.a[1].c).to eq(2)
|
30
|
+
expect(hp.a[2]).to eq(3)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/tarvit-helpers.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
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.20 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.20"
|
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"]
|
13
13
|
s.authors = ["Vitaly Tarasenko"]
|
14
|
-
s.date = "2015-10-
|
14
|
+
s.date = "2015-10-28"
|
15
15
|
s.description = " Simple extensions to standard Ruby classes and useful helpers. "
|
16
16
|
s.email = "vetal.tarasenko@gmail.com"
|
17
17
|
s.extra_rdoc_files = [
|
@@ -30,19 +30,24 @@ Gem::Specification.new do |s|
|
|
30
30
|
"lib/tarvit-helpers/extensions/counter.rb",
|
31
31
|
"lib/tarvit-helpers/modules/conditional_logger.rb",
|
32
32
|
"lib/tarvit-helpers/modules/hash_presenter.rb",
|
33
|
-
"lib/tarvit-helpers/modules/hash_presenter/
|
34
|
-
"lib/tarvit-helpers/modules/hash_presenter/
|
35
|
-
"lib/tarvit-helpers/modules/hash_presenter/
|
36
|
-
"lib/tarvit-helpers/modules/hash_presenter/
|
37
|
-
"lib/tarvit-helpers/modules/hash_presenter/
|
33
|
+
"lib/tarvit-helpers/modules/hash_presenter/cached.rb",
|
34
|
+
"lib/tarvit-helpers/modules/hash_presenter/custom.rb",
|
35
|
+
"lib/tarvit-helpers/modules/hash_presenter/observable.rb",
|
36
|
+
"lib/tarvit-helpers/modules/hash_presenter/simple.rb",
|
37
|
+
"lib/tarvit-helpers/modules/hash_presenter/with_rules.rb",
|
38
38
|
"lib/tarvit-helpers/modules/non_shared_accessors.rb",
|
39
39
|
"lib/tarvit-helpers/modules/recursive_loader.rb",
|
40
40
|
"lib/tarvit-helpers/modules/simple_crypt.rb",
|
41
41
|
"spec/extensions/counter_spec.rb",
|
42
|
+
"spec/modules/hash_presenter/cached_spec.rb",
|
43
|
+
"spec/modules/hash_presenter/custom_spec.rb",
|
44
|
+
"spec/modules/hash_presenter/observable_spec.rb",
|
45
|
+
"spec/modules/hash_presenter/simple_spec.rb",
|
42
46
|
"spec/modules/hash_presenter_spec.rb",
|
43
47
|
"spec/modules/non_shared_accessors_spec.rb",
|
44
48
|
"spec/modules/simple_crypt_spec.rb",
|
45
49
|
"spec/spec_helper.rb",
|
50
|
+
"spec/support/base_tests/base_hash_presenter_test.rb",
|
46
51
|
"tarvit-helpers.gemspec"
|
47
52
|
]
|
48
53
|
s.homepage = "http://github.com/tarvit/tarvit-helpers"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tarvit-helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.20
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vitaly Tarasenko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -113,19 +113,24 @@ files:
|
|
113
113
|
- lib/tarvit-helpers/extensions/counter.rb
|
114
114
|
- lib/tarvit-helpers/modules/conditional_logger.rb
|
115
115
|
- lib/tarvit-helpers/modules/hash_presenter.rb
|
116
|
-
- lib/tarvit-helpers/modules/hash_presenter/
|
117
|
-
- lib/tarvit-helpers/modules/hash_presenter/
|
118
|
-
- lib/tarvit-helpers/modules/hash_presenter/
|
119
|
-
- lib/tarvit-helpers/modules/hash_presenter/
|
120
|
-
- lib/tarvit-helpers/modules/hash_presenter/
|
116
|
+
- lib/tarvit-helpers/modules/hash_presenter/cached.rb
|
117
|
+
- lib/tarvit-helpers/modules/hash_presenter/custom.rb
|
118
|
+
- lib/tarvit-helpers/modules/hash_presenter/observable.rb
|
119
|
+
- lib/tarvit-helpers/modules/hash_presenter/simple.rb
|
120
|
+
- lib/tarvit-helpers/modules/hash_presenter/with_rules.rb
|
121
121
|
- lib/tarvit-helpers/modules/non_shared_accessors.rb
|
122
122
|
- lib/tarvit-helpers/modules/recursive_loader.rb
|
123
123
|
- lib/tarvit-helpers/modules/simple_crypt.rb
|
124
124
|
- spec/extensions/counter_spec.rb
|
125
|
+
- spec/modules/hash_presenter/cached_spec.rb
|
126
|
+
- spec/modules/hash_presenter/custom_spec.rb
|
127
|
+
- spec/modules/hash_presenter/observable_spec.rb
|
128
|
+
- spec/modules/hash_presenter/simple_spec.rb
|
125
129
|
- spec/modules/hash_presenter_spec.rb
|
126
130
|
- spec/modules/non_shared_accessors_spec.rb
|
127
131
|
- spec/modules/simple_crypt_spec.rb
|
128
132
|
- spec/spec_helper.rb
|
133
|
+
- spec/support/base_tests/base_hash_presenter_test.rb
|
129
134
|
- tarvit-helpers.gemspec
|
130
135
|
homepage: http://github.com/tarvit/tarvit-helpers
|
131
136
|
licenses:
|