Class: AbideDevUtils::XCCDF::ObjectContainer

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

Direct Known Subclasses

Controls, Profiles

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::CONTROL_PREFIX, 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, #sorted_control_classes, #sorted_profile_classes, #text_normalize, #validate_xccdf, #xpath

Constructor Details

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

Returns a new instance of ObjectContainer.



355
356
357
358
# File 'lib/abide_dev_utils/xccdf.rb', line 355

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



360
361
362
363
364
365
366
# File 'lib/abide_dev_utils/xccdf.rb', line 360

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)


368
369
370
371
372
# File 'lib/abide_dev_utils/xccdf.rb', line 368

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



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/abide_dev_utils/xccdf.rb', line 381

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



374
375
376
377
378
379
# File 'lib/abide_dev_utils/xccdf.rb', line 374

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