nested_list 0.0.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.
- data/Gemfile +6 -0
- data/Gemfile.lock +20 -0
- data/Rakefile +13 -0
- data/lib/nested_list/nested_select.rb +124 -0
- data/lib/nested_list/tasks.rb +28 -0
- data/lib/nested_list/version.rb +3 -0
- data/lib/nested_list.rb +81 -0
- data/nested_list.gemspec +21 -0
- metadata +64 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
nested_list (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
active_support (3.0.0)
|
10
|
+
activesupport (= 3.0.0)
|
11
|
+
activesupport (3.0.0)
|
12
|
+
rake (0.9.2)
|
13
|
+
|
14
|
+
PLATFORMS
|
15
|
+
ruby
|
16
|
+
|
17
|
+
DEPENDENCIES
|
18
|
+
active_support
|
19
|
+
nested_list!
|
20
|
+
rake (= 0.9.2)
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require 'bundler'
|
3
|
+
include Rake::DSL
|
4
|
+
require 'rake/tasklib'
|
5
|
+
require 'nested_list/tasks'
|
6
|
+
include NestedList::Task
|
7
|
+
|
8
|
+
Bundler::GemHelper.install_tasks
|
9
|
+
|
10
|
+
desc "Create a test html page to demonstrate nested list use."
|
11
|
+
task :nested_list_html_page do
|
12
|
+
generate_html
|
13
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
module NestedSelect
|
2
|
+
|
3
|
+
class Item
|
4
|
+
attr_accessor :name, :parent, :id
|
5
|
+
@@use_spaces = true
|
6
|
+
def initialize(name, id)
|
7
|
+
@name = name.to_s.strip
|
8
|
+
@id = id.to_s
|
9
|
+
@parent = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def spaces
|
13
|
+
space = ""
|
14
|
+
if @@use_spaces
|
15
|
+
(parents_count-1).to_i.times{|i| space+=' '}
|
16
|
+
end
|
17
|
+
space
|
18
|
+
end
|
19
|
+
|
20
|
+
def find_by_name(name_tmp)
|
21
|
+
if !self.name.empty? && self.full_name == name_tmp
|
22
|
+
self.parent
|
23
|
+
else
|
24
|
+
false
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def parents_count
|
29
|
+
item = self
|
30
|
+
count = 0
|
31
|
+
while item = item.parent
|
32
|
+
count += 1
|
33
|
+
end
|
34
|
+
count
|
35
|
+
end
|
36
|
+
|
37
|
+
def items_count
|
38
|
+
1
|
39
|
+
end
|
40
|
+
|
41
|
+
def full_name
|
42
|
+
unless @full_name_str
|
43
|
+
item = self
|
44
|
+
full_name = []
|
45
|
+
full_name << name
|
46
|
+
while item = item.parent
|
47
|
+
full_name << item.name
|
48
|
+
end
|
49
|
+
# Remove last general for all element
|
50
|
+
full_name.pop
|
51
|
+
full_name_str = full_name.reverse!.join(" ")
|
52
|
+
end
|
53
|
+
@full_name_str ||= full_name_str.parameterize
|
54
|
+
end
|
55
|
+
|
56
|
+
def wrap
|
57
|
+
self.id && !self.id.empty? ? "<option value='#{self.id}' class='level_#{parents_count} item'>#{spaces}#{name.humanize}</option>" : ""
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
class List < Item
|
63
|
+
|
64
|
+
def initialize(name, id)
|
65
|
+
super(name, id)
|
66
|
+
@items = []
|
67
|
+
end
|
68
|
+
|
69
|
+
def node
|
70
|
+
@items.first
|
71
|
+
end
|
72
|
+
|
73
|
+
def branches
|
74
|
+
@branches = @items[1..@items.size]
|
75
|
+
@branches
|
76
|
+
end
|
77
|
+
|
78
|
+
def add_item(item)
|
79
|
+
@items << item
|
80
|
+
item.parent = self
|
81
|
+
end
|
82
|
+
|
83
|
+
def remove_item(item)
|
84
|
+
@items.delete(item)
|
85
|
+
item.parent = nil
|
86
|
+
end
|
87
|
+
|
88
|
+
def find_item(tmp_name)
|
89
|
+
@items.each do |item|
|
90
|
+
return item if item.name == tmp_name.to_s.strip
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def items_count
|
95
|
+
count = 0
|
96
|
+
@items.each do |item|
|
97
|
+
count += item.items_count
|
98
|
+
end
|
99
|
+
count
|
100
|
+
end
|
101
|
+
|
102
|
+
def find_by_name(name_tmp)
|
103
|
+
existed_item = nil
|
104
|
+
existed_items = []
|
105
|
+
|
106
|
+
if self.full_name == name_tmp
|
107
|
+
return self
|
108
|
+
end
|
109
|
+
|
110
|
+
@items.each do |item|
|
111
|
+
existed_item = item.find_by_name(name_tmp)
|
112
|
+
existed_items << existed_item if existed_item
|
113
|
+
end
|
114
|
+
return nil if existed_items.size == 0
|
115
|
+
return existed_items[0]
|
116
|
+
end
|
117
|
+
|
118
|
+
def wrap
|
119
|
+
result = (name == 'root') ? "" : "<option value='#{full_name}:group' class='level_#{(n = parents_count.modulo(4))>0 || parents_count==0 ? n : 4} group'>#{spaces}#{name.humanize}</option>"
|
120
|
+
@items.each {|item| result += item.wrap }
|
121
|
+
result
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
$:.push File.expand_path("../../", __FILE__)
|
2
|
+
require 'erb'
|
3
|
+
require 'nested_list'
|
4
|
+
module NestedList
|
5
|
+
module Task
|
6
|
+
include Generator
|
7
|
+
def generate_html
|
8
|
+
template = ERB.new <<-EOF
|
9
|
+
<!DOCTYPE html>
|
10
|
+
<html>
|
11
|
+
<head>
|
12
|
+
<title>Nested List</title>
|
13
|
+
</head>
|
14
|
+
<body>
|
15
|
+
<select><%= nested_options_example %></select>
|
16
|
+
</body>
|
17
|
+
</html>
|
18
|
+
EOF
|
19
|
+
mkdir_p "lib/public/"
|
20
|
+
puts "cd lib/public/"
|
21
|
+
puts "File nested_list.html preparing..."
|
22
|
+
File.open("lib/public/nested_list.html", "w") do |f|
|
23
|
+
f.puts template.result(binding)
|
24
|
+
end
|
25
|
+
puts "lib/public/nested_list.html was created"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/nested_list.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require "nested_list/nested_select"
|
2
|
+
require "active_support/inflector"
|
3
|
+
|
4
|
+
module Generator
|
5
|
+
|
6
|
+
class NestedList
|
7
|
+
# Get Array of nested names:
|
8
|
+
#[{name: "All Categories", id: 'all'},
|
9
|
+
# {name: "Audio", id: '1'},
|
10
|
+
# {name: "Audio>Accessorize", id: '1424'},
|
11
|
+
# {name: "Audio>Accessorize>Pillow", id: '45255'}
|
12
|
+
# {name: "Audio>DVD", id: '234245'},
|
13
|
+
# {name: "Baby", id: '44245tr5'}]
|
14
|
+
def initialize(nested_names_arr)
|
15
|
+
@nested_names_arr = nested_names_arr
|
16
|
+
end
|
17
|
+
|
18
|
+
#Return a list of nested html_options from nested names array
|
19
|
+
def html_options
|
20
|
+
@wraped_list = {}
|
21
|
+
list = NestedSelect::List.new("root", nil)
|
22
|
+
|
23
|
+
@nested_names_arr.each do |nested_names|
|
24
|
+
@names = nested_names[:name].split(">")
|
25
|
+
point = list
|
26
|
+
length = @names.length - 1
|
27
|
+
@names.each_with_index do |name, level|
|
28
|
+
id = nested_names[:id]
|
29
|
+
full_name = @names[0..level].join(" ").parameterize
|
30
|
+
|
31
|
+
if exist_node=list.find_by_name(full_name)
|
32
|
+
# Use existed name
|
33
|
+
if exist_node.full_name!=full_name && length != level
|
34
|
+
|
35
|
+
found_item = exist_node.find_item(name)
|
36
|
+
moved_item = found_item
|
37
|
+
|
38
|
+
exist_node.remove_item(found_item)
|
39
|
+
item = NestedSelect::List.new(name, nil)
|
40
|
+
item.add_item(moved_item)
|
41
|
+
exist_node.add_item(item)
|
42
|
+
point = item
|
43
|
+
else
|
44
|
+
point = exist_node
|
45
|
+
end
|
46
|
+
|
47
|
+
else
|
48
|
+
# Create new node
|
49
|
+
unless (level == length)
|
50
|
+
item = NestedSelect::List.new(name, nil)
|
51
|
+
item.add_item(NestedSelect::Item.new(name, nil))
|
52
|
+
else
|
53
|
+
item = NestedSelect::Item.new(name, id)
|
54
|
+
end
|
55
|
+
|
56
|
+
point.add_item(item)
|
57
|
+
point = item
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
@wraped_list = list.wrap
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Example
|
68
|
+
def nested_options_example
|
69
|
+
@nested_names_arr = []
|
70
|
+
@nested_names_arr << {name: "All Categories", id: 'all'}
|
71
|
+
@nested_names_arr << {name: "Audio", id: '1'}
|
72
|
+
@nested_names_arr << {name: "Audio>Accessorize>Smile", id: '566767'}
|
73
|
+
@nested_names_arr << {name: "Audio>Accessorize>Pillow", id: '45255'}
|
74
|
+
@nested_names_arr << {name: "Audio>DVD", id: '234245'}
|
75
|
+
@nested_names_arr << {name: "Baby", id: '44245tr5'}
|
76
|
+
|
77
|
+
nested_list = NestedList.new(@nested_names_arr)
|
78
|
+
nested_list.html_options
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
data/nested_list.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "nested_list/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "nested_list"
|
7
|
+
s.version = NestedList::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Dina Zhyliaieva"]
|
10
|
+
s.email = ["din.chick@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{A Nested list is library that allow to generate a set of options to use it in simple select element.}
|
13
|
+
s.description = %q{A Nested list is library that allow to generate a set of options to use it in simple select element. The options are generated in such way that you can get nested drop-down list saved system style just using simple <select/> element. Run rake nested_list_html_page to generate html page as example.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "nested_list"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nested_list
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dina Zhyliaieva
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-02-16 00:00:00 +02:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: A Nested list is library that allow to generate a set of options to use it in simple select element. The options are generated in such way that you can get nested drop-down list saved system style just using simple <select/> element. Run rake nested_list_html_page to generate html page as example.
|
18
|
+
email:
|
19
|
+
- din.chick@gmail.com
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- Gemfile
|
28
|
+
- Gemfile.lock
|
29
|
+
- Rakefile
|
30
|
+
- lib/nested_list.rb
|
31
|
+
- lib/nested_list/nested_select.rb
|
32
|
+
- lib/nested_list/tasks.rb
|
33
|
+
- lib/nested_list/version.rb
|
34
|
+
- nested_list.gemspec
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: ""
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project: nested_list
|
59
|
+
rubygems_version: 1.6.2
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: A Nested list is library that allow to generate a set of options to use it in simple select element.
|
63
|
+
test_files: []
|
64
|
+
|