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.



442
443
444
445
446
# File 'lib/abide_dev_utils/xccdf.rb', line 442

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



448
449
450
451
452
453
454
# File 'lib/abide_dev_utils/xccdf.rb', line 448

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)


456
457
458
459
460
# File 'lib/abide_dev_utils/xccdf.rb', line 456

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



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/abide_dev_utils/xccdf.rb', line 469

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



462
463
464
465
466
467
# File 'lib/abide_dev_utils/xccdf.rb', line 462

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