lib/fusuma/device.rb in fusuma-0.10.2 vs lib/fusuma/device.rb in fusuma-0.11.0

- old
+ new

@@ -9,10 +9,24 @@ @id = id @name = name @available = available end + # @param [Hash] + def assign_attributes(attributes) + attributes.each do |k, v| + case k + when :id + self.id = v + when :name + self.name = v + when :available + self.available = v + end + end + end + class << self # @return [Array] def ids available.map(&:id) end @@ -36,11 +50,11 @@ def reset @available = nil end - # @params [String] + # @param [String] def given_device=(name) return if name.nil? @available = available.select { |d| d.name == name } return unless names.empty? MultiLogger.error("Device #{name} is not found.\n @@ -70,32 +84,35 @@ # @param line [String] def push(line) lines.push(line) end + # @return [Array] def generate_devices device = nil lines.each_with_object([]) do |line, devices| device ||= Device.new - device = parse(device: device, line: line) + device.assign_attributes extract_attribute(line: line) if device.available devices << device device = nil end end end - # @return [Device] - def parse(device:, line:) + # @param [String] + # @return [Hash] + def extract_attribute(line:) if (id = id_from(line)) - device.id = id + { id: id } elsif (name = name_from(line)) - device.name = name - elsif (available = natural_scroll_is_available?(line)) - device.available = available + { name: name } + elsif (available = available?(line)) + { available: available } + else + {} end - device end def id_from(line) line.match('^Kernel:[[:space:]]*') do |m| m.post_match.match(/event[0-9]+/).to_s @@ -106,10 +123,11 @@ line.match('^Device:[[:space:]]*') do |m| m.post_match.strip end end - def natural_scroll_is_available?(line) + def available?(line) + # NOTE: natural scroll is available? return false unless line =~ /^Nat.scrolling: / return false if line =~ %r{n/a} true end end