class Aka::ShortcutManager

Public Class Methods

new(shortcuts) click to toggle source
# File lib/aka/shortcut_manager.rb, line 3
def initialize(shortcuts)
  @shortcuts = shortcuts
end

Public Instance Methods

add(shortcut) click to toggle source
# File lib/aka/shortcut_manager.rb, line 7
def add(shortcut)
  @shortcuts << shortcut
end
by_tag() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) { |tag, rows| ... } click to toggle source
# 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) click to toggle source
# File lib/aka/shortcut_manager.rb, line 11
def remove(shortcut)
  @shortcuts.delete_if do |item|
    item == shortcut
  end
end