site_map 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,29 @@
1
+ = SiteMap
2
+
3
+ == Description
4
+
5
+ SiteMap provides a way to model out your site's views in a hierarchal fashion. You configure your views and then it builds a tree structure from it that is easy to lookup specific nodes and also traverse their relationships, such as parent or children.
6
+
7
+ == Installation
8
+
9
+ sudo gem install site_map
10
+
11
+ == Basic Usage
12
+
13
+ SiteMap by default looks for "config/site_map.rb" to try and load configurations from. Defining your site map is easy with a syntax similar to Rails routes:
14
+
15
+ SiteMap.define do |map|
16
+ map.view :user
17
+ end
18
+
19
+ This creates a single view in your map that can be found with the index :user. Finding view node's is easy as well:
20
+
21
+ SiteMap[:user]
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.
27
+
28
+
29
+
data/Rakefile ADDED
@@ -0,0 +1,63 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/testtask'
4
+
5
+ require 'lib/site_map/version'
6
+
7
+ spec = Gem::Specification.new do |s|
8
+ s.name = 'site_map'
9
+ s.version = SiteMap::Version.to_s
10
+ s.has_rdoc = true
11
+ s.extra_rdoc_files = %w(README.rdoc)
12
+ s.rdoc_options = %w(--main README.rdoc)
13
+ s.summary = "SiteMap provides a way to model out your site's views in a hierarchal fashion."
14
+ s.author = 'Collin Redding'
15
+ s.email = 'TempestTTU@gmail.com'
16
+ s.homepage = 'http://github.com/jcredding/site_map'
17
+ s.files = %w(README.rdoc Rakefile) + Dir.glob("{lib}/**/*")
18
+ # s.executables = ['site_map']
19
+
20
+ # s.add_dependency('gem_name', '~> 0.0.1')
21
+ end
22
+
23
+ Rake::GemPackageTask.new(spec) do |pkg|
24
+ pkg.gem_spec = spec
25
+ end
26
+
27
+ Rake::TestTask.new do |t|
28
+ t.libs << 'test'
29
+ t.test_files = FileList["test/**/*_test.rb"]
30
+ t.verbose = true
31
+ end
32
+
33
+ begin
34
+ require 'rcov/rcovtask'
35
+
36
+ Rcov::RcovTask.new(:coverage) do |t|
37
+ t.libs = ['test']
38
+ t.test_files = FileList["test/**/*_test.rb"]
39
+ t.verbose = true
40
+ t.rcov_opts = ['--text-report', "-x #{Gem.path}", '-x /Library/Ruby', '-x /usr/lib/ruby']
41
+ end
42
+
43
+ task :default => :coverage
44
+
45
+ rescue LoadError
46
+ warn "\n**** Install rcov (sudo gem install relevance-rcov) to get coverage stats ****\n"
47
+ task :default => :test
48
+ end
49
+
50
+
51
+ desc 'Generate the gemspec to serve this gem'
52
+ task :gemspec do
53
+ file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
54
+ File.open(file, 'w') {|f| f << spec.to_ruby }
55
+ puts "Created gemspec: #{file}"
56
+ end
57
+
58
+ require 'cucumber'
59
+ require 'cucumber/rake/task'
60
+
61
+ Cucumber::Rake::Task.new(:features) do |t|
62
+ t.cucumber_opts = "test/features --format pretty"
63
+ end
@@ -0,0 +1,21 @@
1
+ module SiteMap
2
+ module Helpers
3
+
4
+ module Mapping
5
+
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)
9
+ yield view_node if block_given?
10
+ end
11
+ 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)
15
+ yield view_node if block_given?
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ Dir[File.join(File.dirname(__FILE__), "helpers" ,"*.rb")].each do |file|
2
+ require File.join('site_map', 'helpers', File.basename(file, ".rb"))
3
+ end
@@ -0,0 +1,42 @@
1
+ require File.join('site_map', 'helpers', 'mapping')
2
+ module SiteMap
3
+
4
+ class Map
5
+ include SiteMap::Helpers::Mapping
6
+ ATTRIBUTES = [:view_nodes, :index_of_nodes]
7
+ ATTRIBUTES.each{|attribute| attr_reader attribute }
8
+
9
+ def initialize
10
+ self.clear_nodes!
11
+ end
12
+
13
+ def find(view_node_index)
14
+ self.index_of_nodes[view_node_index.to_sym]
15
+ end
16
+
17
+ def add_to_children(view_node)
18
+ @view_nodes.push(view_node)
19
+ end
20
+ def add_to_index(view_node)
21
+ @index_of_nodes[view_node.index.to_sym] = view_node
22
+ end
23
+
24
+ def clear_nodes!
25
+ @view_nodes = []
26
+ @index_of_nodes = {}
27
+ end
28
+
29
+ # convenience method
30
+ def map
31
+ self
32
+ end
33
+
34
+ protected
35
+
36
+ def view_node_params(index, options)
37
+ [index, self, options]
38
+ end
39
+
40
+ end
41
+
42
+ end
@@ -0,0 +1,13 @@
1
+ module SiteMap
2
+ module Version
3
+
4
+ MAJOR = 0
5
+ MINOR = 2
6
+ TINY = 0
7
+
8
+ def self.to_s # :nodoc:
9
+ [MAJOR, MINOR, TINY].join('.')
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,26 @@
1
+ module SiteMap
2
+ module ViewHelpers
3
+
4
+ # Override in view scope to resolve against your labels
5
+ def view_node_label_options
6
+ {}
7
+ end
8
+
9
+ # subbing hash options into view node label code thanks to kelredd-useful
10
+ def view_node_label(view_node)
11
+ label_string = view_node.label
12
+ view_node_label_options.each do |label_key, value|
13
+ label_string.gsub!(":#{label_key}", value.to_s)
14
+ end
15
+ label_string
16
+ end
17
+ def view_node_url(view_node)
18
+ eval(view_node.url)
19
+ end
20
+ def view_node_visible(view_node)
21
+ eval(view_node.visible)
22
+ end
23
+ alias :view_node_visible? :view_node_visible
24
+
25
+ end
26
+ end
@@ -0,0 +1,63 @@
1
+ require File.join('site_map', 'helpers', 'mapping')
2
+ module SiteMap
3
+
4
+ class ViewNode
5
+ include SiteMap::Helpers::Mapping
6
+ ATTRIBUTES = [ :map, :index, :label, :url, :visible, :parent ]
7
+ ATTRIBUTES.each{|attribute| attr_reader attribute }
8
+
9
+ def initialize(index, map, options={})
10
+ raise(ArgumentError, "index and map arguements required") unless index && map
11
+ @index = index
12
+ @map = map
13
+ options.each do |method, value|
14
+ instance_variable_set("@#{method}", value)
15
+ end
16
+ @view_nodes = []
17
+ end
18
+
19
+ def [](attribute_sym)
20
+ instance_variable_get("@#{attribute_sym}")
21
+ end
22
+
23
+ def label
24
+ @label ? @label : @index.to_s
25
+ end
26
+ def visible
27
+ @visible ? @visible : 'true'
28
+ end
29
+
30
+ def add_to_children(view_node)
31
+ @view_nodes.push(view_node)
32
+ end
33
+
34
+ def children
35
+ @view_nodes
36
+ end
37
+ def ancestors
38
+ unless @ancestors
39
+ node, @ancestors = self, []
40
+ @ancestors << node = node.parent while node.parent
41
+ @ancestors.reverse!
42
+ end
43
+ @ancestors
44
+ end
45
+ def self_and_ancestors
46
+ @with_ancestors ||= self.ancestors.dup.push(self)
47
+ end
48
+ def siblings
49
+ @siblings ||= (self.self_and_siblings - [self])
50
+ end
51
+ def self_and_siblings
52
+ @with_siblings ||= self.parent.children
53
+ end
54
+
55
+ protected
56
+
57
+ def view_node_params(new_index, options)
58
+ [new_index, self.map, options.merge(:parent => self)]
59
+ end
60
+
61
+ end
62
+
63
+ end
data/lib/site_map.rb ADDED
@@ -0,0 +1,37 @@
1
+ Dir[File.join(File.dirname(__FILE__), "site_map" ,"*.rb")].each do |file|
2
+ require File.join('site_map', File.basename(file, ".rb"))
3
+ end
4
+
5
+ module SiteMap
6
+
7
+ def self.setup(*files)
8
+ files = files.first if files.first.is_a?(Array)
9
+ files << File.join('config', 'site_map.rb') if files.empty?
10
+ @@map ||= SiteMap::Map.new
11
+ @@map.clear_nodes!
12
+ files.each do |file|
13
+ begin
14
+ load file
15
+ rescue LoadError => exception
16
+ end
17
+ end
18
+ end
19
+
20
+ def self.[](view_node_index)
21
+ self.map.find(view_node_index.to_sym)
22
+ end
23
+
24
+ def self.view_nodes
25
+ self.map.view_nodes
26
+ end
27
+
28
+ def self.map
29
+ @@map
30
+ end
31
+
32
+ def self.define(&block)
33
+ yield @@map
34
+ end
35
+
36
+ end
37
+ SiteMap.setup
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: site_map
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Collin Redding
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-15 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: TempestTTU@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - README.rdoc
26
+ - Rakefile
27
+ - lib/site_map/helpers/mapping.rb
28
+ - lib/site_map/helpers.rb
29
+ - lib/site_map/map.rb
30
+ - lib/site_map/version.rb
31
+ - lib/site_map/view_helpers.rb
32
+ - lib/site_map/view_node.rb
33
+ - lib/site_map.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/jcredding/site_map
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --main
41
+ - README.rdoc
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: SiteMap provides a way to model out your site's views in a hierarchal fashion.
63
+ test_files: []
64
+