Class: Fusuma::Device::LineParser
- Inherits:
-
Object
- Object
- Fusuma::Device::LineParser
- Defined in:
- lib/fusuma/device.rb
Overview
parse line and generate devices
Instance Attribute Summary collapse
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
Instance Method Summary collapse
- #generate_devices ⇒ Object
- #id_from(line) ⇒ Object
-
#initialize ⇒ LineParser
constructor
A new instance of LineParser.
- #name_from(line) ⇒ Object
- #natural_scroll_is_available?(line) ⇒ Boolean
- #parse(device:, line:) ⇒ Device
- #push(line) ⇒ Object
Constructor Details
#initialize ⇒ LineParser
Returns a new instance of LineParser.
66 67 68 |
# File 'lib/fusuma/device.rb', line 66 def initialize @lines = [] end |
Instance Attribute Details
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
64 65 66 |
# File 'lib/fusuma/device.rb', line 64 def lines @lines end |
Instance Method Details
#generate_devices ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/fusuma/device.rb', line 75 def generate_devices device = nil lines.each_with_object([]) do |line, devices| device ||= Device.new device = parse(device: device, line: line) if device.available devices << device device = nil end end end |
#id_from(line) ⇒ Object
99 100 101 102 103 |
# File 'lib/fusuma/device.rb', line 99 def id_from(line) line.match('^Kernel:[[:space:]]*') do |m| m.post_match.match(/event[0-9]+/).to_s end end |
#name_from(line) ⇒ Object
105 106 107 108 109 |
# File 'lib/fusuma/device.rb', line 105 def name_from(line) line.match('^Device:[[:space:]]*') do |m| m.post_match.strip end end |
#natural_scroll_is_available?(line) ⇒ Boolean
111 112 113 114 115 |
# File 'lib/fusuma/device.rb', line 111 def natural_scroll_is_available?(line) return false unless line =~ /^Nat.scrolling: / return false if line =~ %r{n/a} true end |
#parse(device:, line:) ⇒ Device
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/fusuma/device.rb', line 88 def parse(device:, line:) if (id = id_from(line)) device.id = id elsif (name = name_from(line)) device.name = name elsif (available = natural_scroll_is_available?(line)) device.available = available end device end |
#push(line) ⇒ Object
71 72 73 |
# File 'lib/fusuma/device.rb', line 71 def push(line) lines.push(line) end |