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
: (String) -> bool?.
-
#capabilities_from(line) ⇒ Object
: (String) -> String?.
-
#extract_attribute(line:) ⇒ Hash
: (line: String) -> Hash[untyped, untyped].
-
#generate_devices ⇒ Array
: () -> Array.
-
#id_from(line) ⇒ Object
: (String) -> String?.
-
#initialize ⇒ LineParser
constructor
: () -> void.
-
#name_from(line) ⇒ Object
: (String) -> String?.
-
#push(line) ⇒ Object
: (String) -> Array.
Constructor Details
#initialize ⇒ LineParser
: () -> void
87 88 89 |
# File 'lib/fusuma/device.rb', line 87 def initialize @lines = [] end |
Instance Attribute Details
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
84 85 86 |
# File 'lib/fusuma/device.rb', line 84 def lines @lines end |
Instance Method Details
#available_from(line) ⇒ Object
: (String) -> bool?
153 154 155 156 157 158 159 160 161 |
# File 'lib/fusuma/device.rb', line 153 def available_from(line) # NOTE: is natural scroll available? if /^Nat.scrolling: /.match?(line) return false if %r{n/a}.match?(line) return true # disabled / enabled end nil end |
#capabilities_from(line) ⇒ Object
: (String) -> String?
146 147 148 149 150 |
# File 'lib/fusuma/device.rb', line 146 def capabilities_from(line) line.match("^Capabilities:[[:space:]]*") do |m| m.post_match.strip end end |
#extract_attribute(line:) ⇒ Hash
: (line: String) -> Hash[untyped, untyped]
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/fusuma/device.rb', line 117 def extract_attribute(line:) if (id = id_from(line)) {id: id} elsif (name = name_from(line)) {name: name} elsif (capabilities = capabilities_from(line)) {capabilities: capabilities} elsif (available = available_from(line)) {available: available} else {} end end |
#generate_devices ⇒ Array
: () -> Array
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/fusuma/device.rb', line 99 def generate_devices lines.each_with_object([]) do |line, devices| attributes = extract_attribute(line: line) next if attributes == {} if attributes[:name] # when detected new line including device name devices << Device.new # next device end devices.last.assign_attributes(attributes) unless devices.empty? end end |
#id_from(line) ⇒ Object
: (String) -> String?
132 133 134 135 136 |
# File 'lib/fusuma/device.rb', line 132 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
: (String) -> String?
139 140 141 142 143 |
# File 'lib/fusuma/device.rb', line 139 def name_from(line) line.match("^Device:[[:space:]]*") do |m| m.post_match.strip end end |
#push(line) ⇒ Object
: (String) -> Array
93 94 95 |
# File 'lib/fusuma/device.rb', line 93 def push(line) lines.push(line) end |