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
- #available_from(line) ⇒ Object
- #extract_attribute(line:) ⇒ Hash
- #generate_devices ⇒ Array
- #id_from(line) ⇒ Object
-
#initialize ⇒ LineParser
constructor
A new instance of LineParser.
- #name_from(line) ⇒ Object
- #push(line) ⇒ Object
Constructor Details
#initialize ⇒ LineParser
Returns a new instance of LineParser.
95 96 97 |
# File 'lib/fusuma/device.rb', line 95 def initialize @lines = [] end |
Instance Attribute Details
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
93 94 95 |
# File 'lib/fusuma/device.rb', line 93 def lines @lines end |
Instance Method Details
#available_from(line) ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'lib/fusuma/device.rb', line 146 def available_from(line) # NOTE: is natural scroll available? if line =~ /^Nat.scrolling: / return false if line =~ %r{n/a} return true # disabled / enabled end nil end |
#extract_attribute(line:) ⇒ Hash
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/fusuma/device.rb', line 122 def extract_attribute(line:) if (id = id_from(line)) { id: id } elsif (name = name_from(line)) { name: name } elsif (available = available_from(line)) { available: available } else {} end end |
#generate_devices ⇒ Array
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/fusuma/device.rb', line 105 def generate_devices device = Device.new lines.each_with_object([]) do |line, devices| attributes = extract_attribute(line: line) # detect new line including device name if attributes[:name] && (device&.name != attributes[:name]) devices << device device = Device.new end devices.last.assign_attributes(attributes) end end |
#id_from(line) ⇒ Object
134 135 136 137 138 |
# File 'lib/fusuma/device.rb', line 134 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
140 141 142 143 144 |
# File 'lib/fusuma/device.rb', line 140 def name_from(line) line.match('^Device:[[:space:]]*') do |m| m.post_match.strip end end |
#push(line) ⇒ Object
100 101 102 |
# File 'lib/fusuma/device.rb', line 100 def push(line) lines.push(line) end |