Class: ProcessSettings::TargetedSettings

Inherits:
Object
  • Object
show all
Defined in:
lib/process_settings/targeted_settings.rb

Overview

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

Constant Summary collapse

KEY_NAMES =
["filename", "target", "settings"].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(targeted_settings_array, version:) ⇒ TargetedSettings

Returns a new instance of TargetedSettings.



13
14
15
16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#targeted_settings_arrayObject (readonly)

Returns the value of attribute targeted_settings_array.



11
12
13
# File 'lib/process_settings/targeted_settings.rb', line 11

def targeted_settings_array
  @targeted_settings_array
end

#versionObject (readonly)

Returns the value of attribute version.



11
12
13
# File 'lib/process_settings/targeted_settings.rb', line 11

def version
  @version
end

Class Method Details

.from_array(settings_array, only_meta: false) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object



75
76
77
78
# File 'lib/process_settings/targeted_settings.rb', line 75

def from_file(file_path, only_meta: false)
  json_doc = Psych.safe_load_file(file_path)
  from_array(json_doc, only_meta: only_meta)
end

Instance Method Details

#==(rhs) ⇒ Object



25
26
27
# File 'lib/process_settings/targeted_settings.rb', line 25

def ==(rhs)
  to_json_doc == rhs.to_json_doc
end

#eql?(rhs) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/process_settings/targeted_settings.rb', line 29

def eql?(rhs)
  self == rhs
end

#matching_settings(context_hash) ⇒ Object



81
82
83
84
85
# 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) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# 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_docObject



33
34
35
# File 'lib/process_settings/targeted_settings.rb', line 33

def to_json_doc
  @targeted_settings_array.map(&:to_json_doc)
end

#to_yamlObject



37
38
39
# File 'lib/process_settings/targeted_settings.rb', line 37

def to_yaml
  to_json_doc.to_yaml
end

#with_static_context(static_context_hash) ⇒ Object

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



89
90
91
92
93
94
# 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