Class: Aka::ShortcutManager

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

Instance Method Summary collapse

Constructor Details

#initialize(shortcuts) ⇒ ShortcutManager

Returns a new instance of ShortcutManager.



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

def initialize(shortcuts)
  @shortcuts = shortcuts
end

Instance Method Details

#add(shortcut) ⇒ Object



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

def add(shortcut)
  @shortcuts << shortcut
end

#by_tagObject



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/aka/shortcut_manager.rb', line 95

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

#find(options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/aka/shortcut_manager.rb', line 17

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

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

#generate(options) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
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
# File 'lib/aka/shortcut_manager.rb', line 33

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

  excluded = match_by_tag(options[:tag] || []) do |tag, rows|
    rows.each do |row|
      unless row.function
        scripts << Shortcut.generate_output(row)
      else
        functions << 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

      f.puts "export AKA_VERSION=#{Aka::VERSION}"
      f.puts "export AKA_GENERATED_AT=#{Time.now.to_f}"
    end

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

    functions.each do |function|
      puts function
    end

    puts "export AKA_VERSION=#{Aka::VERSION}"
    puts "export AKA_GENERATED_AT=#{Time.now.to_f}"
  end

  excluded
end

#match_by_tag(tags, &blk) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/aka/shortcut_manager.rb', line 78

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

#remove(shortcut) ⇒ Object



11
12
13
14
15
# File 'lib/aka/shortcut_manager.rb', line 11

def remove(shortcut)
  @shortcuts.delete_if do |item|
    item == shortcut
  end
end