Class: Aka::Shortcuts

Inherits:
Object
  • Object
show all
Defined in:
lib/aka/shortcuts.rb

Instance Method Summary collapse

Constructor Details

#initialize(shortcuts) ⇒ Shortcuts

Returns a new instance of Shortcuts.



3
4
5
# File 'lib/aka/shortcuts.rb', line 3

def initialize(shortcuts)
  @shortcuts = shortcuts.dup
end

Instance Method Details

#allObject



24
25
26
# File 'lib/aka/shortcuts.rb', line 24

def all
  @shortcuts.dup
end

#append(shortcut) ⇒ Object



7
8
9
# File 'lib/aka/shortcuts.rb', line 7

def append(shortcut)
  @shortcuts[count + 1] = shortcut
end

#by_tagObject



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/aka/shortcuts.rb', line 107

def by_tag
  @shortcuts.inject({ :default => [] }) do |acc, (_, row)|
    if row.tag && !row.tag.empty?
      row.tag.each do |tag|
        acc[tag] ||= []
        acc[tag] << row
      end
    else
      acc[:default] << row
    end
    acc
  end
end

#countObject



19
20
21
22
# File 'lib/aka/shortcuts.rb', line 19

def count
  result, _ = @shortcuts.max { |(n, _)| n }
  result || 0
end

#delete(key) ⇒ Object



15
16
17
# File 'lib/aka/shortcuts.rb', line 15

def delete(key)
  @shortcuts.delete(key)
end

#excluded_output(excluded) ⇒ Object



83
84
85
86
87
88
# File 'lib/aka/shortcuts.rb', line 83

def excluded_output(excluded)
  return if excluded[:tags].empty?

  tags = excluded[:tags].map { |t| '#' + t }.join(', ')
  $stderr.puts "#{excluded[:shortcuts]} shortcut(s) excluded (#{tags})."
end

#find(options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/aka/shortcuts.rb', line 28

def find(options)
  if options[:tag]
    @shortcuts.select do |_, row|
      next unless row.shortcut == options[:shortcut]

      options[:tag].find do |tag|
        row.tag && row.tag.include?(tag)
      end
    end
  else
    @shortcuts.select do |_, row|
      row.shortcut == options[:shortcut]
    end
  end
end

#generate(options) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/aka/shortcuts.rb', line 44

def generate(options)
  scripts = []
  functions = []

  excluded = match_by_tag(options[:tag] || []) do |tag, rows|
    rows.each do |row|
      unless row.function
        scripts << Configuration::Shortcut.generate_output(row)
      else
        functions << Configuration::Shortcut.generate_output(row)
      end
    end
  end

  if options[:output]
    File.open(File.expand_path(options[:output]), 'w+') do |f|
      scripts.each do |script|
        f.puts script
      end

      functions.each do |function|
        f.puts function
      end
    end

    puts "Generated #{options[:output]}."
  else
    scripts.each do |script|
      puts script
    end

    functions.each do |function|
      puts function
    end
  end

  excluded
end

#match_by_tag(tags, &blk) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/aka/shortcuts.rb', line 90

def match_by_tag(tags, &blk)
  excluded = { :tags => [], :shortcuts => 0 }

  by_tag.each do |tag, rows|
    next if rows.empty?
    unless tag == :default || tags.empty? || tags.include?(tag)
      excluded[:tags] << tag
      excluded[:shortcuts] += rows.length
      next
    end

    yield(tag, rows)
  end

  excluded
end

#replace(key, shortcut) ⇒ Object



11
12
13
# File 'lib/aka/shortcuts.rb', line 11

def replace(key, shortcut)
  @shortcuts[key] = shortcut
end