Class: AbideDevUtils::XCCDF::ObjectContainer

Inherits:
XccdfObject
  • Object
show all
Includes:
Common
Defined in:
lib/abide_dev_utils/xccdf.rb

Direct Known Subclasses

CisControls, Profiles, StigControls

Constant Summary

Constants included from Common

Common::CIS_CONTROL_NUMBER, Common::CIS_CONTROL_PARTS, Common::CIS_LEVEL_CODE, Common::CIS_NEXT_GEN_WINDOWS, Common::CIS_PROFILE_PARTS, Common::CIS_TITLE_MARKER, Common::CONTROL_PARTS, Common::CONTROL_PREFIX, Common::PROFILE_PARTS, Common::STIG_CONTROL_PARTS, Common::STIG_PROFILE_PARTS, Common::STIG_TITLE_MARKER, Common::UNDERSCORED, Common::XPATHS

Instance Method Summary collapse

Methods included from Common

#==, #abide_object?, #control_parts, #control_profile_text, #name_normalize_control, #normalize_control_name, #normalize_profile_name, #normalize_string, #number_normalize_control, #profile_parts, #text_normalize, #validate_xccdf, #xpath

Methods inherited from XccdfObject

#control_class, #control_sort_key, #controls_class

Constructor Details

#initialize(list, object_creation_method, benchmark, *args, **kwargs) ⇒ ObjectContainer

Returns a new instance of ObjectContainer.



433
434
435
436
437
# File 'lib/abide_dev_utils/xccdf.rb', line 433

def initialize(list, object_creation_method, benchmark, *args, **kwargs)
  super(benchmark)
  @object_list = send(object_creation_method.to_sym, list, benchmark, *args, **kwargs)
  @searchable = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



439
440
441
442
443
444
445
# File 'lib/abide_dev_utils/xccdf.rb', line 439

def method_missing(m, *args, &block)
  property = m.to_s.start_with?('search_') ? m.to_s.split('_')[-1].to_sym : nil
  return search(property, *args, &block) if property && @searchable.include?(property)
  return @object_list.send(m, *args, &block) if @object_list.respond_to?(m)

  super
end

Instance Method Details

#respond_to_missing?(m, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


447
448
449
450
451
# File 'lib/abide_dev_utils/xccdf.rb', line 447

def respond_to_missing?(m, include_private = false)
  return true if m.to_s.start_with?('search_') && @searchable.include?(m.to_s.split('_')[-1].to_sym)

  super
end

#search(property, item) ⇒ Object



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/abide_dev_utils/xccdf.rb', line 460

def search(property, item)
  max = @object_list.length - 1
  min = 0
  while min <= max
    mid = (min + max) / 2
    return @object_list[mid] if @object_list[mid].send(property.to_sym) == item

    if @object_list[mid].send(property.to_sym) > item
      max = mid - 1
    else
      min = mid + 1
    end
  end
  nil
end

#to_hObject



453
454
455
456
457
458
# File 'lib/abide_dev_utils/xccdf.rb', line 453

def to_h
  @object_list.each_with_object({}) do |obj, self_hash|
    key = resolve_hash_key(obj)
    self_hash[key] = obj.to_h
  end
end