Module: StickyFlag::TagFactory

Included in:
ThorApp
Defined in:
lib/stickyflag/tag_factory.rb

Constant Summary collapse

TAG_MODULES =
StickyFlag::Tags.constants.map { |sym| 
  StickyFlag::Tags.const_get(sym) 
}.select { |const| 
  const.is_a?(Module) && const.respond_to?(:extensions)
}
TAG_EXTENSIONS =
TAG_MODULES.map { |mod| mod.extensions }.reduce(:|)

Instance Method Summary collapse

Instance Method Details

#available_tagging_extensionsObject



22
23
24
# File 'lib/stickyflag/tag_factory.rb', line 22

def available_tagging_extensions
  TAG_EXTENSIONS
end

#call_tag_method(file_name, method, *args) ⇒ Object

Raises:

  • (Thor::Error)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/stickyflag/tag_factory.rb', line 26

def call_tag_method(file_name, method, *args)
  extension = File.extname file_name
  
  TAG_MODULES.each do |mod|
    if mod.send(:extensions).include? extension  
      if mod.respond_to?(:config_values)
        config_values = mod.send(:config_values)
        config_values.each { |val| args << get_config(val) }
      end
              
      return mod.send(method, file_name, *args)
    end
  end
  
  raise Thor::Error.new("ERROR: Don't know how to tag a file of extension '#{extension}'")
end

#clear_tags_for(file_name) ⇒ Object



63
64
65
66
# File 'lib/stickyflag/tag_factory.rb', line 63

def clear_tags_for(file_name)
  clear_database_tags file_name
  call_tag_method(file_name, :clear)
end

#get_tags_for(file_name) ⇒ Object



43
44
45
# File 'lib/stickyflag/tag_factory.rb', line 43

def get_tags_for(file_name)
  call_tag_method(file_name, :get)
end

#set_tag_for(file_name, tag) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/stickyflag/tag_factory.rb', line 47

def set_tag_for(file_name, tag)
  tags = get_tags_for file_name
  return if tags.include? tag

  set_database_tag file_name, tag
  call_tag_method(file_name, :set, tag)
end

#unset_tag_for(file_name, tag) ⇒ Object

Raises:

  • (Thor::Error)


55
56
57
58
59
60
61
# File 'lib/stickyflag/tag_factory.rb', line 55

def unset_tag_for(file_name, tag)
  tags = get_tags_for file_name
  raise Thor::Error.new("ERROR: Cannot unset tag #{tag} from file, not set") unless tags.include? tag

  unset_database_tag file_name, tag
  call_tag_method(file_name, :unset, tag)
end