better_helpers 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 177556adb63a634d36214319a642a9a5fc058817
4
+ data.tar.gz: 43af52bb3c31ac178b90728bad4ccfdcfbc37779
5
+ SHA512:
6
+ metadata.gz: 73bc5487a07a4c4e712e7e6f037a7cf5e3c47f610998ce91ed2346d4abab57d2a39dc1255344d6afd7adb96c1b9cc7a4dd26098de546e12dffbd16ca24a52686
7
+ data.tar.gz: d798c4bdba0e27e5cac1ce41795314dc0c9eae9681ae2a31f313d9aa997c8f1ca7c04f8faa76675d4363c4e40d2fccc49c68c745b8a2b89238dfc8ca58ab5e56
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ better_helpers
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0-p353
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in better_helpers.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 tulios
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # BetterHelpers
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/better_helpers.png)](http://badge.fury.io/rb/better_helpers)
4
+
5
+ It is a better way to organize and maintain your Rails helpers. It's provide a simple pattern to keep your helpers scoped, avoiding conflicts in the global namespace.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'better_helpers'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install better_helpers
20
+
21
+ ## Usage
22
+
23
+ BetterHelpers is magic free, it does nothing if you do not want. It is 100% compatible with your old helpers and will only activate under your command.
24
+
25
+ The usage is very simple, add the module **BetterHelpers::Base** in your helper module and create the methods inside the "DSL" **better_helpers**, like:
26
+
27
+ ```ruby
28
+ module ProfileHelper
29
+ include BetterHelpers::Base
30
+
31
+ better_helpers do
32
+
33
+ def username string
34
+ link_to(string, "/username/#{string}")
35
+ end
36
+
37
+ end
38
+ end
39
+ ```
40
+
41
+ It will create the namespace *profile_helper* based on the underscore name of module, to use in your views is as simple as:
42
+
43
+ ```sh
44
+ <%= profile_helper.username "name" %>
45
+ ```
46
+
47
+ It also work with modules, consider the example:
48
+
49
+ ```ruby
50
+ module Admin
51
+ module Users
52
+ module ProfileHelper
53
+ include BetterHelpers::Base
54
+
55
+ better_helpers do
56
+
57
+ def username string
58
+ "admin #{string}"
59
+ end
60
+
61
+ end
62
+ end
63
+ end
64
+ end
65
+ ```
66
+
67
+ ```sh
68
+ <%= admin.users.profile_helper.username "name" %>
69
+ ```
70
+
71
+ By default, it will create the whole hierarchy but it is possible to define the namespace, check the section **Defining the namespace** to understand how to proceed.
72
+
73
+ BetterHelpers will include in the namespace just the methods inside the "DSL", you could keep your "old fashion" helpers inside the module without problems.
74
+
75
+ ### Defining the namespace
76
+
77
+ Just pass a symbol to the "DSL" and it's done.
78
+
79
+ ```ruby
80
+ module HomeHelper
81
+ include BetterHelpers::Base
82
+
83
+ better_helpers :custom_namespace do
84
+
85
+ def title
86
+ "<h1>HOME</h1>".html_safe
87
+ end
88
+
89
+ end
90
+ end
91
+ ```
92
+
93
+ ```sh
94
+ <%= custom_namespace.title %>
95
+ ```
96
+
97
+ ```ruby
98
+ module MyModule
99
+ module MyHelper
100
+ include BetterHelpers::Base
101
+
102
+ better_helpers :custom do
103
+
104
+ def helper_method
105
+ end
106
+
107
+ end
108
+ end
109
+ end
110
+ ```
111
+
112
+ ```sh
113
+ <%= custom.helper_method %>
114
+ ```
115
+
116
+ ## Contributing
117
+
118
+ 1. Fork it
119
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
120
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
121
+ 4. Push to the branch (`git push origin my-new-feature`)
122
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'better_helpers/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "better_helpers"
8
+ s.version = BetterHelpers::VERSION
9
+ s.authors = ["tulios"]
10
+ s.email = ["ornelas.tulio@gmail.com"]
11
+ s.description = %q{It is a better way to organize and maintain your Rails helpers. It's provide a simple pattern to keep your helpers scoped, avoiding conflicts in the global namespace}
12
+ s.summary = %q{It is a better way to organize and maintain your Rails helpers}
13
+ s.homepage = "https://github.com/tulios/better_helpers"
14
+ s.license = "MIT"
15
+
16
+ s.files = `git ls-files`.split($/)
17
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency "railties", ">= 3.2"
22
+
23
+ s.add_development_dependency "bundler", "~> 1.3"
24
+ s.add_development_dependency "rake"
25
+ s.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,10 @@
1
+ require "active_support/core_ext/string/inflections"
2
+ require "action_view"
3
+
4
+ require "better_helpers/version"
5
+ require "better_helpers/base"
6
+ require "better_helpers/namespace_to_hash"
7
+ require "better_helpers/hash_to_object"
8
+
9
+ module BetterHelpers
10
+ end
@@ -0,0 +1,30 @@
1
+ module BetterHelpers
2
+ module Base
3
+
4
+ def self.included base
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def better_helpers namespace = nil, &block
10
+ helper_class = Class.new(&block)
11
+ helper_class.class_eval do
12
+ include ActionView::Helpers
13
+ include ActionView::Context
14
+ extend ActionView::Helpers
15
+ extend ActionView::Context
16
+ end
17
+
18
+ namespace ||= self.to_s.underscore
19
+ names = namespace.to_s.split("/")
20
+ name = names.shift
21
+
22
+ hash = NamespaceToHash.new(helper_class, names).perform
23
+ value = HashToObject.new(hash).perform
24
+
25
+ self.send(:define_method, name) { value }
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,28 @@
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
@@ -0,0 +1,25 @@
1
+ module BetterHelpers
2
+ class NamespaceToHash
3
+ def initialize helper_class, names
4
+ @helper_class = helper_class
5
+ @names = names.clone
6
+ end
7
+
8
+ def perform
9
+ generate_hash @names.shift, @names, {}
10
+ end
11
+
12
+ private
13
+ def generate_hash name, names, hash
14
+ if names.empty?
15
+ hash[name] = @helper_class.new
16
+
17
+ else
18
+ hash[name] = {}
19
+ generate_hash(names.shift, names, hash[name])
20
+ end
21
+
22
+ hash
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module BetterHelpers
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,61 @@
1
+ require "spec_helper"
2
+
3
+ require "fixtures/some_helper"
4
+ require "fixtures/another_helper"
5
+ require "fixtures/inside_module_helper"
6
+ require "fixtures/inside_module_custom_helper"
7
+
8
+ describe BetterHelpers::Base do
9
+
10
+ let :helpers do
11
+ Module.new do
12
+ extend SomeHelper
13
+ extend AnotherHelper
14
+ extend InsideModuleHelper::A::B
15
+ extend InsideModuleCustomHelper::A::B
16
+ end
17
+ end
18
+
19
+ it "should not conflict with old helpers" do
20
+ helpers.should respond_to :global_helper_method
21
+ helpers.global_helper_method.should eql "test5"
22
+ end
23
+
24
+ describe "when included" do
25
+ it "should include the class method 'better_helpers'" do
26
+ SomeHelper.should respond_to :better_helpers
27
+ AnotherHelper.should respond_to :better_helpers
28
+ end
29
+ end
30
+
31
+ describe "::better_helpers" do
32
+ describe "without arguments" do
33
+ it "should include the helper methods into a 'namespace' with the underscore name of the module" do
34
+ helpers.should respond_to SomeHelper.to_s.underscore
35
+ helpers.some_helper.helper_method.should eql "test"
36
+ end
37
+ end
38
+
39
+ describe "with namespace argument" do
40
+ it "should include the helper methods into the defined 'namespace'" do
41
+ helpers.should respond_to :another
42
+ helpers.another.helper_method.should eql "test2"
43
+ end
44
+ end
45
+
46
+ context "for helpers inside modules" do
47
+ it "should include as cascade methods" do
48
+ helpers.should respond_to :inside_module_helper
49
+ helpers.inside_module_helper.should respond_to :a
50
+ helpers.inside_module_helper.a.should respond_to :b
51
+ helpers.inside_module_helper.a.b.helper_method.should eql "test3"
52
+ end
53
+
54
+ it "should allow namespace configuration" do
55
+ helpers.should respond_to :custom_helper
56
+ helpers.custom_helper.helper_method.should eql "test4"
57
+ end
58
+ end
59
+ end
60
+
61
+ end
@@ -0,0 +1,36 @@
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
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ describe BetterHelpers::NamespaceToHash do
4
+
5
+ let :namespace do
6
+ ["a", "b", "c"]
7
+ end
8
+
9
+ subject do
10
+ BetterHelpers::NamespaceToHash.new(String, namespace).perform
11
+ end
12
+
13
+ it "should generate a nested hash resulting in the given object" do
14
+ subject.should be_an_instance_of Hash
15
+ subject.keys.should eql ["a"]
16
+
17
+ subject["a"].should be_an_instance_of Hash
18
+ subject["a"].keys.should eql ["b"]
19
+
20
+ subject["a"]["b"].should be_an_instance_of Hash
21
+ subject["a"]["b"].keys.should eql ["c"]
22
+ subject["a"]["b"]["c"].should be_an_instance_of String
23
+ end
24
+
25
+ end
@@ -0,0 +1,15 @@
1
+ module AnotherHelper
2
+ include BetterHelpers::Base
3
+
4
+ def global_helper_method
5
+ "test5"
6
+ end
7
+
8
+ better_helpers :another do
9
+
10
+ def helper_method
11
+ "test2"
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module InsideModuleCustomHelper
2
+ module A
3
+ module B
4
+ include BetterHelpers::Base
5
+
6
+ better_helpers :custom_helper do
7
+
8
+ def helper_method
9
+ "test4"
10
+ end
11
+
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module InsideModuleHelper
2
+ module A
3
+ module B
4
+ include BetterHelpers::Base
5
+
6
+ better_helpers do
7
+
8
+ def helper_method
9
+ "test3"
10
+ end
11
+
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module SomeHelper
2
+ include BetterHelpers::Base
3
+
4
+ better_helpers do
5
+
6
+ def helper_method
7
+ "test"
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+
3
+ $:.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
4
+
5
+ require "better_helpers"
6
+
7
+ # This file was generated by the `rspec --init` command. Conventionally, all
8
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
9
+ # Require this file using `require "spec_helper"` to ensure that it is only
10
+ # loaded once.
11
+ #
12
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
13
+ RSpec.configure do |config|
14
+ config.treat_symbols_as_metadata_keys_with_true_values = true
15
+ config.run_all_when_everything_filtered = true
16
+ config.filter_run :focus
17
+
18
+ # Run specs in random order to surface order dependencies. If you find an
19
+ # order dependency and want to debug it, you can fix the order by providing
20
+ # the seed, which is printed after each run.
21
+ # --seed 1234
22
+ config.order = 'random'
23
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: better_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - tulios
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: It is a better way to organize and maintain your Rails helpers. It's
70
+ provide a simple pattern to keep your helpers scoped, avoiding conflicts in the
71
+ global namespace
72
+ email:
73
+ - ornelas.tulio@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - .gitignore
79
+ - .rspec
80
+ - .ruby-gemset
81
+ - .ruby-version
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - better_helpers.gemspec
87
+ - lib/better_helpers.rb
88
+ - lib/better_helpers/base.rb
89
+ - lib/better_helpers/hash_to_object.rb
90
+ - lib/better_helpers/namespace_to_hash.rb
91
+ - lib/better_helpers/version.rb
92
+ - spec/better_helpers/base_spec.rb
93
+ - spec/better_helpers/hash_to_object_spec.rb
94
+ - spec/better_helpers/namespace_to_hash_spec.rb
95
+ - spec/fixtures/another_helper.rb
96
+ - spec/fixtures/inside_module_custom_helper.rb
97
+ - spec/fixtures/inside_module_helper.rb
98
+ - spec/fixtures/some_helper.rb
99
+ - spec/spec_helper.rb
100
+ homepage: https://github.com/tulios/better_helpers
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.2.1
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: It is a better way to organize and maintain your Rails helpers
124
+ test_files:
125
+ - spec/better_helpers/base_spec.rb
126
+ - spec/better_helpers/hash_to_object_spec.rb
127
+ - spec/better_helpers/namespace_to_hash_spec.rb
128
+ - spec/fixtures/another_helper.rb
129
+ - spec/fixtures/inside_module_custom_helper.rb
130
+ - spec/fixtures/inside_module_helper.rb
131
+ - spec/fixtures/some_helper.rb
132
+ - spec/spec_helper.rb