site_map 0.2.0 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -20,10 +20,13 @@ This creates a single view in your map that can be found with the index :user. F
20
20
 
21
21
  SiteMap[:user]
22
22
 
23
- If the a view node with the index :user exists in the site map, then it will return it and can be used as needed.
24
-
25
- Copyright (c) 2009 John Collin Redding
26
- See the attached MIT License.
23
+ If the a view node with the index :user exists in the site map, then it will return it and can be used as needed. SiteMap can also be used to define views in a resource based method. You can define a collection of view node's with the collection method:
27
24
 
25
+ SiteMap.define do |map|
26
+ map.collection :users
27
+ end
28
28
 
29
+ This defines a single view node with an index of :users__index. It then uses the alias method, to also allow looking up the view node with :users__new and :users_create. SiteMap also defines a member method, which creates a node with :users__show, and alias' :users__edit, :users__update and :users__destroy.
29
30
 
31
+ Copyright (c) 2010 John Collin Redding
32
+ See the attached MIT License.
@@ -0,0 +1,8 @@
1
+ module SiteMap
2
+ module Exceptions
3
+
4
+ class NonExistantViewNode < StandardError
5
+ end
6
+
7
+ end
8
+ end
@@ -4,16 +4,48 @@ module SiteMap
4
4
  module Mapping
5
5
 
6
6
  def group(new_index, options={})
7
- view_node = SiteMap::ViewNode.new(*view_node_params(new_index, options))
8
- self.add_to_children(view_node)
7
+ view_node = self.add_node(new_index, :group, options)
9
8
  yield view_node if block_given?
10
9
  end
11
10
  def view(new_index, options={})
12
- view_node = SiteMap::ViewNode.new(*view_node_params(new_index, options))
13
- self.add_to_children(view_node)
14
- self.map.add_to_index(view_node)
11
+ view_node = self.add_node(new_index, :view, options)
12
+ yield view_node if block_given?
13
+ end
14
+
15
+ def alias(new_index, existing_index)
16
+ view_node = SiteMap[existing_index]
17
+ raise(SiteMap::Exceptions::NonExistantViewNode, "view node with index #{existing_index} does not exist for aliasing") unless view_node
18
+ self.map.add_to_index(new_index, view_node)
19
+ end
20
+
21
+ def collection(resource, options={})
22
+ base_index = "#{resource}__:action"
23
+ new_index = base_index.gsub(':action', 'index').to_sym
24
+ view_node = self.add_node(new_index, :view, options)
25
+ [:new, :create].each do |action|
26
+ action_index = base_index.gsub(':action', action.to_s).to_sym
27
+ view_node.alias(action_index, new_index)
28
+ end
15
29
  yield view_node if block_given?
16
30
  end
31
+ def member(resource, options={})
32
+ base_index = "#{resource}__:action"
33
+ new_index = base_index.gsub(':action', 'show').to_sym
34
+ view_node = self.add_node(new_index, :view, options)
35
+ [:edit, :update, :destroy].each do |action|
36
+ action_index = base_index.gsub(':action', action.to_s).to_sym
37
+ view_node.alias(action_index, new_index)
38
+ end
39
+ end
40
+
41
+ protected
42
+
43
+ def add_node(new_index, type, options={})
44
+ view_node = SiteMap::ViewNode.new(*view_node_params(new_index, type, options))
45
+ self.add_to_children(view_node)
46
+ self.map.add_to_index(new_index, view_node) unless type == :group
47
+ view_node
48
+ end
17
49
 
18
50
  end
19
51
 
data/lib/site_map/map.rb CHANGED
@@ -17,8 +17,8 @@ module SiteMap
17
17
  def add_to_children(view_node)
18
18
  @view_nodes.push(view_node)
19
19
  end
20
- def add_to_index(view_node)
21
- @index_of_nodes[view_node.index.to_sym] = view_node
20
+ def add_to_index(index, view_node)
21
+ @index_of_nodes[index.to_sym] = view_node
22
22
  end
23
23
 
24
24
  def clear_nodes!
@@ -31,10 +31,14 @@ module SiteMap
31
31
  self
32
32
  end
33
33
 
34
+ def inspect
35
+ "#<#{self.class}>"
36
+ end
37
+
34
38
  protected
35
39
 
36
- def view_node_params(index, options)
37
- [index, self, options]
40
+ def view_node_params(index, type, options)
41
+ [index, self, type, options]
38
42
  end
39
43
 
40
44
  end
@@ -3,7 +3,7 @@ module SiteMap
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 2
6
- TINY = 0
6
+ TINY = 5
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
@@ -3,13 +3,16 @@ module SiteMap
3
3
 
4
4
  class ViewNode
5
5
  include SiteMap::Helpers::Mapping
6
- ATTRIBUTES = [ :map, :index, :label, :url, :visible, :parent ]
6
+ ATTRIBUTES = [ :map, :index, :label, :url, :visible, :parent, :type ]
7
7
  ATTRIBUTES.each{|attribute| attr_reader attribute }
8
8
 
9
- def initialize(index, map, options={})
10
- raise(ArgumentError, "index and map arguements required") unless index && map
9
+ TYPES = [ :view, :group ]
10
+
11
+ def initialize(index, map, type, options={})
12
+ raise(ArgumentError, "index, map and type arguements required") unless index && map && type
11
13
  @index = index
12
14
  @map = map
15
+ @type = type
13
16
  options.each do |method, value|
14
17
  instance_variable_set("@#{method}", value)
15
18
  end
@@ -23,6 +26,13 @@ module SiteMap
23
26
  def label
24
27
  @label ? @label : @index.to_s
25
28
  end
29
+ def url
30
+ if !@url && self.group?
31
+ self.children.empty? ? @url : self.children.first.url
32
+ else
33
+ @url
34
+ end
35
+ end
26
36
  def visible
27
37
  @visible ? @visible : 'true'
28
38
  end
@@ -52,10 +62,21 @@ module SiteMap
52
62
  @with_siblings ||= self.parent.children
53
63
  end
54
64
 
65
+ TYPES.each do |type|
66
+ self.send(:define_method, "#{type}?", lambda{ self.type == type })
67
+ end
68
+
69
+ def inspect
70
+ attributes_string = [:index, :type, :label, :url, :visible].collect do |attribute|
71
+ "#{attribute}: #{self.send(attribute).inspect}"
72
+ end.join(", ")
73
+ "#<#{self.class} #{attributes_string}>"
74
+ end
75
+
55
76
  protected
56
77
 
57
- def view_node_params(new_index, options)
58
- [new_index, self.map, options.merge(:parent => self)]
78
+ def view_node_params(new_index, type, options)
79
+ [new_index, self.map, type, options.merge(:parent => self)]
59
80
  end
60
81
 
61
82
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: site_map
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Collin Redding
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-15 00:00:00 -06:00
12
+ date: 2010-01-18 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -24,6 +24,7 @@ extra_rdoc_files:
24
24
  files:
25
25
  - README.rdoc
26
26
  - Rakefile
27
+ - lib/site_map/exceptions.rb
27
28
  - lib/site_map/helpers/mapping.rb
28
29
  - lib/site_map/helpers.rb
29
30
  - lib/site_map/map.rb