Sha256: a3386824f82cc3a7968103a91407ae3a2703b43e3e40f8358d987512e4537fc7

Contents?: true

Size: 1.82 KB

Versions: 3

Compression:

Stored size: 1.82 KB

Contents

# module as namespace
module Fusuma
  require 'singleton'
  # read keymap from yaml file
  class Config
    include Singleton

    class << self
      def shortcut(gesture_info)
        instance.shortcut(gesture_info)
      end

      def threshold(action_type)
        instance.threshold(action_type)
      end

      def reload
        instance.reload
      end
    end

    def initialize
      reload
    end
    attr_accessor :keymap

    def reload
      @cache  = nil
      @keymap = YAML.load_file(file_path)
      self
    end

    def shortcut(gesture_info)
      seek_index = [*action_index(gesture_info), 'shortcut']
      cache(seek_index) { search_config(keymap, seek_index) }
    end

    def threshold(action_type)
      seek_index = ['threshold', action_type]
      cache(seek_index) { search_config(keymap, seek_index) } || 1
    end

    private

    def search_config(keymap_node, seek_index)
      if seek_index == []
        return nil if keymap_node.is_a? Hash
        return keymap_node
      end
      key = seek_index[0]
      child_node = keymap_node[key]
      next_index = seek_index[1..-1]
      return search_config(child_node, next_index) if child_node
      search_config(keymap_node, next_index)
    end

    def file_path
      filename = 'fusuma/config.yml'
      original_path = File.expand_path "~/.config/#{filename}"
      default_path  = File.expand_path "../../#{filename}", __FILE__
      File.exist?(original_path) ? original_path : default_path
    end

    def action_index(gesture_info)
      action_type = gesture_info.action_type
      finger      = gesture_info.finger
      direction   = gesture_info.direction
      [action_type, finger, direction]
    end

    def cache(key)
      @cache ||= {}
      key = key.join(',') if key.is_a? Array
      @cache[key] ||= block_given? ? yield : nil
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fusuma-0.2.5 lib/fusuma/config.rb
fusuma-0.2.3 lib/fusuma/config.rb
fusuma-0.2.2 lib/fusuma/config.rb