class MacShortcuts::Shortcut

Constants

CODE_MAP
KEYS_MAP
PRETTY_MAP

Attributes

alt[RW]
command[RW]
control[RW]
key[RW]
shift[RW]

Public Class Methods

from_code(shortcut_str) click to toggle source

@param [String] shortcut_str

# File lib/mac_shortcuts/shortcut.rb, line 35
def self.from_code(shortcut_str)
  inst = self.new
  CODE_MAP.each do |key, value|
    inst.send("#{key}=", shortcut_str.include?(value))
  end
  inst.key = shortcut_str[-1]
  inst
end
from_pretty(shortcut_str) click to toggle source

@param [String] shortcut_str

# File lib/mac_shortcuts/shortcut.rb, line 46
def self.from_pretty(shortcut_str)
  inst = self.new
  components = components_from_pretty(shortcut_str)
  components.each do |cmp|
    case cmp
    when String
      inst.key = cmp
    when Symbol
      inst.send("#{cmp}=", true)
    end
  end
  inst
end
new() click to toggle source
# File lib/mac_shortcuts/shortcut.rb, line 27
def initialize
  @command = @shift = @control = @alt = false
  @key = nil
end

Private Class Methods

components_from_pretty(str) click to toggle source

@param str [String]

@return [Array<Symbol, String>]

# File lib/mac_shortcuts/shortcut.rb, line 100
def self.components_from_pretty(str)
  components = str.split(/( |\+)/)
  components.reject! { |cmp| cmp.strip.empty? }
  components.reject! { |cmp| cmp == '+' }
  last = components.pop

  correct_last = KEYS_MAP[last] || last

  components.map! do |cmp|
    key = key_for_pretty(cmp)
    if key.nil?
      raise "Unknown text for #{cmp}"
    end
    key
  end
  components + [correct_last]
end
key_for_pretty(cmp) click to toggle source

@param cmp [String]

@return [Symbol, nil]

# File lib/mac_shortcuts/shortcut.rb, line 85
def self.key_for_pretty(cmp)
  key = nil
  PRETTY_MAP.each do |curr_key, values|
    if values.include?(cmp)
      key = curr_key
      break
    end
  end
  key
end

Public Instance Methods

to_code() click to toggle source
# File lib/mac_shortcuts/shortcut.rb, line 70
def to_code
  components = []
  CODE_MAP.each do |key, value|
    components << value if self.send(key)
  end
  components << @key if @key
  components.join
end
to_s() click to toggle source
# File lib/mac_shortcuts/shortcut.rb, line 61
def to_s
  components = []
  PRETTY_MAP.each do |key, value|
    components << value.first if self.send(key)
  end
  components << @key if @key
  components.join(' + ')
end