Sha256: 83c53062f1f4c6d5be5d171b0bfb2656c2d42141852e08376681f496842527e4

Contents?: true

Size: 1.33 KB

Versions: 4

Compression:

Stored size: 1.33 KB

Contents

require "karabiner/dsl/item"
require "karabiner/xml_tree"

class Karabiner::Item
  include Karabiner::XmlTree
  include Karabiner::DSL::Item

  AVAILABLE_OPTIONS = %i[
    not
    only
    config_not
    config_only
    device_not
    device_only
  ].freeze

  def initialize(name, options = {})
    @skip_identifier = options.delete(:skip_identifier)

    if name
      property = Karabiner::Property.new("name", name)
      add_child(property)
    end

    options.each do |option, value|
      raise "Unavailable option: #{option}" unless AVAILABLE_OPTIONS.include?(option)

      property = Karabiner::Property.new(option, value)
      add_child(property)
    end
  end

  def to_xml
    validate_name_existence
    generate_identifier unless @skip_identifier

    super
  end

  private

  def validate_name_existence
    properties = search_childs(Karabiner::Property)
    raise "Name property does not exist" unless properties.map(&:attr).include?("name")
  end

  def generate_identifier
    properties = search_childs(Karabiner::Property)
    return if properties.map(&:attr).include?("identifier")

    name = properties.find { |p| p.attr == "name" }
    generated_identifier = name.value.gsub(/[^a-zA-Z]/, "_").downcase
    identifier = Karabiner::Property.new("identifier", "remap.#{generated_identifier}")
    childs[1, 0] = identifier
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
karabiner-0.4.0 lib/karabiner/item.rb
karabiner-0.3.1 lib/karabiner/item.rb
karabiner-0.3.0 lib/karabiner/item.rb
karabiner-0.2.1 lib/karabiner/item.rb