Removing .DS_Store and .pid files from repository.
[merb_radiant.git] / lib / radiant / admin_ui.rb
blob67d31d7b0792e1ea016a8b3c5e9c832207789b4c
1 #TODO ok
3 require 'simpleton'
5 module Radiant
6   class AdminUI
7     
8     class DuplicateTabNameError < StandardError; end
9     
10     class Tab
11       attr_accessor :name, :url, :visibility
12       
13       def initialize(name, url, options = {})
14         @name, @url = name, url
15         @visibility = [options[:for], options[:visibility]].flatten.compact
16         @visibility = [:all] if @visibility.empty?
17       end
18       
19       def shown_for?(user)
20         visibility.include?(:all) or
21           visibility.any? { |role| user.send("#{role}?") }
22       end  
23     end
24     
25     class TabSet
26       def initialize
27         @tabs = []
28       end
29     
30       def add(name, url, options = {})
31         options.symbolize_keys!
32         before = options.delete(:before)
33         after = options.delete(:after)
34         tab_name = before || after
35         if self[name]
36           raise DuplicateTabNameError.new("duplicate tab name `#{name}'")
37         else
38           if tab_name
39             index = @tabs.index(self[tab_name])
40             index += 1 if before.nil?
41             @tabs.insert(index, Tab.new(name, url, options))
42           else
43             @tabs << Tab.new(name, url, options)
44           end
45         end
46       end
47       
48       def remove(name)
49         @tabs.delete(self[name])
50       end
51       
52       def size
53         @tabs.size
54       end
55       
56       def [](index)
57         if index.kind_of? Integer
58           @tabs[index]
59         else
60           @tabs.find { |tab| tab.name == index }
61         end
62       end
63       
64       def each
65         @tabs.each { |t| yield t }
66       end
67       
68       def clear
69         @tabs.clear
70       end
71       
72       include Enumerable
73     end
74     
75     include Simpleton
76     
77     attr_accessor :tabs
78     
79     def initialize
80       @tabs = TabSet.new
81     end
82     
83   end
84 end