class ProcessSettings::TargetedSettings

This class encapsulates an ordered collection of TargetAndSettings (each of which came from one YAML file).

Constants

KEY_NAMES

Attributes

targeted_settings_array[R]
version[R]

Public Class Methods

from_array(settings_array, only_meta: false) click to toggle source
# File lib/process_settings/targeted_settings.rb, line 42
def from_array(settings_array, only_meta: false)
  settings_array.is_a?(Array) or raise ArgumentError, "settings_array must be an Array of Hashes; got #{settings_array.inspect}"
  meta_hash = nil

  targeted_settings_array =
    settings_array.map do |settings_hash|
      settings_hash.is_a?(Hash) or raise ArgumentError, "settings_array entries must each be a Hash; got #{settings_hash.inspect}"

      meta_hash and raise ArgumentError, "\"meta\" marker must be at end; got #{settings_hash.inspect} after"
      if (meta_hash = settings_hash["meta"])
        meta_hash.to_a.last == ['END', true] or raise ArgumentError, "END: true must be at end of file; got #{meta_hash.inspect}"
        next
      end

      unless only_meta
        filename               = settings_hash["filename"]
        target_settings_hash   = settings_hash["target"] || true
        settings_settings_hash = settings_hash["settings"]

        settings_settings_hash or raise ArgumentError, "settings_array entries must each have 'settings' hash: #{settings_hash.inspect}"

        (extra_keys = settings_hash.keys - KEY_NAMES).empty? or
          raise ArgumentError, "settings_array entries must each have exactly these keys: #{KEY_NAMES.inspect}; got these extras: #{extra_keys.inspect}\nsettings_hash: #{settings_hash.inspect}"

        TargetAndSettings.from_json_docs(filename, target_settings_hash, settings_settings_hash)
      end
    end.compact

  meta_hash or raise ArgumentError, "Missing meta: marker at end; got #{settings_array.inspect}"

  new(targeted_settings_array, version: meta_hash['version'])
end
from_file(file_path, only_meta: false) click to toggle source
# File lib/process_settings/targeted_settings.rb, line 75
def from_file(file_path, only_meta: false)
  json_doc = Psych.load_file(file_path)
  from_array(json_doc, only_meta: only_meta)
end
new(targeted_settings_array, version:) click to toggle source
# File lib/process_settings/targeted_settings.rb, line 13
def initialize(targeted_settings_array, version:)
  targeted_settings_array.is_a?(Array) or raise ArgumentError, "targeted_settings_array must be an Array of Hashes; got #{targeted_settings_array.inspect}"
  targeted_settings_array.each do |target_and_settings|
    target_and_settings.is_a?(TargetAndSettings) or
      raise ArgumentError, "targeted_settings_array entries must each be a TargetAndProcessSettings; got #{target_and_settings.inspect}"
  end

  @targeted_settings_array = targeted_settings_array

  @version = version or raise ArgumentError, "version must not be empty"
end

Public Instance Methods

==(rhs) click to toggle source
# File lib/process_settings/targeted_settings.rb, line 25
def ==(rhs)
  to_json_doc == rhs.to_json_doc
end
eql?(rhs) click to toggle source
# File lib/process_settings/targeted_settings.rb, line 29
def eql?(rhs)
  self == rhs
end
matching_settings(context_hash) click to toggle source
# File lib/process_settings/targeted_settings.rb, line 81
def matching_settings(context_hash)
  @targeted_settings_array.select do |target_and_settings|
    target_and_settings.target.target_key_matches?(context_hash)
  end
end
settings_with_static_context(static_context_hash) click to toggle source
# File lib/process_settings/targeted_settings.rb, line 96
def settings_with_static_context(static_context_hash)
  result_settings =
    @settings_array.map do |target_and_settings|
      if (new_target = target.with_static_context(static_context_hash))
        TargetAndSettings.new(new_target, target_and_settings.settings)
      end
    end.compact

  self.class.new(result_settings)
end
to_json_doc() click to toggle source
# File lib/process_settings/targeted_settings.rb, line 33
def to_json_doc
  @targeted_settings_array.map(&:to_json_doc)
end
to_yaml() click to toggle source
# File lib/process_settings/targeted_settings.rb, line 37
def to_yaml
  to_json_doc.to_yaml
end
with_static_context(static_context_hash) click to toggle source

returns the collection of targeted_settings with target simplified based on given static_context_hash omits entries whose targeting is then false

# File lib/process_settings/targeted_settings.rb, line 89
def with_static_context(static_context_hash)
  @targeted_settings_array.map do |target_and_settings|
    new_target_and_process_settings = target_and_settings.with_static_context(static_context_hash)
    new_target_and_process_settings if new_target_and_process_settings.target.json_doc
  end.compact
end