labyrinth 0.0.1.alpha

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in labyrinth.gemspec
4
+ gemspec
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "labyrinth/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "labyrinth"
7
+ s.version = Labyrinth::VERSION
8
+ s.authors = ["Bry Ashman"]
9
+ s.email = ["bryashman@gmail.com"]
10
+ s.homepage = "https://github.com/yrb/labyrinth"
11
+ s.summary = "Declarative breadcrumbs for Rails"
12
+ s.description = "Declarative breadcrumbs for Rails"
13
+
14
+ s.rubyforge_project = "labyrinth"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,8 @@
1
+ require "labyrinth/version"
2
+ require "labyrinth/crumb"
3
+ require "labyrinth/layout"
4
+ require "labyrinth/clew"
5
+ require "labyrinth/view_helpers"
6
+
7
+ require "labyrinth/railtie" if defined?(Rails)
8
+
@@ -0,0 +1,20 @@
1
+ module Labyrinth
2
+ class Clew
3
+ def initialize(last_crumb, *args)
4
+ @path = []
5
+ parent(last_crumb, *args)
6
+ end
7
+
8
+ def parent(crumb_name, *args)
9
+ instance_exec(*args, &Labyrinth.find(crumb_name))
10
+ end
11
+
12
+ def link(title, href, options={})
13
+ @path << Labyrinth::Crumb.new(title, href, options)
14
+ end
15
+
16
+ def path
17
+ @path.reverse
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ module Labyrinth
2
+ class Crumb
3
+ attr_reader :title, :href, :options
4
+
5
+ def initialize(title, href, options={})
6
+ @title, @href, @options = title, href, options
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,24 @@
1
+ module Labyrinth
2
+ extend self
3
+
4
+ def layout(&block)
5
+ instance_eval &block
6
+ end
7
+
8
+ def reset!
9
+ @crumbs = {}
10
+ end
11
+
12
+ def crumbs
13
+ @crumbs ||= {}
14
+ end
15
+
16
+ def crumb(name, &block)
17
+ crumbs[name] = block
18
+ end
19
+
20
+ def find(name)
21
+ raise "Crumb '#{name}' has not been defined." unless crumbs.has_key?(name)
22
+ crumbs[name]
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ module Labyrinth
2
+ class Railtie < Rails::Railtie
3
+
4
+ initializer "labyrinth.view_helpers" do
5
+ ActionView::Base.send(:include, Labyrinth::ViewHelpers)
6
+ end
7
+
8
+ config.after_initialize do
9
+ Labyrinth::Clew.send(:include, Rails.application.routes.url_helpers)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Labyrinth
2
+ VERSION = "0.0.1.alpha"
3
+ end
@@ -0,0 +1,24 @@
1
+ module Labyrinth
2
+ module ViewHelpers
3
+ def labyrinth_trail(last_crumb, *args)
4
+ trail = Labyrinth::Clew.new(last_crumb, *args).path
5
+ trail_end = trail.pop
6
+
7
+ content_tag(:ul, :class => "breadcrumb") do
8
+ trail.map(&method(:linked_crumb)).reduce(&:+) +
9
+ active_crumb(trail_end)
10
+ end
11
+ end
12
+
13
+ private
14
+ def linked_crumb(crumb)
15
+ content_tag(:li,
16
+ link_to(crumb.title, crumb.href, crumb.options) +
17
+ content_tag(:span, '/', :class => "divider"))
18
+ end
19
+
20
+ def active_crumb(crumb)
21
+ content_tag(:li, crumb.title, :class => "active")
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe Labyrinth::Clew do
4
+ before do
5
+ Labyrinth.reset!
6
+ Labyrinth.layout do
7
+ crumb :root do
8
+ link 'Root Title', 'root href'
9
+ end
10
+
11
+ crumb :level_2 do |title|
12
+ link 'Level 2', title
13
+ parent :root
14
+ end
15
+
16
+ crumb :test do |obj|
17
+ link obj.title, obj.href, :option => true
18
+ parent :level_2
19
+ end
20
+ end
21
+ end
22
+
23
+ let(:test_object) do
24
+ stub('Test Object',
25
+ :title => 'Test Title',
26
+ :href => 'Test Link')
27
+ end
28
+ let(:clew) { Labyrinth::Clew.new(:test, test_object) }
29
+
30
+ describe "#path" do
31
+ subject { clew.path }
32
+
33
+ it { should have(3).items }
34
+
35
+ it "should be in the correct order" do
36
+ subject.first.title.should == 'Root Title'
37
+ subject.last.title.should == 'Test Title'
38
+ end
39
+ end
40
+
41
+ context "crumbs should be created as per the crumb definition" do
42
+ subject { clew.path.last }
43
+
44
+ it { subject.title.should == 'Test Title' }
45
+ it { subject.href.should == 'Test Link' }
46
+ it { subject.options.should have_key :option }
47
+ end
48
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe Labyrinth::Crumb do
4
+ subject { Labyrinth::Crumb.new('Title', 'Href', :option => true) }
5
+
6
+ specify { subject.title.should == 'Title' }
7
+ specify { subject.href.should == 'Href' }
8
+ specify { subject.options.should have_key(:option) }
9
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Labyrinth do
4
+ it { should respond_to :layout }
5
+
6
+ context "when I have defined a layout" do
7
+ before do
8
+ subject.reset!
9
+ subject.layout do
10
+ crumb :root do
11
+ link 'Root Title', 'root href'
12
+ end
13
+
14
+ crumb :test do |obj|
15
+ link 'Test Title', 'test href', :option => true
16
+ parent :test
17
+ end
18
+ end
19
+ end
20
+
21
+ it { subject.crumbs.should have(2).items }
22
+
23
+ it "can be reset" do
24
+ subject.reset!
25
+ subject.crumbs.should be_empty
26
+ end
27
+
28
+ it "should be able find defined crumbs" do
29
+ subject.find(:test).should_not be_nil
30
+ end
31
+
32
+ it "should raise an exception when it cannot find a crumb" do
33
+ expect {
34
+ subject.find(:nonexistant_crumb)
35
+ }.to raise_error(RuntimeError)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1 @@
1
+ require 'labyrinth'
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: labyrinth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.alpha
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Bry Ashman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-05 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70354723253140 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70354723253140
25
+ description: Declarative breadcrumbs for Rails
26
+ email:
27
+ - bryashman@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - .rspec
34
+ - Gemfile
35
+ - Rakefile
36
+ - labyrinth.gemspec
37
+ - lib/labyrinth.rb
38
+ - lib/labyrinth/clew.rb
39
+ - lib/labyrinth/crumb.rb
40
+ - lib/labyrinth/layout.rb
41
+ - lib/labyrinth/railtie.rb
42
+ - lib/labyrinth/version.rb
43
+ - lib/labyrinth/view_helpers.rb
44
+ - spec/labyrinth/clew_spec.rb
45
+ - spec/labyrinth/crumb_spec.rb
46
+ - spec/labyrinth/layout_spec.rb
47
+ - spec/spec_helper.rb
48
+ homepage: https://github.com/yrb/labyrinth
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>'
64
+ - !ruby/object:Gem::Version
65
+ version: 1.3.1
66
+ requirements: []
67
+ rubyforge_project: labyrinth
68
+ rubygems_version: 1.8.6
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Declarative breadcrumbs for Rails
72
+ test_files:
73
+ - spec/labyrinth/clew_spec.rb
74
+ - spec/labyrinth/crumb_spec.rb
75
+ - spec/labyrinth/layout_spec.rb
76
+ - spec/spec_helper.rb
77
+ has_rdoc: