Class: Cohortly::TagConfig

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/cohortly/tag_config.rb

Defined Under Namespace

Classes: Controller, Tag

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_groupsObject

Returns the value of attribute _groups.



5
6
7
# File 'lib/cohortly/tag_config.rb', line 5

def _groups
  @_groups
end

#_tagsObject

Returns the value of attribute _tags.



5
6
7
# File 'lib/cohortly/tag_config.rb', line 5

def _tags
  @_tags
end

#lookup_tableObject

Returns the value of attribute lookup_table.



5
6
7
# File 'lib/cohortly/tag_config.rb', line 5

def lookup_table
  @lookup_table
end

Class Method Details

.all_groupsObject



62
63
64
# File 'lib/cohortly/tag_config.rb', line 62

def self.all_groups
  instance._groups.sort
end

.all_tagsObject



54
55
56
57
58
59
60
# File 'lib/cohortly/tag_config.rb', line 54

def self.all_tags
  if instance._tags
    instance._tags.collect {|x| x._name.to_s }
  else
    []
  end
end

.draw_tags(&block) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/cohortly/tag_config.rb', line 6

def self.draw_tags(&block)
  instance._tags = []
  instance._groups = []      
  instance.lookup_table = {}
  instance.instance_eval(&block)
  instance.compile!
  instance
end

.tags_for(controller, action = :_all) ⇒ Object



49
50
51
52
# File 'lib/cohortly/tag_config.rb', line 49

def self.tags_for(controller, action = :_all)
  return [] if controller.nil?
  instance.tags_for(controller, action)
end

Instance Method Details

#compile!Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cohortly/tag_config.rb', line 27

def compile!
  self._tags.each do |tag|
    tag._controllers.each do |cont|
      lookup_table[cont._name] ||= {}
      cont._acts.each do |a|
        lookup_table[cont._name][a] ||= []
        tag_names = lookup_table[cont._name][a] << tag._name
        lookup_table[cont._name][a] = tag_names.uniq
      end
    end
  end
end

#groups(*args) ⇒ Object



23
24
25
# File 'lib/cohortly/tag_config.rb', line 23

def groups(*args)
  self._groups = *args.collect(&:to_s)
end

#tag(tag_name, &block) ⇒ Object



15
16
17
# File 'lib/cohortly/tag_config.rb', line 15

def tag(tag_name, &block)
  self._tags << Tag.new(tag_name, &block)
end

#tags(*args, &block) ⇒ Object



19
20
21
# File 'lib/cohortly/tag_config.rb', line 19

def tags(*args, &block)
  args.each {|x| tag(x, &block) }
end

#tags_for(controller, action = :_all) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/cohortly/tag_config.rb', line 40

def tags_for(controller, action = :_all)
  res = []
  if lookup_table[controller.to_sym]
    res += lookup_table[controller.to_sym][action.to_sym] || []
    res += lookup_table[controller.to_sym][:_all] || []
  end
  res.uniq.collect &:to_s
end