nutmeg 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f7bbd92eb161de717a7158fcafb80113168d0c86
4
+ data.tar.gz: 4606cdc2c77bdacdfcff1ec39c4751ea58d06ddb
5
+ SHA512:
6
+ metadata.gz: 71b7063fce1deba45ac519b44ea1d77dbb257459ceb91b1edbf2f4f09e5eb83c0a08302941c765605b146845f4685f28c97bcd7ce8b5442fd311c7dbf4f31b2e
7
+ data.tar.gz: c2840fd116ce1e535f29c9c073639425351a7c4952cdc5ee53e344025333c6a4289ff550ab7c441e23f7f72d5bcb09196dc78a0a0ba0d5e774b52c60dbab1f3d
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,3 @@
1
+ --colour
2
+ --format documentation
3
+ --drb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nutmeg.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Dennis van der Vliet
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,53 @@
1
+ # Nutmeg
2
+
3
+ Help you build and show a menu tree in rails
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ gem 'nutmeg'
9
+
10
+ And then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install nutmeg
17
+
18
+ ## Usage
19
+
20
+ First create a tree (in this example from a yaml file)
21
+
22
+ `@tree_from_yaml = Nutmeg::TreeFromYaml.new({file: [Dir.pwd, "/spec/files/tree.yml"].join})`
23
+
24
+ To print a nice HTML version of this tree use the helper
25
+
26
+ `Nutmeg::TagTreeHelper.new(@tree_from_yaml.tree).print_html([1])`
27
+
28
+ The argument passed into `print_html` is an array of tags that you can use to select the right path in the tree.
29
+
30
+ Using the example from the spec directory this would output something as follows:
31
+
32
+ `<ul><li class='level_1 active'>north<ul><li class='level_2 active'>men<ul><li class='level_3 active'>collections<ul><li class='level_4 leaf'>winter 2015</li><li class='level_4 leaf'>spring 2015</li><li class='level_4 leaf active'>summer 2015</li></ul></li></ul></li><li class='level_2'>women<ul><li class='level_3'>collections<ul><li class='level_4 leaf'>winter 2015</li><li class='level_4 leaf'>spring 2015</li><li class='level_4 leaf'>summer 2015</li></ul></li></ul></li><li class='level_2'>events<ul><li class='level_3 leaf'>lowlands 2014</li><li class='level_3 leaf'>sinterklaas 2014</li></ul></li><li class='level_2'>collaborations<ul><li class='level_3 leaf'>afro jack</li><li class='level_3 leaf'>pr.co</li></ul></li></ul></li><li class='level_1'>south<ul></li></li></ul></li></ul>`
33
+
34
+ For the time time being you can override the template using the following code
35
+
36
+ ```ruby
37
+ module Nutmeg
38
+ class TagTreeHelper
39
+ def node_html(node, active, leaf)
40
+ # return html
41
+ end
42
+ end
43
+ end
44
+ ```
45
+
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ Dir.glob('tasks/**/*.rake').each(&method(:import))
@@ -0,0 +1,36 @@
1
+ module Nutmeg
2
+ class TagTree
3
+ attr_accessor :original
4
+
5
+ def initialize(tree)
6
+ raise "No valid data type" unless tree.class == Tree::TreeNode
7
+ @original = tree
8
+ end
9
+
10
+ def subtree(id)
11
+ @original.each do |node|
12
+ return Nutmeg::TagTree.new(node.detached_subtree_copy) if node.content[:tag_id] == id
13
+ end
14
+ end
15
+
16
+ def get_paths(tags_given)
17
+ start_nodes = @original.children.select{|l| tags_given.include?(l.content[:tag_id])}
18
+ end_nodes = @original.each_leaf.select{|l| tags_given.include?(l.content[:tag_id])}
19
+
20
+ end_nodes.select do |end_node|
21
+ (end_node.parentage & start_nodes).count >= 1
22
+ end.collect do |leaf|
23
+ ([leaf] + leaf.parentage).reverse
24
+ end.select do |path|
25
+ # we always include the 'root' tag
26
+ (path.map{|tag| tag.content[:tag_id] } - tags_given).count == 1
27
+ end
28
+ end
29
+
30
+ def get_paths_formatted(tags_given, seperator = "/")
31
+ get_paths(tags_given).collect do |tags|
32
+ tags.reject{|tag| tag.content[:slug] == "root"}.map{|node| node.content[:slug]}.join(seperator)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,52 @@
1
+ module Nutmeg
2
+ class TagTreeHelper
3
+ attr_accessor :tree
4
+ def initialize(tree)
5
+ raise "No valid data type" unless tree.class == TagTree
6
+ @tree = tree
7
+ end
8
+
9
+ def print_html(tags_given)
10
+ [print_node(@tree.original, tags_given)].join("")
11
+ end
12
+
13
+ def node_html(node, active, leaf)
14
+ "<li class='level_#{node.level}#{leaf ? ' leaf' : ''}#{active ? ' active' : ''}'>"
15
+ end
16
+
17
+ def print_node(node, tags_given)
18
+ output = []
19
+ if render_node?(node,tags_given)
20
+ output << node_html(node, (!@tree.get_paths(tags_given).first.nil? && @tree.get_paths(tags_given).first.map(&:name).include?(node.name)), node.is_leaf? )
21
+ output << node.content[:slug]
22
+ end
23
+
24
+ if !traverse_node?(node, tags_given)
25
+ output << "</li>"
26
+ return output
27
+ end
28
+ if node.has_children?
29
+ output << "<ul>"
30
+ node.children.each do |child|
31
+ output << print_node(child, tags_given)
32
+ end
33
+ output << "</ul>"
34
+ end
35
+ if render_node?(node,tags_given)
36
+ output << "</li>"
37
+ end
38
+ output
39
+ end
40
+
41
+ def render_node?(node, tags_given)
42
+ return false if node.is_root?
43
+ traverse_node?(node, tags_given)
44
+ end
45
+
46
+ def traverse_node?(node, tags_given)
47
+ return true if node.is_root?
48
+ return true if node.level == 1
49
+ (node.parentage.select{|n| n.level == 1}.map{|n| n.content[:tag_id]} & tags_given).count >= 1
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,55 @@
1
+ module Nutmeg
2
+ class TreeFromYaml
3
+ attr_accessor :base_tree, :tags, :tree_hash, :tree, :tag_tree
4
+
5
+ def initialize(params)
6
+ @data = YAML.load_file(params[:file])
7
+ @tags = @data["tags"]
8
+ @base_tree = @data["tree"]
9
+ @tree_hash = []
10
+ traverse_tree
11
+ get_levels
12
+ @tag_tree = Tree::TreeNode.new("ROOT", {:tag => "root",:otag => "root", :level => nil, :slug => "root"})
13
+ @tree_hash.select{|node|node[:level] == 0}.each do |node|
14
+ node_tree = @tag_tree << Tree::TreeNode.new("tag_#{node[:tag]}_#{node[:level]}_root", node)
15
+ build_node(node_tree, node)
16
+ end
17
+ @tree = Nutmeg::TagTree.new(@tag_tree)
18
+ nil
19
+ end
20
+
21
+ private
22
+ def build_node(start, node)
23
+ get_children(node[:tag], node[:level]).each do |node|
24
+ node_tree = start << Tree::TreeNode.new("tag_#{node[:tag]}", node)
25
+ build_node(node_tree, node)
26
+ end
27
+ end
28
+
29
+ def get_levels
30
+ @levels = @tree_hash.collect do |tag|
31
+ tag[:level]
32
+ end.uniq.sort
33
+ end
34
+
35
+ def get_children(parent, level)
36
+ @tree_hash.select{|node| node[:parent] == parent && node[:level] > level }
37
+ end
38
+
39
+ def traverse_tree
40
+ base_tree.each do |l|
41
+ walk(l,0,"root")
42
+ end
43
+ end
44
+
45
+ def walk(array, level = 0, parent = nil)
46
+ array.keys.each do |key|
47
+ @tree_hash << {:tag => [key, level,parent].join("_"), :level => level, :parent => parent, :otag => key, :slug => @tags[key], :tag_id => key}
48
+ return if array[key].nil?
49
+ array[key].compact.each do |child|
50
+ walk(child, level + 1, [key, level,parent].join("_"))
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module Nutmeg
2
+ VERSION = "0.0.1"
3
+ end
data/lib/nutmeg.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "nutmeg/version"
2
+ require "nutmeg/tree_from_yaml"
3
+ require "nutmeg/tag_tree"
4
+ require "nutmeg/tag_tree_helper"
5
+ require "yaml"
6
+ require "tree"
data/nutmeg.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nutmeg/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nutmeg"
8
+ spec.version = Nutmeg::VERSION
9
+ spec.authors = ["Dennis van der Vliet"]
10
+ spec.email = ["dennis.vandervliet@gmail.com"]
11
+ spec.description = %q{Make creating trees in rails easier and close to fun}
12
+ spec.summary = %q{Easy trees in rails}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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 "rubytree"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+
27
+ end
@@ -0,0 +1,47 @@
1
+ tree:
2
+ - 1:
3
+ - 3:
4
+ - 5:
5
+ - 8:
6
+ - 9:
7
+ - 10:
8
+ - 4:
9
+ - 5:
10
+ - 8:
11
+ - 9:
12
+ - 10:
13
+ - 6:
14
+ - 13:
15
+ - 14:
16
+ - 7:
17
+ - 11:
18
+ - 12:
19
+ - 2:
20
+ - 3:
21
+ - 5:
22
+ - 8:
23
+ - 9:
24
+ - 10:
25
+ - 4:
26
+ - 5:
27
+ - 8:
28
+ - 9:
29
+ - 10:
30
+
31
+
32
+
33
+ tags:
34
+ 1: north
35
+ 2: south
36
+ 3: men
37
+ 4: women
38
+ 5: collections
39
+ 6: events
40
+ 7: collaborations
41
+ 8: winter 2015
42
+ 9: spring 2015
43
+ 10: summer 2015
44
+ 11: afro jack
45
+ 12: pr.co
46
+ 13: lowlands 2014
47
+ 14: sinterklaas 2014
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe Nutmeg do
4
+ end
@@ -0,0 +1 @@
1
+ require 'nutmeg'
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe Nutmeg::TagTreeHelper do
4
+
5
+ before(:each) do
6
+ @tree_from_yaml = Nutmeg::TreeFromYaml.new({file: [Dir.pwd, "/spec/files/tree.yml"].join})
7
+ end
8
+
9
+ describe "#print_html" do
10
+ it "outputs html" do
11
+ expect(Nutmeg::TagTreeHelper.new(@tree_from_yaml.tree).print_html([1]).class).to eq(String)
12
+ expect(Nutmeg::TagTreeHelper.new(@tree_from_yaml.tree).print_html([1,3,5,10])).to eq("<ul><li class='level_1 active'>north<ul><li class='level_2 active'>men<ul><li class='level_3 active'>collections<ul><li class='level_4 leaf'>winter 2015</li><li class='level_4 leaf'>spring 2015</li><li class='level_4 leaf active'>summer 2015</li></ul></li></ul></li><li class='level_2'>women<ul><li class='level_3'>collections<ul><li class='level_4 leaf'>winter 2015</li><li class='level_4 leaf'>spring 2015</li><li class='level_4 leaf'>summer 2015</li></ul></li></ul></li><li class='level_2'>events<ul><li class='level_3 leaf'>lowlands 2014</li><li class='level_3 leaf'>sinterklaas 2014</li></ul></li><li class='level_2'>collaborations<ul><li class='level_3 leaf'>afro jack</li><li class='level_3 leaf'>pr.co</li></ul></li></ul></li><li class='level_1'>south<ul></li></li></ul></li></ul>")
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Nutmeg::TagTree do
4
+
5
+ before(:each) do
6
+ tree = Tree::TreeNode.new("ROOT", {:tag_id => 0, :slug => "root"})
7
+ tree << Tree::TreeNode.new("base", {:tag_id => 1, :slug => "base"}) << Tree::TreeNode.new("child", {:tag_id => 2, :slug => "child"})
8
+ @tag_tree = Nutmeg::TagTree.new(tree)
9
+ end
10
+
11
+ describe "#initialize" do
12
+ it "verifies initialize works correctly" do
13
+ expect(@tag_tree.original.class).to eq(Tree::TreeNode)
14
+ end
15
+ end
16
+
17
+ describe "#subtree" do
18
+ it "returns a subtree starting at tag with tag_id 1" do
19
+ expect(@tag_tree.original.size).to eq(3)
20
+ expect(@tag_tree.subtree(1).original.size).to eq(2)
21
+ end
22
+ end
23
+
24
+ describe "#get_paths" do
25
+ pending "Fix this"
26
+ end
27
+
28
+ describe "#get_paths_formatted" do
29
+ it "returns base/child when using [1,2] as selector" do
30
+ expect(@tag_tree.get_paths_formatted([1,2]).first).to eq("base/child")
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Nutmeg::TreeFromYaml do
4
+
5
+ before(:each) do
6
+ @tree_from_yaml = Nutmeg::TreeFromYaml.new({file: [Dir.pwd, "/spec/files/tree.yml"].join})
7
+ end
8
+
9
+ describe "#initialize" do
10
+ it "verifies initialize works correctly" do
11
+ expect(@tree_from_yaml.tree_hash.class).to eq(Array)
12
+ expect(@tree_from_yaml.tree_hash.first.class).to eq(Hash)
13
+ expect(@tree_from_yaml.tree.class).to eq(Nutmeg::TagTree)
14
+ end
15
+ end
16
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nutmeg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dennis van der Vliet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubytree
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.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: Make creating trees in rails easier and close to fun
70
+ email:
71
+ - dennis.vandervliet@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/nutmeg.rb
83
+ - lib/nutmeg/tag_tree.rb
84
+ - lib/nutmeg/tag_tree_helper.rb
85
+ - lib/nutmeg/tree_from_yaml.rb
86
+ - lib/nutmeg/version.rb
87
+ - nutmeg.gemspec
88
+ - spec/files/tree.yml
89
+ - spec/nutmeg_spec.rb
90
+ - spec/spec_helper.rb
91
+ - spec/tag_tree_helper_spec.rb
92
+ - spec/tag_tree_spec.rb
93
+ - spec/tree_from_yaml_spec.rb
94
+ - tasks/rspec.rake
95
+ homepage: ''
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.1.9
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Easy trees in rails
119
+ test_files:
120
+ - spec/files/tree.yml
121
+ - spec/nutmeg_spec.rb
122
+ - spec/spec_helper.rb
123
+ - spec/tag_tree_helper_spec.rb
124
+ - spec/tag_tree_spec.rb
125
+ - spec/tree_from_yaml_spec.rb