class AbideDataProcessor::Parser::ProcessorObject
This class holds all base attributes and methods for every syntax object. rubocop:disable Metrics/ClassLength
Attributes
Public Class Methods
# File lib/abide-data-processor/parser.rb, line 283 def initialize(name, data, control_maps) @name = name @data = validate_hiera_data(data) @control_maps = validate_control_maps(control_maps) @dependent = Set.new initialize_metaparams(@data, @control_maps) end
Public Instance Methods
Returns any Resource
objects that must be ordered after this object. @return [Array] The Resources that must be ordered after this object.
# File lib/abide-data-processor/parser.rb, line 314 def after_me defined?(@after_me) ? @after_me : initialize_after_me end
Returns any Resource
objects that must be ordered before this object. @return [Array] The Resources that must be ordered before this object.
# File lib/abide-data-processor/parser.rb, line 308 def before_me defined?(@before_me) ? @before_me : initialize_before_me end
Gives a more detailed String representation of this object. @return [String] The class, object id, and name of this object.
# File lib/abide-data-processor/parser.rb, line 326 def inspect "#<#{self.class.name}:#{object_id} '#{@name}'>" end
Determines if the name supplied is equal to the name of the object. This is overridden by subclasses to implement name mapped matches. @param name [String] The name to be compared to the object’s name. @return [Boolean] True if the name is equal to the object’s name, false otherwise.
# File lib/abide-data-processor/parser.rb, line 295 def name?(_name) raise NotImplementedError, 'This method must be implemented by a subclass' end
Abstract method to be implemented by subclasses. Returns a representation of this object as a Hash usable by Puppet’s create_resources function.
# File lib/abide-data-processor/parser.rb, line 302 def resource_data raise NotImplementedError, 'This method must be implemented by a subclass' end
Converts this object to a String. @return [String] The class and name of this object.
# File lib/abide-data-processor/parser.rb, line 320 def to_s "#{self.class.name}('#{@name}')" end
Private Instance Methods
This method adds the supplied Resource
to the inverse ordering list of this object based on the supplied metaparameter. This method is never directly used by an object on itself, rather it is called by other objects when they establish ordering relationships with this object. Because this method is private, other objects must use ‘send` to call it. @param metaparam [String] The metaparameter to inverse @param resource [Resource] The Resource
to be added to the inverse ordering list.
# File lib/abide-data-processor/parser.rb, line 395 def add_inverse_ordered_resource(metaparam, resource) if %w[require subscribe].include?(metaparam) add_after_me(resource) elsif %w[before notify].include?(metaparam) add_before_me(resource) end end
This method calculates the ordering of this object based on the ordering of the controls that are defined for this object. @param order_function [String] The function to use to calculate the ordering (before_me
or after_me
). @return [Array] The list of Resources gathered from the order function return of all controls.
# File lib/abide-data-processor/parser.rb, line 407 def calculate_ordered_controls(order_function) @controls.each_with_object([]) do |control, ary| ary << control.send(order_function.to_sym) end end
This method calculates the ordering of this object based on the the supplied metaparameters. This function is used with “like pairs” of metaparameters, such as “require” and “subscribe”. @param metaparameters [Array] The metaparameters to use to calculate the ordering. @return [Array] The list of Resources gathered from this object’s metaparameters.
# File lib/abide-data-processor/parser.rb, line 418 def calculate_self_ordering(*metaparams) ordered = metaparams.each_with_object([]) do |mparam, ary| next unless send("#{mparam}?".to_sym) ordered_resources = send(mparam.to_sym) ordered_resources.each { |r| r.send(:add_inverse_ordered_resource, mparam, self) } ary << ordered_resources end normalize_resource_array(ordered) end
Defines singleton methods for this instance of ProcessorObject
that are used to determine if the metaparameter is set. @param param [String] The metaparameter that will have boolean methods defined. @param raw_value [Boolean] The boolean value for the <metaparam>_raw? method. @param value [Boolean] The boolean value for the <metaparam>? method.
# File lib/abide-data-processor/parser.rb, line 478 def define_metaparam_bool_methods(param, raw_value, value) define_singleton_method("#{param}_raw?".to_sym) { raw_value } define_singleton_method("#{param}?".to_sym) { value } end
Returns the mapped names for the given control identifier. @param identifier [String] The control identifier to be mapped. @return [Array] The mapped names for the given control identifier.
# File lib/abide-data-processor/parser.rb, line 486 def find_mapped_names(identifier) @control_maps.each do |control_map| return control_map[identifier] if control_map.include?(identifier) end [] end
Initializes the after_me
instance variable with a list of Resources that must be ordered after this object. @return [Array] The list of Resources that must be ordered after this object.
# File lib/abide-data-processor/parser.rb, line 381 def initialize_after_me ctrls = @controls ? calculate_ordered_controls('after_me') : [] this = calculate_self_ordering('notify', 'subscribe') @after_me = normalize_resource_array(this.concat(ctrls)) @after_me end
Initilizes the before_me
instance variable with a list of Resources that must be ordered before this object. @return [Array] The list of Resources that must be ordered before this object.
# File lib/abide-data-processor/parser.rb, line 371 def initialize_before_me ctrls = @controls ? calculate_ordered_controls('before_me') : [] this = calculate_self_ordering('require', 'subscribe') @before_me = normalize_resource_array(this.concat(ctrls)) @before_me end
Initializes any relevant metaparameters based on the data supplied. @param data [Hash] The resource data to be parsed. @param control_maps [Array] The control maps to be used.
# File lib/abide-data-processor/parser.rb, line 357 def initialize_metaparams(data, control_maps) METAPARAMS.each do |param| metaparam_data = data == :no_params ? {} : data[param] raw_mdata = data == :no_params ? {} : data value, bool_value = parse_metaparam(metaparam_data, control_maps) raw_value, raw_bool_value = parse_raw_metaparam(raw_mdata, param) set_metaparam_instance_vars(param, value, raw_value) define_metaparam_bool_methods(param, raw_bool_value, bool_value) end end
This method normalizes an array of Resources, or anything really, by flattening it, removing any nil values, removing any duplicates, and rejecting any empty objects if they respond to ‘empty?`. It then returns the new array. @param resources [Array] The array of Resources to be normalized. @return [Array] The normalized array of Resources.
# File lib/abide-data-processor/parser.rb, line 338 def normalize_resource_array(array) array.flatten.compact.uniq.reject { |r| r.empty? if r.respond_to?(:empty?) } end
This method normalizes an array of Resources, or anything really, by flattening it, removing any nil values, removing any duplicates, and rejecting any empty objects if they respond to ‘empty?`. It does this in place, directly modifying the input array. @param resources [Array] The array of Resources to be normalized.
# File lib/abide-data-processor/parser.rb, line 347 def normalize_resource_array!(array) array.flatten! array.compact! array.uniq! array.reject! { |r| r.empty? if r.respond_to?(:empty?) } end
Adds a each dependent control from a list of dependent controls to the @dependent instance variable. @param value [Array] The dependent controls to be added to the @dependent instance variable.
# File lib/abide-data-processor/parser.rb, line 449 def parse_dependent_param(value) value.each { |x| @dependent.add(x) } end
Returns appropriate values for instance variables of the given metaparam based off the supplied value. @param value [Array] The metaparameter declaration value from Hiera. @param control_maps [Array] The relevant control maps used in Resource
creation. @return [Array] Values for the instance variables of the given metaparam. The order of the values
is: Resource collection value, boolean value.
# File lib/abide-data-processor/parser.rb, line 435 def parse_metaparam(value, control_maps) return [nil, false] unless not_nil_or_empty?(value) return parse_dependent_param(value) if value.is_a?(Array) objects = value.each_with_object([]) do |(k, v), a| a << AbideDataProcessor::Parser.new_resource(k, v, control_maps) end [normalize_resource_array(objects), !objects.empty?] end
Returns appropriate raw value for instance variables of the given metaparam based off the supplied value. The raw value is the text values for the metaparameter declaration supplied via the resource data. @param data [Hash] The resource data to be parsed. @param param [String] The metaparameter to be parsed. @return [Array] Values for the instance variables of the given metaparam. The order of the values
is: raw value, boolean raw value.
# File lib/abide-data-processor/parser.rb, line 459 def parse_raw_metaparam(data, param) raw_value = data.fetch(param, nil) [raw_value, (!raw_value.nil? && !raw_value.empty?)] end
Sets the instance variables of the given metaparam based off the supplied values. @param param [String] The metaparameter to be set. @param value [Array] The Resource
value to be set. @param raw_value [Array] The raw value to be set.
# File lib/abide-data-processor/parser.rb, line 468 def set_metaparam_instance_vars(param, value, raw_value) instance_variable_set("@#{param}", value) instance_variable_set("@#{param}_raw", raw_value) end