activemenu 0.6.4 → 0.7.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ca64f356d92dc4cd07df9bc37032f002b169a547
4
- data.tar.gz: f0ba8f51a9adbba6b53469d30314310cd8d86358
3
+ metadata.gz: 93755f43aa9964d6ccdf71ecc44bffc069ac70df
4
+ data.tar.gz: 64c21a172ed17c5d7f116f05e6a4a640ad74bea6
5
5
  SHA512:
6
- metadata.gz: 3a3f824c1043ae8228d91d0ddb86c2aea5f001ddc168eb018f2d4b4f68f7cfecd15e42af8d6409f02cc5a88780f109d48b1df372ea036ae5431190c2c9618975
7
- data.tar.gz: 2cc46332660dca894c0c89e524ac22c033a6ec14df0611302a7245e638dd0bb655d83dfc40846e34b0cad8550bf64190da082071c68e1d2d95885ff6affb0fc2
6
+ metadata.gz: 25ff9d89be8614cc30fcede3e61b98e334637bbaf2259e41fe3ed99e0db089d30eeb836394d25e15462e288ad2ddff01eb87b90a764e5760af5fc0a204700d12
7
+ data.tar.gz: 4fb6b175ec9878e116708362285456b639bcec476e4d52a84061036a7bd6fb868355bcde8c08e6e89dc1dd911c02a1f28c274f6e879e351e88f53b32dcc3ef91
@@ -1,28 +1,4 @@
1
- class ActiveMenu::Menu
2
-
3
- attr_accessor :id, :submenus, :parent, :options
4
-
5
- def initialize(id, options={}, &block)
6
- @id = id.to_sym
7
- @options = options
8
- @submenus = []
9
- yield(self) if block_given?
10
- end
11
-
12
- def submenu(id, options={}, &block)
13
- sm = self.class.new(id, options, &block)
14
- sm.parent = self
15
- @submenus << sm
16
- sm
17
- end
18
-
19
- def option(key, value=nil)
20
- if value.nil?
21
- @options[key]
22
- else
23
- @options[key] = value
24
- end
25
- end
1
+ class ActiveMenu::Menu < ActiveMenu::Node
26
2
 
27
3
  def text(value = nil)
28
4
  self.option(:text, value)
@@ -32,8 +8,4 @@ class ActiveMenu::Menu
32
8
  self.option(:href, value)
33
9
  end
34
10
 
35
- def tag(value=nil)
36
- self.option(:tag, value)
37
- end
38
-
39
11
  end
@@ -0,0 +1,69 @@
1
+ class ActiveMenu::Node
2
+
3
+ attr_accessor :id, :parent, :children, :options
4
+
5
+ def initialize(id, options={}, &block)
6
+ @id = id.to_sym
7
+ @parent = nil
8
+ @children = []
9
+ @options = options
10
+ yield(self) if block_given?
11
+ end
12
+
13
+
14
+ def option(key, value=nil)
15
+ if value.nil?
16
+ @options[key]
17
+ else
18
+ @options[key] = value
19
+ end
20
+ end
21
+
22
+ def tag(value=nil)
23
+ self.option(:tag, value)
24
+ end
25
+
26
+ def child(id, options={}, &block)
27
+ new_child = self.class.new(id, options)
28
+ new_child.parent = self
29
+ @children << new_child
30
+ yield(new_child) if block_given?
31
+ new_child
32
+ end
33
+
34
+ def get(id, &block)
35
+ id = id.to_sym
36
+ selected = @children.select {|m| m.id == id}
37
+ if selected.length >= 1
38
+ first_element = selected.first
39
+ yield(first_element) if block_given?
40
+ first_element
41
+ else
42
+ false
43
+ end
44
+ end
45
+
46
+ def exists?(id)
47
+ id = id.to_sym
48
+ if self.get(id)
49
+ true
50
+ else
51
+ false
52
+ end
53
+ end
54
+
55
+ def leave_children!
56
+ @children = []
57
+ end
58
+
59
+ def exists?(id)
60
+ id = id.to_sym
61
+ if self.get(id)
62
+ true
63
+ else
64
+ false
65
+ end
66
+ end
67
+
68
+
69
+ end
@@ -2,40 +2,30 @@ require 'singleton'
2
2
  class ActiveMenu::Registry
3
3
  include Singleton
4
4
 
5
- attr_accessor :menus
5
+ attr_accessor :main_node
6
6
 
7
7
  def initialize
8
- @menus = []
8
+ @main_node = ActiveMenu::Menu.new(:main_node)
9
9
  end
10
10
 
11
11
  def create(id, options={}, &block)
12
- menu = ActiveMenu::Menu.new(id, options, &block)
13
- @menus << menu
14
- menu
12
+ @main_node.child(id, options, &block)
15
13
  end
16
14
 
17
15
  def reset
18
- @menus = []
16
+ @main_node.leave_children!
19
17
  end
20
18
 
21
19
  def get(id, &block)
22
- id = id.to_sym
23
- selected = @menus.select {|m| m.id == id}
24
- if selected.length >= 1
25
- yield(selected.first) if block_given?
26
- selected.first
27
- else
28
- false
29
- end
30
- end
20
+ @main_node.get(id, &block)
21
+ end
22
+
23
+ def menus
24
+ @main_node.children
25
+ end
31
26
 
32
27
  def exists?(id)
33
- id = id.to_sym
34
- if self.get(id)
35
- true
36
- else
37
- false
38
- end
28
+ @main_node.exists?(id)
39
29
  end
40
30
 
41
31
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveMenu
2
- VERSION = "0.6.4"
2
+ VERSION = "0.7.0"
3
3
  end
data/lib/active_menu.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "active_menu/version"
2
+ require "active_menu/node"
2
3
  require "active_menu/menu"
3
4
  require "active_menu/registry"
4
5
 
@@ -5,11 +5,11 @@ describe ActiveMenu::Menu do
5
5
  subject {ActiveMenu::Menu.new(:idtest)}
6
6
 
7
7
  it {should respond_to(:id)}
8
- it {should respond_to(:submenus)}
8
+ it {should respond_to(:children)}
9
9
  it {should respond_to(:parent)}
10
10
  it {should respond_to(:options)}
11
11
 
12
- its(:submenus) {should be_a(Array)}
12
+ its(:children) {should be_a(Array)}
13
13
 
14
14
  before :each do
15
15
  @menu = ActiveMenu::Menu.new(:idtest, href: "http://example.com", text: "My menu")
@@ -23,33 +23,33 @@ describe ActiveMenu::Menu do
23
23
  @menu.href.should == "http://example.com"
24
24
  end
25
25
 
26
- it 'can add submenus' do
27
- @submenu = ActiveMenu::Menu.new(:mysubmenu, href: '#an_anchor', text: "My submenu")
28
- @menu.submenus << @submenu
29
- @menu.submenus.length.should == 1
26
+ it 'can add children' do
27
+ @child = ActiveMenu::Menu.new(:mychild, href: '#an_anchor', text: "My child")
28
+ @menu.children << @child
29
+ @menu.children.length.should == 1
30
30
  end
31
31
 
32
- it 'can add submenus directly' do
33
- submenu = @menu.submenu(:mysubmenu, href: '#an_anchor', text: "My submenu")
34
- @menu.submenus.length.should == 1
35
- submenu.parent.should == @menu
36
- submenu.parent.id.should == @menu.id
32
+ it 'can add children directly' do
33
+ child = @menu.child(:mychild, href: '#an_anchor', text: "My child")
34
+ @menu.children.length.should == 1
35
+ child.parent.should == @menu
36
+ child.parent.id.should == @menu.id
37
37
  end
38
38
 
39
39
 
40
40
  it 'have a flexible DSL for menus' do
41
- @menu.submenu(:mysubmenu, text: "test") do |sm|
41
+ @menu.child(:mychild, text: "test") do |sm|
42
42
  @sm = sm
43
- sm.text 'My submenu'
44
- sm.submenu(:mysubsubmenu, text: 'test 2') do |ssm|
43
+ sm.text 'My child'
44
+ sm.child(:mysubchild, text: 'test 2') do |ssm|
45
45
  @ssm = ssm
46
- ssm.text == 'My subsubmenu'
46
+ ssm.text == 'My subchild'
47
47
  end
48
48
  end
49
49
 
50
- @menu.submenus.length.should == 1
51
- @sm.submenus.length.should == 1
52
- @ssm.submenus.length.should == 0
50
+ @menu.children.length.should == 1
51
+ @sm.children.length.should == 1
52
+ @ssm.children.length.should == 0
53
53
 
54
54
  end
55
55
 
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveMenu::Node do
4
+ subject {ActiveMenu::Node.new(:idtest)}
5
+
6
+ it {should respond_to(:id)}
7
+ it {should respond_to(:parent)}
8
+ it {should respond_to(:children)}
9
+ it {should respond_to(:options)}
10
+
11
+ its(:parent) {should be_nil}
12
+ its(:children) {should be_a(Array)}
13
+
14
+
15
+ before :each do
16
+ @node = ActiveMenu::Node.new(:mainnode)
17
+ end
18
+
19
+ it 'can add childs with #child method' do
20
+ @node.child :childid do |c|
21
+ c.parent.should == @node
22
+ end
23
+ end
24
+
25
+ context "with a node with children" do
26
+
27
+ subject do
28
+ @node.child :childid do |c|
29
+ c.parent.should == @node
30
+ end
31
+ end
32
+
33
+ it 'can leave the children!' do
34
+ @node.leave_children!
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -6,19 +6,19 @@ describe 'ActiveMenu::Registry' do
6
6
  it {should be_a_kind_of(Singleton)}
7
7
 
8
8
 
9
- before :each do
9
+ before :each do
10
10
  ActiveMenu::Registry.instance.reset
11
11
  end
12
12
 
13
13
  it 'can reset the menus to zero' do
14
- ActiveMenu::Registry.instance.create(:idmenu, 'http://example.com')
14
+ ActiveMenu::Registry.instance.create(:idmenu, href: 'http://example.com')
15
15
  ActiveMenu::Registry.instance.menus.length == 1
16
16
  ActiveMenu::Registry.instance.reset
17
17
  ActiveMenu::Registry.instance.menus.length == 0
18
18
  end
19
19
 
20
20
  it 'can get a menu and yield it to a block' do
21
- ActiveMenu::Registry.instance.create(:idmenu, 'http://example.com')
21
+ ActiveMenu::Registry.instance.create(:idmenu, href: 'http://example.com')
22
22
  ActiveMenu::get(:idmenu) do |m|
23
23
  m.id.should == :idmenu
24
24
  end
@@ -35,14 +35,56 @@ describe ActiveMenu do
35
35
  it 'can create menus with lambdas' do
36
36
  ActiveMenu::create('admix-nav') do |nav|
37
37
  nav.tag :div
38
-
39
- nav.submenu :dashboard do |d|
38
+ nav.child :dashboard do |d|
40
39
  d.text 'dashboard.dashboard'
41
40
  d.href lambda { admix_root_url }
42
41
  d.option :icon, 'icon-flag'
43
42
  end
43
+ end
44
+ end
45
+
46
+
47
+ context 'with a child with lambda' do
48
+
49
+ before :each do
50
+ @dashboard = nil
51
+ @nav = ActiveMenu::create('admix-nav') do |nav|
52
+ nav.tag :div
53
+ @dashboard = nav.child :dashboard do |d|
54
+ d.text 'dashboard.dashboard'
55
+ d.href lambda { admix_root_url }
56
+ d.option :icon, 'icon-flag'
57
+ end
58
+ end
59
+ end
60
+
61
+ it 'can retrieve the admix nav' do
62
+ ActiveMenu::get('admix-nav').should == @nav
63
+ end
64
+
65
+ it 'can retrieve the child and add a child' do
44
66
 
67
+ ActiveMenu::get(:'admix-nav').get(:dashboard).should == @dashboard
68
+
69
+ @testchild = nil
70
+
71
+ ActiveMenu::get(:'admix-nav') do |nav|
72
+ nav.get(:dashboard) do |d|
73
+ d.child :testchild do |sub|
74
+ @testchild = sub
75
+ sub.text "My child"
76
+ end
77
+ end
78
+ end
79
+
80
+ ActiveMenu::get(:'admix-nav').get(:dashboard).get(:testchild).should == @testchild
81
+
82
+ ActiveMenu::get(:'admix-nav').get(:dashboard).get(:testchild).text.should == "My child"
83
+
45
84
  end
85
+
86
+
46
87
  end
47
88
 
89
+
48
90
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activemenu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sadjow Medeiros Leão
@@ -54,10 +54,12 @@ files:
54
54
  - activemenu.gemspec
55
55
  - lib/active_menu.rb
56
56
  - lib/active_menu/menu.rb
57
+ - lib/active_menu/node.rb
57
58
  - lib/active_menu/registry.rb
58
59
  - lib/active_menu/version.rb
59
60
  - lib/activemenu.rb
60
61
  - spec/lib/active_menu/menu_spec.rb
62
+ - spec/lib/active_menu/node_spec.rb
61
63
  - spec/lib/active_menu/registry_spec.rb
62
64
  - spec/lib/active_menu_spec.rb
63
65
  - spec/spec_helper.rb
@@ -87,6 +89,7 @@ specification_version: 4
87
89
  summary: A toolkit for menus.
88
90
  test_files:
89
91
  - spec/lib/active_menu/menu_spec.rb
92
+ - spec/lib/active_menu/node_spec.rb
90
93
  - spec/lib/active_menu/registry_spec.rb
91
94
  - spec/lib/active_menu_spec.rb
92
95
  - spec/spec_helper.rb