better_helpers 0.1.1 → 0.1.2

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: 177556adb63a634d36214319a642a9a5fc058817
4
- data.tar.gz: 43af52bb3c31ac178b90728bad4ccfdcfbc37779
3
+ metadata.gz: 59e0dcdc57a82caa0ac352e95c921c38cb5c1538
4
+ data.tar.gz: be6447d2ce60248ec87aebcef3eaf43fdb760f5e
5
5
  SHA512:
6
- metadata.gz: 73bc5487a07a4c4e712e7e6f037a7cf5e3c47f610998ce91ed2346d4abab57d2a39dc1255344d6afd7adb96c1b9cc7a4dd26098de546e12dffbd16ca24a52686
7
- data.tar.gz: d798c4bdba0e27e5cac1ce41795314dc0c9eae9681ae2a31f313d9aa997c8f1ca7c04f8faa76675d4363c4e40d2fccc49c68c745b8a2b89238dfc8ca58ab5e56
6
+ metadata.gz: 6c2194c8a920d73f7dc3e045b14696903eabf754907e7670f6569e7cc51183266eb0d2d75b6a3f8b92734ec935864a1f632fa2065a96edc5a86816ae3279a253
7
+ data.tar.gz: 0adac9536f828fa06408acceb23e7bc3af7e3e540a34bc440daf63ca5ed8f7080ebc96d62538d78913b133a5c6f5bf324dc6fb8d8db539aeaf786c69e6d039ae
@@ -4,7 +4,7 @@ require "action_view"
4
4
  require "better_helpers/version"
5
5
  require "better_helpers/base"
6
6
  require "better_helpers/namespace_to_hash"
7
- require "better_helpers/hash_to_object"
7
+ require "better_helpers/hash_hierarchy_to_class"
8
8
 
9
9
  module BetterHelpers
10
10
  end
@@ -6,6 +6,10 @@ module BetterHelpers
6
6
  end
7
7
 
8
8
  module ClassMethods
9
+ def self.extended base
10
+ @@BetterHelpersMasterHelper ||= Class.new
11
+ end
12
+
9
13
  def better_helpers namespace = nil, &block
10
14
  helper_class = Class.new(&block)
11
15
  helper_class.class_eval do
@@ -20,7 +24,7 @@ module BetterHelpers
20
24
  name = names.shift
21
25
 
22
26
  hash = NamespaceToHash.new(helper_class, names).perform
23
- value = HashToObject.new(hash).perform
27
+ value = HashHierarchyToClass.new(hash, @@BetterHelpersMasterHelper).apply
24
28
 
25
29
  self.send(:define_method, name) { value }
26
30
  end
@@ -0,0 +1,39 @@
1
+ module BetterHelpers
2
+ class HashHierarchyToClass
3
+
4
+ def initialize hash, parent_class
5
+ @hash = hash
6
+ @parent_class = parent_class
7
+ end
8
+
9
+ def apply
10
+ value = apply_to_class @hash, @parent_class.new
11
+ @hash.keys.first.nil? ? value : @parent_class.new
12
+ end
13
+
14
+ private
15
+ def apply_to_class obj, parent_obj
16
+ if obj.is_a? Hash
17
+ key = obj.keys.first
18
+ value = obj[key]
19
+
20
+ return value if key.nil?
21
+
22
+ if parent_obj.respond_to?(key)
23
+ apply_to_class value, parent_obj.send(key)
24
+ else
25
+
26
+ klass = Class.new
27
+ instance = klass.new
28
+ return_obj = apply_to_class value, instance
29
+
30
+ parent_obj.class.send(:define_method, key) { return_obj }
31
+ return parent_obj
32
+ end
33
+ end
34
+
35
+ obj
36
+ end
37
+
38
+ end
39
+ end
@@ -1,3 +1,3 @@
1
1
  module BetterHelpers
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -56,6 +56,28 @@ describe BetterHelpers::Base do
56
56
  helpers.custom_helper.helper_method.should eql "test4"
57
57
  end
58
58
  end
59
+
60
+ context "for helpers sharing a module" do
61
+ let :helpers do
62
+ Module.new do
63
+ extend InsideModuleHelper::A::B
64
+ extend InsideModuleHelper::A::C
65
+ end
66
+ end
67
+
68
+ it "should include both helpers in the same module" do
69
+ helpers.should respond_to :inside_module_helper
70
+ helpers.inside_module_helper.should respond_to :a
71
+ helpers.inside_module_helper.a.should respond_to :b
72
+ helpers.inside_module_helper.a.should respond_to :c
73
+
74
+ helpers.inside_module_helper.a.b.should respond_to :helper_method
75
+ helpers.inside_module_helper.a.c.should respond_to :helper_method
76
+
77
+ helpers.inside_module_helper.a.b.helper_method.should eql "test3"
78
+ helpers.inside_module_helper.a.c.helper_method.should eql "test6"
79
+ end
80
+ end
59
81
  end
60
82
 
61
83
  end
@@ -0,0 +1,131 @@
1
+ require "spec_helper"
2
+
3
+ describe BetterHelpers::HashHierarchyToClass do
4
+
5
+ let :parent_class do
6
+ Class.new
7
+ end
8
+
9
+ let :hash do
10
+ {a: {b: {c: 1}}}
11
+ end
12
+
13
+ let :apply do
14
+ BetterHelpers::HashHierarchyToClass.new(hash, parent_class).apply
15
+ end
16
+
17
+ describe "when modifying the class" do
18
+ subject { parent_class.new }
19
+
20
+ context "for unique modules" do
21
+ before { apply }
22
+
23
+ it "should include the methods into the parent class" do
24
+ apply
25
+ subject.should respond_to :a
26
+ subject.should_not respond_to :b
27
+ subject.should_not respond_to :c
28
+
29
+ subject.a.should respond_to :b
30
+ subject.a.should_not respond_to :a
31
+ subject.a.should_not respond_to :c
32
+
33
+ subject.a.b.should respond_to :c
34
+ subject.a.b.should_not respond_to :a
35
+ subject.a.b.should_not respond_to :b
36
+
37
+ subject.a.b.c.should eql 1
38
+ end
39
+
40
+ it "should allow new instances with the same methods" do
41
+ apply
42
+ instance2 = parent_class.new
43
+ instance3 = parent_class.new
44
+
45
+ [instance2, instance3].each do |instance|
46
+ instance.should respond_to :a
47
+ instance.should_not respond_to :b
48
+ instance.should_not respond_to :c
49
+
50
+ instance.a.should respond_to :b
51
+ instance.a.should_not respond_to :a
52
+ instance.a.should_not respond_to :c
53
+
54
+ instance.a.b.should respond_to :c
55
+ instance.a.b.should_not respond_to :a
56
+ instance.a.b.should_not respond_to :b
57
+
58
+ instance.a.b.c.should eql 1
59
+ end
60
+ end
61
+ end
62
+
63
+ context "for different modules with the same parent" do
64
+ let :hash1 do
65
+ {a: {b: {d: 1}}}
66
+ end
67
+
68
+ let :hash2 do
69
+ {a: {c: {d: 2}}}
70
+ end
71
+
72
+ before do
73
+ BetterHelpers::HashHierarchyToClass.new(hash1, parent_class).apply
74
+ BetterHelpers::HashHierarchyToClass.new(hash2, parent_class).apply
75
+ end
76
+
77
+ it "should define the method in the parent class" do
78
+ subject.should respond_to :a
79
+ subject.should_not respond_to :b
80
+ subject.should_not respond_to :c
81
+ subject.should_not respond_to :d
82
+
83
+ subject.a.should respond_to :b
84
+ subject.a.should respond_to :c
85
+ subject.a.should_not respond_to :a
86
+ subject.a.should_not respond_to :d
87
+
88
+ subject.a.b.should respond_to :d
89
+ subject.a.b.should_not respond_to :a
90
+ subject.a.b.should_not respond_to :b
91
+ subject.a.b.should_not respond_to :c
92
+
93
+ subject.a.b.d.should eql 1
94
+
95
+ subject.a.c.should respond_to :d
96
+ subject.a.c.should_not respond_to :a
97
+ subject.a.c.should_not respond_to :b
98
+ subject.a.c.should_not respond_to :c
99
+
100
+ subject.a.c.d.should eql 2
101
+ end
102
+ end
103
+ end
104
+
105
+ describe "when returning value" do
106
+ subject { apply }
107
+
108
+ context "when key is nil" do
109
+ let :hash do
110
+ {nil => 1}
111
+ end
112
+
113
+ it "should return the value" do
114
+ subject.should eql 1
115
+ end
116
+ end
117
+
118
+ context "when exist some hierarchy" do
119
+ let :hash do
120
+ {a: {b: 1}}
121
+ end
122
+
123
+ it "should return an instance of a callable object" do
124
+ subject.should respond_to :a
125
+ subject.a.should respond_to :b
126
+ subject.a.b.should eql 1
127
+ end
128
+ end
129
+ end
130
+
131
+ end
@@ -11,5 +11,17 @@ module InsideModuleHelper
11
11
 
12
12
  end
13
13
  end
14
+
15
+ module C
16
+ include BetterHelpers::Base
17
+
18
+ better_helpers do
19
+
20
+ def helper_method
21
+ "test6"
22
+ end
23
+
24
+ end
25
+ end
14
26
  end
15
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - tulios
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-07 00:00:00.000000000 Z
11
+ date: 2014-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -86,11 +86,11 @@ files:
86
86
  - better_helpers.gemspec
87
87
  - lib/better_helpers.rb
88
88
  - lib/better_helpers/base.rb
89
- - lib/better_helpers/hash_to_object.rb
89
+ - lib/better_helpers/hash_hierarchy_to_class.rb
90
90
  - lib/better_helpers/namespace_to_hash.rb
91
91
  - lib/better_helpers/version.rb
92
92
  - spec/better_helpers/base_spec.rb
93
- - spec/better_helpers/hash_to_object_spec.rb
93
+ - spec/better_helpers/hash_hierarchy_to_class_spec.rb
94
94
  - spec/better_helpers/namespace_to_hash_spec.rb
95
95
  - spec/fixtures/another_helper.rb
96
96
  - spec/fixtures/inside_module_custom_helper.rb
@@ -117,13 +117,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
117
  version: '0'
118
118
  requirements: []
119
119
  rubyforge_project:
120
- rubygems_version: 2.2.1
120
+ rubygems_version: 2.1.11
121
121
  signing_key:
122
122
  specification_version: 4
123
123
  summary: It is a better way to organize and maintain your Rails helpers
124
124
  test_files:
125
125
  - spec/better_helpers/base_spec.rb
126
- - spec/better_helpers/hash_to_object_spec.rb
126
+ - spec/better_helpers/hash_hierarchy_to_class_spec.rb
127
127
  - spec/better_helpers/namespace_to_hash_spec.rb
128
128
  - spec/fixtures/another_helper.rb
129
129
  - spec/fixtures/inside_module_custom_helper.rb
@@ -1,28 +0,0 @@
1
- module BetterHelpers
2
- class HashToObject
3
- def initialize obj
4
- @obj = obj
5
- end
6
-
7
- def perform
8
- generate_obj @obj
9
- end
10
-
11
- private
12
- def generate_obj obj
13
- if obj.is_a? Hash
14
- key = obj.keys.first
15
- value = obj[key]
16
-
17
- return value if key.nil?
18
- return_obj = generate_obj value
19
-
20
- c = Class.new
21
- c.send(:define_method, key) { return_obj }
22
- return c.new
23
- end
24
-
25
- obj
26
- end
27
- end
28
- end
@@ -1,36 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe BetterHelpers::HashToObject do
4
-
5
- let :hash do
6
- {
7
- a: {
8
- b: {
9
- c: 1
10
- }
11
- }
12
- }
13
- end
14
-
15
- subject do
16
- BetterHelpers::HashToObject.new(hash).perform
17
- end
18
-
19
- it "should generate nested objects following the hash structure" do
20
- subject.should respond_to :a
21
- subject.a.should respond_to :b
22
- subject.a.b.should respond_to :c
23
- subject.a.b.c.should eql 1
24
- end
25
-
26
- describe "when key is nil" do
27
- let :hash do
28
- {nil => 1}
29
- end
30
-
31
- it "should return the value" do
32
- subject.should eql 1
33
- end
34
- end
35
-
36
- end