Class: FunkyTabs::Tab

Inherits:
Object
  • Object
show all
Defined in:
lib/funky_tabs/tab.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash_or_string) ⇒ Tab

tab methods

Raises:



23
24
25
26
27
28
29
30
# File 'lib/funky_tabs/tab.rb', line 23

def initialize(hash_or_string)
  # initialize default tab action unless the user has provided one of their own
  default_tab_action unless hash_or_string.is_a?(Hash) && hash_or_string.has_key?(:default_tab_action)

  return set_attrs({:name=>hash_or_string}) if hash_or_string.is_a?(String)
  return set_attrs(hash_or_string) if hash_or_string.is_a?(Hash)
  raise FunkyTabsException.new("invalid tab parameters #{hash_or_string.inspect}")
end

Instance Attribute Details

#default_tab_actionObject

Returns the value of attribute default_tab_action.



8
9
10
# File 'lib/funky_tabs/tab.rb', line 8

def default_tab_action
  @default_tab_action
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/funky_tabs/tab.rb', line 6

def name
  @name
end

#tab_actionsObject

Returns the value of attribute tab_actions.



9
10
11
# File 'lib/funky_tabs/tab.rb', line 9

def tab_actions
  @tab_actions
end

#titleObject

default values for attrs



12
13
14
# File 'lib/funky_tabs/tab.rb', line 12

def title
  @title
end

Instance Method Details

#add_tab_action(string_or_hash) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/funky_tabs/tab.rb', line 62

def add_tab_action(string_or_hash)
  tab_action_index = find_tab_action(string_or_hash)
  if tab_action_index.nil?
    new_tab_action = FunkyTabs::TabAction.new(string_or_hash)
    tab_actions << new_tab_action
    return new_tab_action
  else
    return tab_actions[tab_action_index]
  end
end

#add_tab_actions(array_of_properties) ⇒ Object



73
74
75
76
77
78
# File 'lib/funky_tabs/tab.rb', line 73

def add_tab_actions(array_of_properties)
  array_of_properties.each do |action_properties|
    add_tab_action(action_properties)
  end
  return tab_actions
end

#find_tab_action(tab_action_or_string_or_hash) ⇒ Object

Raises:



53
54
55
56
57
58
59
60
# File 'lib/funky_tabs/tab.rb', line 53

def find_tab_action(tab_action_or_string_or_hash)
  return nil if tab_action_or_string_or_hash.nil? || tab_actions.blank?
  tab_action_or_string_or_hash = tab_action_or_string_or_hash.to_s if tab_action_or_string_or_hash.is_a?(Symbol)
  return tab_actions.index {|action| action.name.downcase == tab_action_or_string_or_hash.downcase} if tab_action_or_string_or_hash.is_a?(String)
  return tab_actions.index {|action| action.name == tab_action_or_string_or_hash[:name] } if tab_action_or_string_or_hash.is_a?(Hash)
  return tab_actions.index(tab_action_or_string_or_hash) if tab_action_or_string_or_hash.is_a?(FunkyTabs::TabAction)
  raise FunkyTabsException.new("Dont know how to find an action with #{tab_action_or_string_or_hash.inspect}")
end

#set_attrs(attrs_hash) ⇒ Object

Raises:



32
33
34
35
36
37
38
39
# File 'lib/funky_tabs/tab.rb', line 32

def set_attrs(attrs_hash)
  raise FunkyTabsException.new("invalid tab attrs format #{attrs_hash.inspect}") unless attrs_hash.is_a?(Hash)
  # in order to make sure that default action gets called after actions have been instantiated, sort hash keys
  attrs_hash.keys.sort.each do |key|
    self.send("#{key.to_s}=",attrs_hash[key])
  end
  return self
end