tarvit-helpers 0.0.10 → 0.0.11

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
  SHA1:
3
- metadata.gz: 047b0913876d901d412a7e7cc11216c66ebcf892
4
- data.tar.gz: e8818a9f0f1bf8f0eff4f583760768e0b2311889
3
+ metadata.gz: 5f2117e675264e10c3128bff56b480e423672048
4
+ data.tar.gz: cd965580ba89f6982bb98f6c12a3ef97cd217e1a
5
5
  SHA512:
6
- metadata.gz: 9ee9af47dc6705145a2b9cd5cf13eaa603506b64851e3afa92c2345b921153c9d893686444b26297b697df46c3cf58b621412c918a7e69042949945bf804a1c8
7
- data.tar.gz: 800b7f04ca01b573eeb069ce030f649a7aa7a5728dc184c1d321da133172420869ce66ac012faa337307a2d2c1bb4375b45fc4185c5386c295bb19595f33c5c4
6
+ metadata.gz: 66f741c836965bf736ae7eada042a3f554574d1fb1ee4eef693261cb903af73b6d688e6081bd7c40dfe55520af370533a499605c7f5691248d21bfdb52799340
7
+ data.tar.gz: ba134f8d4ae9e358c32dfac59db198548540aaffeb46428df06df9777ebabf925808f37f7c72544c91f9d9e715ad0221b3e97de6e6480ed2f1f9cb0d0da45cba
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.10
1
+ 0.0.11
@@ -1,44 +1,81 @@
1
1
  module TarvitHelpers
2
- class HashPresenter
3
- require 'active_support/core_ext/string'
2
+ module HashPresenter
4
3
 
5
- attr_reader :hash
6
-
7
- def initialize(hash)
8
- @hash = prepare_keys(hash)
4
+ def self.present(hash, option = :cached )
5
+ raise ArgumentError.new("#{ hash.class } is not a Hash") unless hash.is_a?(Hash)
6
+ factory[option].new(hash)
9
7
  end
10
8
 
11
- def method_missing(m, *args)
12
- return value(m) if accessor_method?(m)
13
- super
9
+ def self.factory
10
+ { cached: CachedHashPresenter, observable: ObservableHashPresenter }
14
11
  end
15
12
 
16
- protected
13
+ class SimpleHashPresenter
14
+ require 'active_support/core_ext/string'
17
15
 
18
- def value(method_name)
19
- res = @hash[method_name]
20
- transform_value(res)
21
- end
16
+ attr_reader :hash
22
17
 
23
- def transform_value(x)
24
- return x.map{|x| transform_value(x) } if x.is_a?(Array)
25
- x.is_a?(Hash) ? self.class.new(x) : x
26
- end
18
+ def initialize(hash)
19
+ @hash = prepare_keys(hash)
20
+ end
21
+
22
+ def method_missing(m, *args)
23
+ return value(m) if accessor_method?(m)
24
+ super
25
+ end
26
+
27
+ protected
28
+
29
+ def value(method_name)
30
+ res = self.hash[method_name]
31
+ transform_value(res)
32
+ end
33
+
34
+ def transform_value(x)
35
+ return x.map{|x| transform_value(x) } if x.is_a?(Array)
36
+ x.is_a?(Hash) ? self.class.new(x) : x
37
+ end
38
+
39
+ def accessor_method?(method_name)
40
+ self.hash.keys.include?(method_name)
41
+ end
42
+
43
+ def key_to_method(key)
44
+ key.to_s.gsub(/\s+/, ?_).underscore.to_sym
45
+ end
46
+
47
+ def prepare_keys(hash)
48
+ res = hash.map do |k ,v|
49
+ [ key_to_method(k), v ]
50
+ end
51
+ Hash[res]
52
+ end
27
53
 
28
- def accessor_method?(method_name)
29
- @hash.keys.include?(method_name)
30
54
  end
31
55
 
32
- def key_to_method(key)
33
- key.to_s.gsub(/\s+/, ?_).underscore.to_sym
56
+ class CachedHashPresenter < SimpleHashPresenter
57
+
58
+ def initialize(hash)
59
+ super
60
+ @cache = {}
61
+ end
62
+
63
+ def value(method_name)
64
+ @cache[method_name] ||= super
65
+ end
66
+
34
67
  end
35
68
 
36
- def prepare_keys(hash)
37
- res = hash.map do |k ,v|
38
- [ key_to_method(k), v ]
69
+ class ObservableHashPresenter < SimpleHashPresenter
70
+ def initialize(hash)
71
+ @hash = hash
72
+ end
73
+
74
+ def hash
75
+ prepare_keys(@hash)
39
76
  end
40
- Hash[res]
41
77
  end
42
78
 
43
79
  end
44
80
  end
81
+
@@ -1,32 +1,144 @@
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
+ end
14
+
15
+ it 'should transform complex keys' do
16
+ hp = hash_presenter.new('A very big key' => :value)
17
+ expect(hp.a_very_big_key).to eq(:value)
18
+ end
19
+
20
+ it 'should present nested hashes' do
21
+ hp = hash_presenter.new(a: { b: 1, c: { d: 2 } })
22
+ expect(hp.a).to be_a(hash_presenter)
23
+ expect(hp.a.b).to eq(1)
24
+ expect(hp.a.c.d).to eq(2)
25
+ end
26
+
27
+ it 'should present arrays with hashes' do
28
+ hp = hash_presenter.new(a: [ { b: 1 }, { c: 2 }, 3 ])
29
+ expect(hp.a[0].b).to eq(1)
30
+ expect(hp.a[1].c).to eq(2)
31
+ expect(hp.a[2]).to eq(3)
32
+ end
33
+ end
34
+ end
35
+
36
+ describe HashPresenter::SimpleHashPresenter do
37
+
38
+ extend BaseHashPresenterTest
39
+ test_presenter(HashPresenter::SimpleHashPresenter)
40
+
41
+ context 'Special Behavior' do
42
+
43
+ it 'should not depend on an attribute object' do
44
+ original = { a: 2 }
45
+ hp = HashPresenter::SimpleHashPresenter.new(original)
46
+
47
+ expect(hp.a).to eq(2)
48
+
49
+ original[:a] = 3
50
+
51
+ expect(hp.a).to eq(2)
52
+ end
53
+
54
+ it 'should regenerate method result for a nested hash' do
55
+ hp = HashPresenter::SimpleHashPresenter.new(a: { b: 1 })
56
+ expect(hp.a.object_id).to_not eq(hp.a.object_id)
57
+ end
58
+ end
59
+ end
60
+
61
+ describe HashPresenter::CachedHashPresenter do
62
+
63
+ extend BaseHashPresenterTest
64
+ test_presenter(HashPresenter::CachedHashPresenter)
65
+
66
+ context 'Special Behavior' do
67
+
68
+ it 'should not depend on an attribute object' do
69
+ original = { a: 2 }
70
+ hp = HashPresenter::CachedHashPresenter.new(original)
71
+
72
+ expect(hp.a).to eq(2)
73
+
74
+ original[:a] = 3
75
+
76
+ expect(hp.a).to eq(2)
77
+ end
78
+
79
+ it 'should not calculate result for a nested hash twice' do
80
+ hp = HashPresenter::CachedHashPresenter.new(a: { b: 1 })
81
+ expect(hp.a.object_id).to eq(hp.a.object_id)
82
+ end
83
+ end
84
+ end
85
+
86
+
87
+ describe HashPresenter::ObservableHashPresenter do
88
+
89
+ extend BaseHashPresenterTest
90
+ test_presenter(HashPresenter::ObservableHashPresenter)
91
+
92
+ context 'Special Behavior' do
93
+
94
+ it 'should observe an attribute object' do
95
+ original = { a: 2 }
96
+ hp = HashPresenter::ObservableHashPresenter.new(original)
97
+
98
+ expect(hp.a).to eq(2)
99
+
100
+ original[:a] = 3
101
+
102
+ expect(hp.a).to eq(3)
103
+
104
+ original[:a] = { b: 1 }
105
+
106
+ expect(hp.a.b).to eq(1)
107
+
108
+ original[:c] = 4
109
+
110
+ expect(hp.c).to eq(4)
111
+ end
112
+
113
+ it 'should regenerate method result for a nested hash' do
114
+ hp = HashPresenter::ObservableHashPresenter.new(a: { b: 1 })
115
+ expect(hp.a.object_id).to_not eq(hp.a.object_id)
116
+ end
117
+ end
118
+ end
119
+
3
120
  describe HashPresenter do
4
121
 
5
- it 'should present a flat hash' do
6
- hp = HashPresenter.new(a: 1, b: 2, 'c' => [ 3 ] )
122
+ it 'should present hashes' do
123
+ hp = HashPresenter.present({ a: 1 })
7
124
  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
125
  end
12
126
 
13
- it 'should transform complex keys' do
14
- hp = HashPresenter.new('A very big key' => :value)
15
- expect(hp.a_very_big_key).to eq(:value)
127
+ it 'should not accept not Hashes' do
128
+ expect(->{
129
+ HashPresenter.present('Hash').to raise_error(ArgumentError)
130
+ })
16
131
  end
17
132
 
18
- it 'should present nested hashes' do
19
- hp = HashPresenter.new(a: { b: 1, c: { d: 2 } })
20
- expect(hp.a).to be_a(HashPresenter)
21
- expect(hp.a.b).to eq(1)
22
- expect(hp.a.c.d).to eq(2)
133
+ it 'should get an observable presenter' do
134
+ hp = HashPresenter.present({ }, :observable)
135
+ expect(hp).to be_a(HashPresenter::ObservableHashPresenter)
23
136
  end
24
137
 
25
- it 'should present arrays with hashes' do
26
- hp = HashPresenter.new(a: [ { b: 1 }, { c: 2 }, 3 ])
27
- expect(hp.a[0].b).to eq(1)
28
- expect(hp.a[1].c).to eq(2)
29
- expect(hp.a[2]).to eq(3)
138
+ it 'should get a cached presenter' do
139
+ hp = HashPresenter.present({ }, :cached)
140
+ expect(hp).to be_a(HashPresenter::CachedHashPresenter)
30
141
  end
31
142
 
143
+
32
144
  end
@@ -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.10 ruby lib
5
+ # stub: tarvit-helpers 0.0.11 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "tarvit-helpers"
9
- s.version = "0.0.10"
9
+ s.version = "0.0.11"
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"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tarvit-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vitaly Tarasenko