alacarte 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,11 @@
1
+ = Change Log
2
+
3
+ == Version 0.0.2
4
+
5
+ * Removed some Rails specific routines and replaced them with custom routines
6
+ * Adding basic tests and documentation
7
+
8
+ == Version 0.0.1
9
+
10
+ Initial version
11
+
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 Stijn Mathysen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -1,6 +1,7 @@
1
1
  module Alacarte
2
2
  class Engine < ::Rails::Engine
3
3
 
4
+ # Adding the Alacarte helper method to the Rails application
4
5
  config.to_prepare do
5
6
  ApplicationController.helper(AlacarteHelper)
6
7
  end
data/lib/alacarte/menu.rb CHANGED
@@ -1,14 +1,45 @@
1
1
  module Alacarte
2
2
 
3
+ # Alacarte::Menu is the base class for defining menu items.
3
4
  class Menu
4
5
 
5
6
  VALID_ELEMENTS = [:link, :span]
7
+ @@env = nil
6
8
 
7
- cattr_reader :env
8
9
  attr_reader :parent, :type, :name, :deep_name, :path, :as, :label, :options, :items, :block, :html_options, :group_options
9
10
 
11
+ # Tests if an environment was set to the Alacarte::Menu
12
+ def self.env?
13
+ !!@@env
14
+ end
15
+
16
+ # Returns the environment that was set to the Alacarte::Menu
17
+ def self.env
18
+ @@env
19
+ end
20
+
21
+ # Resets the env, used in rspec testing
22
+ def self.reset_env!
23
+ @@env = nil
24
+ end
25
+
26
+ # Creates a menu item. Menu items can relate to a +parent+ menu item and a type should be passed. The type can be used for
27
+ # rendering (current types are +:menu+, +:link+ and +:span+).
28
+ #
29
+ # The first two attributes that can be passed (not required) are the +name+ and +path+ of the menu item. These can also
30
+ # be set in the options hash. Other settings are derived from +name+, but can also be passed in the options hash.
31
+ #
32
+ # The options you can pass are:
33
+ # * +:name+ sets the name of the menu item
34
+ # * +:path+ sets the path that the menu item should link to
35
+ # * +:label+ sets the label that should be displayed as the menu item
36
+ # * +:as+ defines how the element can be matched with the current element
37
+ # * +:if+ set a conditions when the menu item should be valid
38
+ # * +:unless+ set an inverse conditions when the menu item should be valid
39
+ # * +:html+ pass any html options that you want to be added to the menu item
40
+ # * +:group+ when the menu item has valid children, the +group+ options are passed as the html options of the grouping element
10
41
  def initialize(parent, type, *args, &block)
11
- @options = args.extract_options!.dup
42
+ @options = args.last.is_a?(::Hash) ? args.pop : {}
12
43
  @parent = parent
13
44
  @type = type
14
45
  @name = options[:name] || args[0]
@@ -23,14 +54,12 @@ module Alacarte
23
54
  build
24
55
  end
25
56
 
57
+ # Tests if a block was passed to the Alacarte::Menu object
26
58
  def block?
27
59
  !!@block
28
60
  end
29
61
 
30
- def self.env?
31
- !!@@env
32
- end
33
-
62
+ # Builds the menu, based on the environment that is passed.
34
63
  def build(env = nil)
35
64
  @@env = env if env
36
65
  @items = []
@@ -38,6 +67,7 @@ module Alacarte
38
67
  self.instance_eval(&@block) if Menu.env? && self.block?
39
68
  end
40
69
 
70
+ # Tests to see if the current menu item is valid in the current setting
41
71
  def valid?
42
72
  if options[:if] && options[:if].respond_to?(:call)
43
73
  return options[:if].call
@@ -49,7 +79,8 @@ module Alacarte
49
79
 
50
80
  return true
51
81
  end
52
-
82
+
83
+ # Try to send the menu to the passed +env+, then try to create a menu item of the missing method.
53
84
  def method_missing(id, *args, &block)
54
85
  if Menu.env? && Menu.env.respond_to?(id)
55
86
  @@env.send(id, *args, &block)
@@ -1,10 +1,13 @@
1
1
  module Alacarte
2
2
 
3
+ # Alacarte::Menus provides a dsl for building menus.
3
4
  class Menus < Hash
5
+ # Draw the menu
4
6
  def draw(&block)
5
7
  self.instance_exec(&block)
6
8
  end
7
9
 
10
+ # Define a menu in a draw block
8
11
  def menu(name, *args, &block)
9
12
  self[name] = Menu.new(nil, :menu, name, *args, &block)
10
13
  end
@@ -1,6 +1,7 @@
1
1
  module Rails
2
2
  class Application
3
3
 
4
+ # Provides an entry point for the Rails application to the Alacarte +menus+ attribute.
4
5
  def menus
5
6
  Alacarte.menus
6
7
  end
@@ -1,3 +1,3 @@
1
1
  module Alacarte
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/alacarte.rb CHANGED
@@ -1,9 +1,13 @@
1
- require 'alacarte/engine'
2
- require 'alacarte/rails'
1
+ require 'alacarte/engine' if defined?(Rails)
2
+ require 'alacarte/rails' if defined?(Rails)
3
3
  require 'alacarte/menus'
4
4
  require 'alacarte/menu'
5
5
 
6
6
  module Alacarte
7
- mattr_reader :menus
8
7
  @@menus = Alacarte::Menus.new
8
+
9
+ # menus provides an access point to the Alacarte module +menus+ attribute.
10
+ def self.menus
11
+ @@menus
12
+ end
9
13
  end
@@ -0,0 +1,113 @@
1
+ require 'spec_helper'
2
+
3
+ describe Alacarte::Menu do
4
+
5
+ context "when creating a menu" do
6
+
7
+ it "should be possible to create an item with its minimal settings" do
8
+ @menu = Alacarte::Menu.new(nil, :menu)
9
+
10
+ @menu.name.should be_nil
11
+ @menu.path.should be_nil
12
+ end
13
+
14
+ it "should be possible to pass in a name" do
15
+ @menu = Alacarte::Menu.new(nil, :menu, 'name')
16
+ @menu.name.should eql 'name'
17
+ @menu.label.should eql 'name'
18
+ @menu.deep_name.should eql 'name'
19
+ @menu.as.should eql 'name'
20
+
21
+ @menu = Alacarte::Menu.new(nil, :menu, :name => 'other name')
22
+ @menu.name.should eql 'other name'
23
+ @menu.label.should eql 'other name'
24
+ @menu.deep_name.should eql 'other name'
25
+ @menu.as.should eql 'other name'
26
+ end
27
+
28
+ it "should be possible to pass in a path" do
29
+ @menu = Alacarte::Menu.new(nil, :link, 'login', '/accounts/login')
30
+ @menu.path.should eql '/accounts/login'
31
+
32
+ @menu = Alacarte::Menu.new(nil, :link, 'logout', :path => '/accounts/logout')
33
+ @menu.path.should eql '/accounts/logout'
34
+ end
35
+
36
+ it "should be possible to specify a label" do
37
+ @menu = Alacarte::Menu.new(nil, :span, :label => 'spacer')
38
+ @menu.label.should eql 'spacer'
39
+
40
+ @menu = Alacarte::Menu.new(nil, :link, 'login', :label => 'Log in here!')
41
+ @menu.name.should eql 'login'
42
+ @menu.label.should eql 'Log in here!'
43
+ end
44
+
45
+ it "should be possible to specify an 'as'" do
46
+ @menu = Alacarte::Menu.new(nil, :span, :as => 'spacestation')
47
+ @menu.as.should eql 'spacestation'
48
+
49
+ @menu = Alacarte::Menu.new(nil, :link, 'login', :as => 'login_button')
50
+ @menu.name.should eql 'login'
51
+ @menu.as.should eql 'login_button'
52
+ end
53
+
54
+ end
55
+
56
+ it "should set the env property when the menu is built" do
57
+ @menu = Alacarte::Menu.new(nil, :menu, :as => 'account')
58
+ Alacarte::Menu.env?.should be_false
59
+ Alacarte::Menu.env.should be_nil
60
+
61
+ @menu.build
62
+ Alacarte::Menu.env?.should be_false
63
+ Alacarte::Menu.env.should be_nil
64
+
65
+ @object = Object.new
66
+ @menu.build(@object)
67
+ Alacarte::Menu.env?.should be_true
68
+ Alacarte::Menu.env.should eql @object
69
+ end
70
+
71
+ it "should be possible to reset the env property" do
72
+ Alacarte::Menu.env?.should be_false
73
+ Alacarte::Menu.env.should be_nil
74
+
75
+ @menu = Alacarte::Menu.new(nil, :menu, :as => 'account')
76
+ @object = Object.new
77
+ @menu.build(@object)
78
+ Alacarte::Menu.env?.should be_true
79
+ Alacarte::Menu.env.should eql @object
80
+
81
+ Alacarte::Menu.reset_env!
82
+ Alacarte::Menu.env?.should be_false
83
+ Alacarte::Menu.env.should be_nil
84
+ end
85
+
86
+ it "should be able to test if a block was set on the menu" do
87
+ @menu = Alacarte::Menu.new(nil, :menu, :as => 'account') do
88
+ link :login, '/login'
89
+ link :logout, '/logout'
90
+ end
91
+ @menu.block?.should be_true
92
+
93
+ @menu = Alacarte::Menu.new(nil, :menu, :as => 'account')
94
+ @menu.block?.should be_false
95
+ end
96
+
97
+ it "should return the correct valid state for if statements" do
98
+ @menu = Alacarte::Menu.new(nil, :menu, :if => lambda{ 4 == 3 })
99
+ @menu.valid?.should be_false
100
+
101
+ @menu = Alacarte::Menu.new(nil, :menu, :if => lambda{ 4 == 4 })
102
+ @menu.valid?.should be_true
103
+ end
104
+
105
+ it "should return the correct valid state for unless statements" do
106
+ @menu = Alacarte::Menu.new(nil, :menu, :unless => lambda{ 4 == 3 })
107
+ @menu.valid?.should be_true
108
+
109
+ @menu = Alacarte::Menu.new(nil, :menu, :unless => lambda{ 4 == 4 })
110
+ @menu.valid?.should be_false
111
+ end
112
+
113
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe Alacarte::Menus do
4
+
5
+ before do
6
+ @menus = Alacarte::Menus.new
7
+ end
8
+
9
+ it "should be possible to draw menus using a block" do
10
+ @menus.size.should eql 0
11
+
12
+ @menus.draw do
13
+ menu :one
14
+ menu :two
15
+ end
16
+
17
+ @menus.size.should eql 2
18
+ @menus.keys.should include(:one)
19
+ @menus.keys.should include(:two)
20
+
21
+ @menus.values.each do |menu|
22
+ menu.should be_instance_of Alacarte::Menu
23
+ end
24
+ end
25
+
26
+ it "should be possible to create a menu directly" do
27
+ @menus.size.should eql 0
28
+ @menus.menu :three
29
+ @menus.size.should eql 1
30
+ @menus.keys.first.should eql :three
31
+ end
32
+
33
+ it "should construct menu items when the build command is issued" do
34
+ Alacarte::Menu.env?.should be_false
35
+ @menus.size.should eql 0
36
+ @menus.draw do
37
+ menu :account do
38
+ link :account, '/account' do
39
+ link :avatar, '/account/avatar'
40
+ link :edit, '/account/edit'
41
+ end
42
+ link :login, '/login'
43
+ link :logout, '/logout'
44
+ end
45
+ end
46
+
47
+ @menus.size.should eql 1
48
+ @menus[:account].items.size.should eql 0
49
+
50
+ @menus[:account].build(Object.new)
51
+ @menus[:account].items.size.should eql 3
52
+ @menus[:account].items.first.items.size.should eql 2
53
+ end
54
+
55
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe Alacarte do
4
+
5
+ before :all do
6
+ end
7
+
8
+ it "should be possible to access the menus in the Alacarte namespace" do
9
+ Alacarte.menus.should be_instance_of Alacarte::Menus
10
+ Alacarte.menus.size.should eql 0
11
+ end
12
+
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'I18n'
2
+ require File.dirname(__FILE__) + '/../lib/alacarte.rb'
3
+
4
+ # Add I18n load_path
5
+ # I18n.load_path = (I18n.load_path << Dir[File.join(File.dirname(__FILE__), 'locales', '*.yml')]).uniq
6
+
7
+ RSpec.configure do |c|
8
+
9
+ c.before :each do
10
+ Alacarte::Menu.reset_env!
11
+ end
12
+
13
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alacarte
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Stijn Mathysen
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-17 00:00:00 +01:00
18
+ date: 2010-12-20 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -30,7 +30,9 @@ extra_rdoc_files: []
30
30
 
31
31
  files:
32
32
  - .gitignore
33
+ - CHANGES
33
34
  - Gemfile
35
+ - LICENSE
34
36
  - README.textile
35
37
  - Rakefile
36
38
  - alacarte.gemspec
@@ -41,6 +43,10 @@ files:
41
43
  - lib/alacarte/menus.rb
42
44
  - lib/alacarte/rails.rb
43
45
  - lib/alacarte/version.rb
46
+ - spec/alacarte/menu_spec.rb
47
+ - spec/alacarte/menus_spec.rb
48
+ - spec/alacarte_spec.rb
49
+ - spec/spec_helper.rb
44
50
  has_rdoc: true
45
51
  homepage: http://github.com/stijnster/alacarte
46
52
  licenses: []
@@ -75,5 +81,8 @@ rubygems_version: 1.3.7
75
81
  signing_key:
76
82
  specification_version: 3
77
83
  summary: Provides a generic menu system for Rails
78
- test_files: []
79
-
84
+ test_files:
85
+ - spec/alacarte/menu_spec.rb
86
+ - spec/alacarte/menus_spec.rb
87
+ - spec/alacarte_spec.rb
88
+ - spec/spec_helper.rb