Class: Hotdog::Commands::Tags

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

Instance Attribute Summary

Attributes inherited from BaseCommand

#application, #logger, #options, #persistent_db_path

Instance Method Summary collapse

Methods inherited from BaseCommand

#define_options, #execute, #fixed_string?, #initialize, #parse_options, #reload

Constructor Details

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

Instance Method Details

#run(args = [], options = {}) ⇒ 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
# File 'lib/hotdog/commands/tags.rb', line 6

def run(args=[], options={})
  if args.empty?
    result = execute("SELECT name, value FROM tags").map { |name, value| [join_tag(name, value)] }
    show_tags(result)
  else
    tags = args.map { |tag| split_tag(tag) }
    if tags.all? { |_tag_name, tag_value| tag_value.empty? }
      result = tags.each_slice(SQLITE_LIMIT_COMPOUND_SELECT).flat_map { |tags|
        q = "SELECT value FROM tags " \
              "WHERE %s;" % tags.map { |tag_name, _tag_value| glob?(tag_name) ? "LOWER(name) GLOB LOWER(?)" : "name = ?" }.join(" OR ")
        execute(q, tags.map { |tag_name, _tag_value| tag_name }).map { |value| [value] }
      }
    else
      result = tags.each_slice(SQLITE_LIMIT_COMPOUND_SELECT / 2).flat_map { |tags|
        q = "SELECT value FROM tags " \
              "WHERE %s;" % tags.map { |tag_name, tag_value| (glob?(tag_name) or glob?(tag_value)) ? "( LOWER(name) GLOB LOWER(?) AND LOWER(value) GLOB LOWER(?) )" : "( name = ? AND value = ? )" }.join(" OR ")
        execute(q, tags).map { |value| [value] }
      }
    end
    if result.empty?
      STDERR.puts("no match found: #{args.join(" ")}")
      exit(1)
    else
      show_tags(result)
      logger.info("found %d tag(s)." % result.length)
      if result.length < args.length
        STDERR.puts("insufficient result: #{args.join(" ")}")
        exit(1)
      end
    end
  end
end

#show_tags(tags) ⇒ Object



39
40
41
# File 'lib/hotdog/commands/tags.rb', line 39

def show_tags(tags)
  STDOUT.print(format(tags, fields: ["tag"]))
end