Class: Hotdog::Commands::Tags

Inherits:
BaseCommand show all
Defined in:
lib/hotdog/commands/tags.rb

Constant Summary

Constants inherited from BaseCommand

BaseCommand::MASK_DATABASE, BaseCommand::MASK_QUERY, BaseCommand::PERSISTENT_DB

Instance Attribute Summary

Attributes inherited from BaseCommand

#application, #logger, #options

Instance Method Summary collapse

Methods inherited from BaseCommand

#execute, #fixed_string?, #initialize, #reload

Constructor Details

This class inherits a constructor from Hotdog::Commands::BaseCommand

Instance Method Details

#run(args = []) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hotdog/commands/tags.rb', line 6

def run(args=[])
  args = optparse.parse(args)
  if 0 < args.length
    fields = args.map { |tag|
      tag_name, tag_value = split_tag(tag)
      tag_name
    }
    result1 = args.map { |tag|
      tag_name, tag_value = split_tag(tag)
      if glob?(tag_name)
        if tag_value.empty?
          execute("SELECT DISTINCT value FROM tags WHERE name GLOB ?", [tag_name]).map { |row| row.join(",") }
        else
          if glob?(tag_value)
            execute("SELECT DISTINCT value FROM tags WHERE name GLOB ? AND value GLOB ?", [tag_name, tag_value]).map { |row| row.join(",") }
          else
            execute("SELECT DISTINCT value FROM tags WHERE name GLOB ? AND value = ?", [tag_name, tag_value]).map { |row| row.join(",") }
          end
        end
      else
        if tag_value.empty?
          execute("SELECT DISTINCT value FROM tags WHERE name = ?", [tag_name]).map { |row| row.join(",") }
        else
          if glob?(tag_value)
            execute("SELECT DISTINCT value FROM tags WHERE name = ? AND value GLOB ?", [tag_name, tag_value]).map { |row| row.join(",") }
          else
            execute("SELECT DISTINCT value FROM tags WHERE name = ? AND value = ?", [tag_name, tag_value]).map { |row| row.join(",") }
          end
        end
      end
    }
    result = (0...result1.reduce(0) { |max, values| [max, values.length].max }).map { |field_index|
      result1.map { |values| values[field_index] }
    }
  else
    fields = ["tag"]
    result = execute("SELECT DISTINCT name, value FROM tags").map { |name, value| [0 < value.length ? "#{name}:#{value}" : name] }
  end
  if 0 < result.length
    STDOUT.print(format(result, fields: fields))
    logger.info("found %d tag(s)." % result.length)
  end
end