Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/hodmin/hodmin_tools.rb,
lib/hodmin/hodmin_tools.rb

Overview

Extends Array class with some specific selection-methods for devices and firmware

Instance Method Summary collapse

Instance Method Details

#create_output_table(attribs, _style) ⇒ Object

Creates an array of rows with desired output from HomiePair-objects.



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/hodmin/hodmin_tools.rb', line 292

def create_output_table(attribs, _style)
  pastel = Pastel.new
  empty_field = ' '
  empty_field = configatron.output.nil.strip unless configatron.output.nil? || configatron.output.nil.nil?
  empty_field = pastel.dim(empty_field) # dim this message
  rows = []
  each do |r|
    row = []
    # color for checksum if applicable:
    checksum_color = if r.hdev.nil?
                       'none'
                     else
                       r.hdev.upgradable ? 'yellow' : 'green'
                     end
    attribs.each do |a|
      row << case a.slice(0, 2)
             when 'HD' then
               var_name = a.gsub(/HD./, '')
               var = r.hdev.nil? ? empty_field : r.hdev.instance_variable_get("@#{a.gsub(/HD./, '')}")
               case var_name
               when 'online'
                 var = pastel.green(var) if var == 'true'
                 var = pastel.red(var) if var == 'false'
                 var if var != 'true' && var != 'false'
               else
                 var = var.nil? ? empty_field : var
               end
             when 'FW' then
               if r.hfw.nil?
                 empty_field
               else
                 var_name = a.gsub(/FW./, '')
                 var = r.hfw.instance_variable_get("@#{var_name}")
                 case var_name
                 when 'checksum'
                   case checksum_color
                   when 'none' then var
                   when 'green' then pastel.green(var)
                   when 'yellow' then pastel.yellow(var)
                   end
                 else
                   var.nil? ? empty_field : var
                 end
               end
             when 'AD' then
               var = r.instance_variable_get("@#{a.gsub(/AD./, '')}")
               var.nil? ? empty_field : var
             end
    end
    rows << row
  end
  rows
end

#find_pattern(binfile) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/hodmin/hodmin_tools.rb', line 60

def find_pattern(binfile)
  result = ''
  if binfile.include?(first)
    result = binfile.split(first)[1].split(last).first.to_s
    result = [result].pack('H*') unless result.empty?
  end
  result
end

#select_by_opts(options) ⇒ Object

Selects Array of HomieDevices or firmwares based on options



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/hodmin/hodmin_tools.rb', line 267

def select_by_opts(options)
  this_object = first.class == HomieDevice ? 'HD' : 'FW'

  # Options valid for selecting Homie-Devices OR for firmwares
  valid_dev_options =
    this_object == 'HD' ? [:mac, :fw_name, :checksum, :localip] : [:checksum, :fw_name, :config]

  # use only valid options:
  my_opts = options.select { |k, _v| valid_dev_options.include?(k) }

  # remove all options not used as CLI argument:
  my_opts = my_opts.select { |_k, v| !v.to_s.empty? }
  return self if my_opts.empty? # no options set, so all devices are selected

  my_devs = self
  # selects objects (devices or firmwares) from an array due to a filter defined by key-value-pair
  # Example: [:checksum => 'c79*']
  my_opts.each_pair do |k, v|
    # puts "looking for #{k} = #{v}"
    my_devs = my_devs.select { |h| SelectObject.new(v) =~ h.instance_variable_get("@#{k}") }
  end
  my_devs
end