navigator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ = v0.1.0
2
+
3
+ * Initial version.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 {Red Alchemist}[http://www.redalchemist.com].
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,99 @@
1
+ = Overview
2
+
3
+ {<img src="https://secure.travis-ci.org/bkuhlmann/navigator.png" />}[http://travis-ci.org/bkuhlmann/navigator]
4
+
5
+ Enhances Rails with a DSL for menu navigation.
6
+
7
+ = Features
8
+
9
+ * A simple DSL for creating navigation menus.
10
+ * Supports sub-menus, nested tags, HTML attributes, etc.
11
+ * Supports the following HTML tags: ul, li, a, b, em, s, small, span, strong, sub, and sup.
12
+ * Provides the "item" convenience method which combines the "li" and "a" HTML tags into a single method for less typing.
13
+
14
+ = Requirements
15
+
16
+ 1. {Ruby 1.9.x}[http://www.ruby-lang.org].
17
+ 2. {Ruby on Rails}[http://rubyonrails.org].
18
+
19
+ = Setup
20
+
21
+ Type the following from the command line to install:
22
+
23
+ gem install navigator
24
+
25
+ Add the following to your Gemfile:
26
+
27
+ gem "navigator"
28
+
29
+ = Usage
30
+
31
+ The following are examples using the render_navigation view helper:
32
+
33
+ == Unordered List (simple)
34
+
35
+ render_navigation do
36
+ item "Dashboard", "/dashboard"
37
+ item "News", "/posts"
38
+ end
39
+
40
+ Yields:
41
+
42
+ <ul>
43
+ <li><a href="/dashboard">Dashboard</a></li>
44
+ <li><a href="/posts">Posts</a></li>
45
+ </ul>
46
+
47
+ == Unordered List (with attributes)
48
+
49
+ render_navigation "ul", class: "nav" do
50
+ item "Dashboard", "/dashboard", class: "active"
51
+ item "News", "/posts"
52
+ end
53
+
54
+ Yields:
55
+
56
+ <ul class="nav">
57
+ <li class="active"><a href="/dashboard">Dashboard</a></li>
58
+ <li><a href="/posts">Posts</a></li>
59
+ </ul>
60
+
61
+ == Nav (with links)
62
+
63
+ render_navigation "nav" do
64
+ a "Dashboard", href: "/dashboard"
65
+ a "News", href: "/posts"
66
+ end
67
+
68
+ Yields:
69
+
70
+ <nav>
71
+ <a href="/dashboard">Dashboard</a>
72
+ <a href="/posts">Posts</a>
73
+ </nav>
74
+
75
+ = Tests
76
+
77
+ To test, do the following:
78
+
79
+ 1. cd to the gem root.
80
+ 2. bundle install
81
+ 3. bundle exec rspec spec
82
+
83
+ = Contributions
84
+
85
+ Please log all feedback/issues via {GitHub Issues}[https://github.com/bkuhlmann/navigator/issues]. Thanks.
86
+
87
+ = Credits
88
+
89
+ Developed by {Brooke Kuhlmann}[http://www.redalchemist.com] at {Red Alchemist}[http://www.redalchemist.com]
90
+
91
+ = License
92
+
93
+ Copyright (c) 2012 {Red Alchemist}[http://www.redalchemist.com].
94
+ Read the LICENSE for details.
95
+
96
+ = History
97
+
98
+ Read the CHANGELOG for details.
99
+ Built with Gemsmith[https://github.com/bkuhlmann/gemsmith].
@@ -0,0 +1,14 @@
1
+ require File.join File.dirname(__FILE__), "navigator", "tag.rb"
2
+ require File.join File.dirname(__FILE__), "navigator", "menu.rb"
3
+
4
+ # Rails Enhancements
5
+ if defined? Rails
6
+ # Dependencies
7
+ require File.join File.dirname(__FILE__), "navigator", "version.rb"
8
+ require File.join File.dirname(__FILE__), "navigator", "action_view", "instance_methods.rb"
9
+
10
+ # View
11
+ if defined? ActionView
12
+ ActionView::Base.send :include, Navigator::ActionView
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module Navigator
2
+ module ActionView
3
+ def self.included base
4
+ base.send :include, InstanceMethods
5
+ end
6
+
7
+ module InstanceMethods
8
+ # Renders a navigation menu.
9
+ # ==== Parameters
10
+ # * +tag+ - Optional. The menu tag. Default: "ul"
11
+ # * +attributes+ - Optional. The menu tag attributes. Default: {}
12
+ # * +block+ - Optional. The code block.
13
+ def render_navigation tag = "ul", attributes = {}, &block
14
+ raw Menu.new(self, tag, attributes, &block).render
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,82 @@
1
+ module Navigator
2
+ # Builds and renders HTML menus. Examples:
3
+ # Example 1:
4
+ # <ul>
5
+ # <li>One</li>
6
+ # <li>Two</li>
7
+ # </ul>
8
+ # Example 2:
9
+ # <nav>
10
+ # <a href="/one">One</a>
11
+ # <a href="/two">Two</a>
12
+ # </nav>
13
+ class Menu
14
+ attr_accessor :options
15
+
16
+ # Initializes the menu.
17
+ # ==== Parameters
18
+ # * +template+ - Required. The view template.
19
+ # * +tag+ - Optional. The menu tag. Default: "ul"
20
+ # * +attributes+ - Optional. The tag attributes. Default: {}
21
+ # * +options+ - Optional. The menu options. Default: {active: "active"}
22
+ # * +block+ - Optional. The code block.
23
+ def initialize template, tag = "ul", attributes = {}, options = {}, &block
24
+ @template = template
25
+ @tag = Tag.new tag, nil, attributes
26
+ @options = options.reverse_merge! active: "active"
27
+ @items = []
28
+ instance_eval(&block) if block_given?
29
+ end
30
+
31
+ # Adds a HTML tag to the menu.
32
+ # ==== Parameters
33
+ # * +name+ - Required. The tag name.
34
+ # * +content+ - Optional. The tag content. Default: nil
35
+ # * +attributes+ - Optional. The HTML attributes (hash) for the tag. Default: {}
36
+ # * +block+ - Optional. The tag code block.
37
+ def add name, content = nil, attributes = {}, &block
38
+ tag = Tag.new name, content, attributes
39
+ if block_given?
40
+ @items << tag.prefix
41
+ @items << tag.content
42
+ instance_eval(&block)
43
+ @items << tag.suffix
44
+ else
45
+ @items << tag.render
46
+ end
47
+ end
48
+
49
+ # Build a list item tag (li) that contains a link tag (a).
50
+ # This is a convenience method for adding simple list items as links.
51
+ # ==== Parameters
52
+ # * +content+ - Required. The content of the link (not the item!)
53
+ # * +url+ - Required. The link url.
54
+ # * +item_attributes+ - Optional. The item HTML attributes. Default: {}
55
+ # * +link_attributes+ - Optional. The link HTML attributes. Default: {}
56
+ def item content, url, item_attributes = {}, link_attributes = {}
57
+ add "li", nil, item_attributes do
58
+ add "a", content, {href: url}.reverse_merge!(link_attributes)
59
+ end
60
+ end
61
+
62
+ # Delegates missing method names to the .add method for supported tags only.
63
+ # Supported Tags: ul, li, a, b, em, s, small, span, strong, sub, and sup.
64
+ # All other method calls are sent to the view template for processing (if apt).
65
+ # ==== Parameters
66
+ # * +name+ - Required. The method name.
67
+ # * +args+ - Optional. The method arguments.
68
+ # * +block+ - Optional. The code block.
69
+ def method_missing name, *args, &block
70
+ if name.to_s =~ /^(ul|li|a|b|em|s|small|span|strong|sub|sup)$/
71
+ add(*args.unshift(name), &block)
72
+ else
73
+ @template.send name, *args
74
+ end
75
+ end
76
+
77
+ # Renders the menu.
78
+ def render
79
+ [@tag.prefix, @tag.content, @items.compact.join(''), @tag.suffix].compact * ''
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,36 @@
1
+ module Navigator
2
+ # Represents a HTML tag.
3
+ class Tag
4
+ attr_accessor :name, :content, :attributes
5
+
6
+ # Initalizes the HTML tag.
7
+ # ==== Parameters
8
+ # * +name+ - Required. The tag name.
9
+ # * +content+ - Optional. The tag content. Default: nil
10
+ # * +attributes+ - Optional. The tag attributes. Default: {}
11
+ def initialize name, content = nil, attributes = {}
12
+ @name, @content, @attributes = name, content, attributes
13
+ end
14
+
15
+ # Answers the attributes as fully formed HTML attributes for the tag.
16
+ def html_attributes
17
+ @attributes.map {|key, value| "#{key}=\"#{value}\""}.compact * ' '
18
+ end
19
+
20
+ # Answers the HTML tag prefix (i.e. the opening tag). Example: <li>.
21
+ def prefix
22
+ attrs = html_attributes.empty? ? nil : " #{html_attributes}"
23
+ ["<#{@name}", attrs, '>'].compact * ''
24
+ end
25
+
26
+ # Answers the HTML tag suffix (i.e. the closing tag). Example: </li>.
27
+ def suffix
28
+ "</#{@name}>"
29
+ end
30
+
31
+ # Renders the fully assembled HTML tag with attributes and content (if apt).
32
+ def render
33
+ [prefix, @content, suffix].compact * ''
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Navigator
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: navigator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brooke Kuhlmann
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70219033230180 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ - - <
23
+ - !ruby/object:Gem::Version
24
+ version: 4.0.0
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: *70219033230180
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: &70219033229380 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: *70219033229380
39
+ - !ruby/object:Gem::Dependency
40
+ name: rspec-rails
41
+ requirement: &70219033228680 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: *70219033228680
50
+ description: Enhances Rails with a DSL for menu navigation complete with sub-menus,
51
+ nested tags, HTML attributes, etc.
52
+ email: brooke@redalchemist.com
53
+ executables: []
54
+ extensions: []
55
+ extra_rdoc_files:
56
+ - README.rdoc
57
+ - CHANGELOG.rdoc
58
+ - LICENSE.rdoc
59
+ files:
60
+ - lib/navigator/action_view/instance_methods.rb
61
+ - lib/navigator/menu.rb
62
+ - lib/navigator/tag.rb
63
+ - lib/navigator/version.rb
64
+ - lib/navigator.rb
65
+ - README.rdoc
66
+ - CHANGELOG.rdoc
67
+ - LICENSE.rdoc
68
+ homepage: http://www.redalchemist.com
69
+ licenses:
70
+ - MIT
71
+ post_install_message: ! '(W): www.redalchemist.com. (T): @ralchemist.'
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ~>
79
+ - !ruby/object:Gem::Version
80
+ version: 1.9.0
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.15
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Enhances Rails with a DSL for menu navigation.
93
+ test_files: []