tarvit-helpers 0.0.9 → 0.0.10

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: 644c35d394db579b8425b82ed1f9e29db00b0167
4
- data.tar.gz: 61b3837f381c44253a35d0cccaa8e9fd28d3d874
3
+ metadata.gz: 047b0913876d901d412a7e7cc11216c66ebcf892
4
+ data.tar.gz: e8818a9f0f1bf8f0eff4f583760768e0b2311889
5
5
  SHA512:
6
- metadata.gz: 9e63338bb91c26d064da40ad2aea2f5fb4a3b12a5cf36a5d609f25e2f70e852301cfdb0bf7d7b0eed546ab75a4ac1ad9d91c23c586fbfe34841d2fa8f8b42ad9
7
- data.tar.gz: 60964357f695dfef8383b61c31303f2fd2b09da42bc38603156626ca470977024d8cd414a750f8961aabe4bcd59c23c4e155d436cdb0916eb5d102b07fad8c59
6
+ metadata.gz: 9ee9af47dc6705145a2b9cd5cf13eaa603506b64851e3afa92c2345b921153c9d893686444b26297b697df46c3cf58b621412c918a7e69042949945bf804a1c8
7
+ data.tar.gz: 800b7f04ca01b573eeb069ce030f649a7aa7a5728dc184c1d321da133172420869ce66ac012faa337307a2d2c1bb4375b45fc4185c5386c295bb19595f33c5c4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.9
1
+ 0.0.10
@@ -4,5 +4,6 @@ module TarvitHelpers
4
4
  require 'tarvit-helpers/modules/non_shared_accessors'
5
5
  require 'tarvit-helpers/modules/simple_crypt'
6
6
  require 'tarvit-helpers/modules/conditional_logger'
7
+ require 'tarvit-helpers/modules/hash_presenter'
7
8
 
8
- end
9
+ end
@@ -0,0 +1,44 @@
1
+ module TarvitHelpers
2
+ class HashPresenter
3
+ require 'active_support/core_ext/string'
4
+
5
+ attr_reader :hash
6
+
7
+ def initialize(hash)
8
+ @hash = prepare_keys(hash)
9
+ end
10
+
11
+ def method_missing(m, *args)
12
+ return value(m) if accessor_method?(m)
13
+ super
14
+ end
15
+
16
+ protected
17
+
18
+ def value(method_name)
19
+ res = @hash[method_name]
20
+ transform_value(res)
21
+ end
22
+
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
27
+
28
+ def accessor_method?(method_name)
29
+ @hash.keys.include?(method_name)
30
+ end
31
+
32
+ def key_to_method(key)
33
+ key.to_s.gsub(/\s+/, ?_).underscore.to_sym
34
+ end
35
+
36
+ def prepare_keys(hash)
37
+ res = hash.map do |k ,v|
38
+ [ key_to_method(k), v ]
39
+ end
40
+ Hash[res]
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe HashPresenter do
4
+
5
+ it 'should present a flat hash' do
6
+ hp = HashPresenter.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
+ end
12
+
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)
16
+ end
17
+
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)
23
+ end
24
+
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)
30
+ end
31
+
32
+ end
@@ -5,52 +5,52 @@ describe NonSharedAccessors do
5
5
  context 'Native Class Accessor in Ruby' do
6
6
 
7
7
  it 'should explain native behavior (shared class variable)' do
8
- class A
8
+ class A1
9
9
  cattr_accessor :value
10
10
  end
11
11
 
12
- class B < A
12
+ class B1 < A1
13
13
 
14
14
  end
15
15
 
16
- expect(A.value).to be_nil
17
- expect(B.value).to be_nil
16
+ expect(A1.value).to be_nil
17
+ expect(B1.value).to be_nil
18
18
 
19
- A.value = 2
19
+ A1.value = 2
20
20
 
21
- expect(A.value).to eq(2)
22
- expect(B.value).to eq(2)
21
+ expect(A1.value).to eq(2)
22
+ expect(B1.value).to eq(2)
23
23
 
24
- B.value = 3
24
+ B1.value = 3
25
25
 
26
- expect(A.value).to eq(3)
27
- expect(B.value).to eq(3)
26
+ expect(A1.value).to eq(3)
27
+ expect(B1.value).to eq(3)
28
28
  end
29
29
  end
30
30
 
31
31
  it 'should behave as a separate accessor(separate class valiable)' do
32
32
 
33
- class A
33
+ class A2
34
34
  include NonSharedAccessors
35
35
  non_shared_cattr_accessor :value
36
36
  end
37
37
 
38
- class B < A
38
+ class B2 < A2
39
39
 
40
40
  end
41
41
 
42
- expect(A.value).to be_nil
43
- expect(B.value).to be_nil
42
+ expect(A2.value).to be_nil
43
+ expect(B2.value).to be_nil
44
44
 
45
- A.value = 2
45
+ A2.value = 2
46
46
 
47
- expect(A.value).to eq(2)
48
- expect(B.value).to eq(nil)
47
+ expect(A2.value).to eq(2)
48
+ expect(B2.value).to eq(nil)
49
49
 
50
- B.value = 3
50
+ B2.value = 3
51
51
 
52
- expect(A.value).to eq(2)
53
- expect(B.value).to eq(3)
52
+ expect(A2.value).to eq(2)
53
+ expect(B2.value).to eq(3)
54
54
  end
55
55
 
56
56
  end
@@ -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.9 ruby lib
5
+ # stub: tarvit-helpers 0.0.10 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "tarvit-helpers"
9
- s.version = "0.0.9"
9
+ s.version = "0.0.10"
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-07-05"
14
+ s.date = "2015-10-21"
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 = [
@@ -29,9 +29,11 @@ Gem::Specification.new do |s|
29
29
  "lib/tarvit-helpers/extensions/colored_string.rb",
30
30
  "lib/tarvit-helpers/extensions/counter.rb",
31
31
  "lib/tarvit-helpers/modules/conditional_logger.rb",
32
+ "lib/tarvit-helpers/modules/hash_presenter.rb",
32
33
  "lib/tarvit-helpers/modules/non_shared_accessors.rb",
33
34
  "lib/tarvit-helpers/modules/simple_crypt.rb",
34
35
  "spec/extensions/counter_spec.rb",
36
+ "spec/modules/hash_presenter_spec.rb",
35
37
  "spec/modules/non_shared_accessors_spec.rb",
36
38
  "spec/modules/simple_crypt_spec.rb",
37
39
  "spec/spec_helper.rb",
@@ -39,7 +41,7 @@ Gem::Specification.new do |s|
39
41
  ]
40
42
  s.homepage = "http://github.com/tarvit/tarvit-helpers"
41
43
  s.licenses = ["MIT"]
42
- s.rubygems_version = "2.2.2"
44
+ s.rubygems_version = "2.4.8"
43
45
  s.summary = "Simple extensions to standard Ruby classes and useful helpers."
44
46
 
45
47
  if s.respond_to? :specification_version then
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.9
4
+ version: 0.0.10
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-07-05 00:00:00.000000000 Z
11
+ date: 2015-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -112,9 +112,11 @@ files:
112
112
  - lib/tarvit-helpers/extensions/colored_string.rb
113
113
  - lib/tarvit-helpers/extensions/counter.rb
114
114
  - lib/tarvit-helpers/modules/conditional_logger.rb
115
+ - lib/tarvit-helpers/modules/hash_presenter.rb
115
116
  - lib/tarvit-helpers/modules/non_shared_accessors.rb
116
117
  - lib/tarvit-helpers/modules/simple_crypt.rb
117
118
  - spec/extensions/counter_spec.rb
119
+ - spec/modules/hash_presenter_spec.rb
118
120
  - spec/modules/non_shared_accessors_spec.rb
119
121
  - spec/modules/simple_crypt_spec.rb
120
122
  - spec/spec_helper.rb
@@ -139,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
141
  version: '0'
140
142
  requirements: []
141
143
  rubyforge_project:
142
- rubygems_version: 2.2.2
144
+ rubygems_version: 2.4.8
143
145
  signing_key:
144
146
  specification_version: 4
145
147
  summary: Simple extensions to standard Ruby classes and useful helpers.