Class: Fusuma::Device::LineParser

Inherits:
Object
  • Object
show all
Defined in:
lib/fusuma/device.rb

Overview

parse line and generate devices

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLineParser

Returns a new instance of LineParser.



81
82
83
# File 'lib/fusuma/device.rb', line 81

def initialize
  @lines = []
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



79
80
81
# File 'lib/fusuma/device.rb', line 79

def lines
  @lines
end

Instance Method Details

#available_from(line) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/fusuma/device.rb', line 140

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



134
135
136
137
138
# File 'lib/fusuma/device.rb', line 134

def capabilities_from(line)
  line.match("^Capabilities:[[:space:]]*") do |m|
    m.post_match.strip
  end
end

#extract_attribute(line:) ⇒ Hash

Parameters:

Returns:



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fusuma/device.rb', line 108

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_devicesArray

Returns:

  • (Array)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fusuma/device.rb', line 91

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



122
123
124
125
126
# File 'lib/fusuma/device.rb', line 122

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



128
129
130
131
132
# File 'lib/fusuma/device.rb', line 128

def name_from(line)
  line.match("^Device:[[:space:]]*") do |m|
    m.post_match.strip
  end
end

#push(line) ⇒ Object

Parameters:



86
87
88
# File 'lib/fusuma/device.rb', line 86

def push(line)
  lines.push(line)
end