Class: Tabster::TabSet

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport, Enumerable
Defined in:
lib/tabster/tab_set.rb

Defined Under Namespace

Classes: TabRoutingError, TabWrongParams

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTabSet

Returns a new instance of TabSet.



11
12
13
# File 'lib/tabster/tab_set.rb', line 11

def initialize
  clear!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(tab_name, *args, &proc) ⇒ Object



42
43
44
# File 'lib/tabster/tab_set.rb', line 42

def method_missing(tab_name, *args, &proc)
  super
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/tabster/tab_set.rb', line 9

def name
  @name
end

#tabsObject (readonly)

Returns the value of attribute tabs.



8
9
10
# File 'lib/tabster/tab_set.rb', line 8

def tabs
  @tabs
end

Instance Method Details

#add(name, options = {}, &block) ⇒ Object



15
16
17
# File 'lib/tabster/tab_set.rb', line 15

def add(name, options = {}, &block)
  add_tab(name, options, &block)
end

#add_tab(name, options = {}, &block) ⇒ Object Also known as: []=



19
20
21
22
23
24
25
26
27
28
# File 'lib/tabster/tab_set.rb', line 19

def add_tab(name, options = {}, &block)
  puts "ADDING TAB: #{options.inspect}"
  raise_tab_name_error if name.nil?
  options = options.dup.symbolize_keys 
  options.assert_valid_keys(:path_to, :highlights_on, :priority)
  ensure_required_fragments(options)
  tab = builder.build(options, &block)
  @tabs[name.to_sym] = tab
  tab
end

#builderObject



38
39
40
# File 'lib/tabster/tab_set.rb', line 38

def builder
  @builder ||= TabBuilder.new
end

#clear!Object



50
51
52
# File 'lib/tabster/tab_set.rb', line 50

def clear!
  @tabs = ActiveSupport::OrderedHash.new
end

#eachObject



58
59
60
61
# File 'lib/tabster/tab_set.rb', line 58

def each
  tabs.each { |name, params| yield name, params }
  self
end

#empty?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/tabster/tab_set.rb', line 76

def empty?
  tabs.empty?
end

#ensure_required_fragments(options) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/tabster/tab_set.rb', line 30

def ensure_required_fragments(options)
  Tabster::REQUIRED_TAB_FRAGMENTS.each do |fragment|
    unless options.has_key? fragment.to_sym
      raise ArgumentError, "Tab fragment :path_to cannot be optional."
    end
  end
end

#get(name) ⇒ Object Also known as: []



46
47
48
# File 'lib/tabster/tab_set.rb', line 46

def get(name)
  @tabs[name.to_sym]
end

#lengthObject



80
81
82
# File 'lib/tabster/tab_set.rb', line 80

def length
  tabs.length
end

#namesObject



63
64
65
# File 'lib/tabster/tab_set.rb', line 63

def names
  tabs.keys
end

#raise_tab_name_errorObject

Raises:



84
85
86
# File 'lib/tabster/tab_set.rb', line 84

def raise_tab_name_error
  raise TabRoutingError, "Failed to generate tab from params."  
end

#sort!(sort_key = :priority, &block) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/tabster/tab_set.rb', line 67

def sort!(sort_key = :priority, &block)
  sort_key = sort_key.to_sym
  if block_given?
    tabs.sort &block
  else
    tabs.sort { |tab1, tab2| tab1[1][sort_key] <=> tab2[1][sort_key] }
  end
end

#tab_namesObject



54
55
56
# File 'lib/tabster/tab_set.rb', line 54

def tab_names
  @tabs.keys
end