nav_node 0.0.2

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 13803c8d048a9d74a6de8ea555bd3b4078ca2e89
4
+ data.tar.gz: 835f5316b624a98c1074f08b44e7e9b7adef2f62
5
+ SHA512:
6
+ metadata.gz: a81c0491f8aee41ac36c673af2e7d85bf98c0c7d887862a11a253462d170cd9d5e55af27296ea04a8f3f10629adaec89cb83e61f31be6d908018d3dc8d9732fe
7
+ data.tar.gz: 9924b59342d0474305751b9dc775a0739601f7319b3c6d064abfee781fb7dc0eb5544a9598d2a70ca8f840771f8d2369d2aadd4a9764244d817b53150875daf0
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ *.swp
15
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nav_node.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Ye Li
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.
@@ -0,0 +1,69 @@
1
+ # NavNode
2
+
3
+ Nav for rails
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```
10
+ gem 'nav_node'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install nav_node
20
+
21
+ ## Usage
22
+
23
+ with erb
24
+
25
+ ```
26
+ <%= render_nav class: 'menu' do |node| %>
27
+ <%= node.+ "Node1", "/node1", class: "menu-node1", match: ['/node1', '/node2'] %>
28
+ <%= node.+ "Node2", "/node2", class: "menu-node2", match: '/node2/*' %>
29
+ <% end %>
30
+ ```
31
+
32
+ with Slim
33
+
34
+ ```
35
+
36
+ = render_nav class: 'menu' do |node|
37
+ - node.+ "Node1", "/node1", class: 'menu-node1', match: ['/node1', '/node2']
38
+ - node.+ "Node2", "/node2", class: 'menu-node2', match: '/node2/*'
39
+ ```
40
+
41
+ in "node1" page output
42
+
43
+ ```
44
+ <ul class="inline-menu">
45
+ <li class="first">
46
+ <a href="/node1"><span>Node1</span></a>
47
+ </li>
48
+ <li class="last active">
49
+ <a href="/node2"><span>Node2</span></a>
50
+ </li>
51
+ </ul>
52
+ ```
53
+
54
+ in "node2" page output
55
+
56
+ ```
57
+ <ul class="inline-menu">
58
+ <li class="first active">
59
+ <a href="/node1"><span>Node1</span></a>
60
+ </li>
61
+ <li class="last active">
62
+ <a href="/node2"><span>Node2</span></a>
63
+ </li>
64
+ </ul>
65
+ ```
66
+
67
+ ## License
68
+
69
+ Copyright © 2015, Ye Li. Released under the MIT License
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,12 @@
1
+ require "nav_node/version"
2
+ require 'nav_node/node'
3
+ require 'nav_node/nav_helper'
4
+
5
+ module NavNode
6
+ class << self
7
+ include NavNode::NavHelper
8
+ end
9
+ end
10
+
11
+
12
+ require 'nav_node/railtie' if defined?(Rails)
@@ -0,0 +1,28 @@
1
+ require 'active_support/core_ext/string/output_safety'
2
+
3
+ module NavNode
4
+ module NavHelper
5
+ def render_nav(options = {})
6
+ nav_node = NavNode::Node.new
7
+
8
+ yield(nav_node) if block_given?
9
+
10
+ if options[:tag]
11
+ if ["ul", "dl", "ol"].include?(options[:tag])
12
+ nav_tag = options[:tag]
13
+ end
14
+ end
15
+
16
+ nav_tag ||= "ul"
17
+ full_path = if respond_to?(:request)
18
+ request.fullpath
19
+ else
20
+ "/"
21
+ end
22
+
23
+ node_html = nav_node.parse_list(full_path, "").join("")
24
+ list_html = "<#{nav_tag} class='#{options[:class]}'>#{node_html}</#{nav_tag}>"
25
+ list_html.html_safe
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,67 @@
1
+ module NavNode
2
+ class Node
3
+ attr_accessor :list
4
+
5
+ def initialize
6
+ @list = []
7
+ end
8
+
9
+ def add(content, path = nil, options = {})
10
+ if !path.nil? || path != ""
11
+ @list << { type: :link, content: content, path: path, options: options }
12
+ else
13
+ @list << {content: content, options: options}
14
+ end
15
+ end
16
+ alias_method :+, :add
17
+
18
+ def parse_list(request_path, root_path = nil)
19
+ nodes = []
20
+ list.each_with_index do |value, index|
21
+ node_match = value[:options][:match]
22
+ node_type = value[:type]
23
+ node_path = value[:path]
24
+ node_class = []
25
+ node_content = ""
26
+ node_class<< "first" if index == 0
27
+ node_class << "last" if index == list.length - 1
28
+ if url_match(node_match, request_path, node_type, node_path)
29
+ node_class << "active"
30
+ end
31
+ if node_type == :link
32
+ link_class = " class='#{value[:class]}'" if value[:class]
33
+ nodes << "<li class='#{node_class.join(" ")}'><a href='#{node_path}'#{link_class}>#{value[:content]}</a></li>"
34
+ else
35
+ nodes << "<li class='#{node_class.join(" ")}'>#{value[:content]}</li>"
36
+ end
37
+ end
38
+ nodes
39
+ end
40
+
41
+ def url_match(node_match, request_path, node_type, node_path)
42
+ match = false
43
+ if node_match
44
+ if node_match.is_a?(String)
45
+ if request_path.match(%r"#{node_match}")
46
+ match = true
47
+ end
48
+ end
49
+ if node_match.is_a?(Array)
50
+ node_match.each do |nm|
51
+ if request_path.match(%r"#{nm}")
52
+ match = true
53
+ break
54
+ end
55
+ end
56
+ end
57
+ else
58
+ if node_type == :link
59
+ if request_path == node_path
60
+ match = true
61
+ end
62
+ end
63
+ end
64
+ return match
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,6 @@
1
+ module NavNode
2
+ class Railtie < Rails::Railtie
3
+ ActionView::Base.send :include, NavNode::NavHelper
4
+ end
5
+ end
6
+
@@ -0,0 +1,3 @@
1
+ module NavNode
2
+ VERSION = "0.0.2"
3
+ end
@@ -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 'nav_node/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nav_node"
8
+ spec.version = NavNode::VERSION
9
+ spec.authors = ["yee.li"]
10
+ spec.email = ["yeeli@outlook.com"]
11
+ spec.summary = %q{Navigate for rails}
12
+ spec.description = %q{Navigate for rails}
13
+ spec.homepage = "https://github.com/yeeli/nav_node"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rails"
26
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nav_node
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - yee.li
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
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.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
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: Navigate for rails
70
+ email:
71
+ - yeeli@outlook.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/nav_node.rb
82
+ - lib/nav_node/nav_helper.rb
83
+ - lib/nav_node/node.rb
84
+ - lib/nav_node/railtie.rb
85
+ - lib/nav_node/version.rb
86
+ - nav_node.gemspec
87
+ homepage: https://github.com/yeeli/nav_node
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.4.2
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Navigate for rails
111
+ test_files: []