Class: OctocatalogDiff::API::V1::Override

Inherits:
Object
  • Object
show all
Defined in:
lib/octocatalog-diff/api/v1/override.rb

Overview

Sets up the override of a fact or ENC parameter during catalog compilation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Override

Constructor: Accepts a key and value.

Parameters:

  • input (Hash)

    Must contain :key and :value


15
16
17
18
19
# File 'lib/octocatalog-diff/api/v1/override.rb', line 15

def initialize(input)
  key = input.fetch(:key)
  @key = key =~ %r{\A/(.+)/\Z} ? Regexp.new(Regexp.last_match(1)) : key
  @value = parsed_value(input.fetch(:value))
end

Instance Attribute Details

#keyObject (readonly)

Accessors


11
12
13
# File 'lib/octocatalog-diff/api/v1/override.rb', line 11

def key
  @key
end

#valueObject (readonly)

Accessors


11
12
13
# File 'lib/octocatalog-diff/api/v1/override.rb', line 11

def value
  @value
end

Class Method Details

.create_from_input(input, key = nil) ⇒ OctocatalogDiff::API::V1::Override

Initialize from a parsed command line

Parameters:

  • input (String)

    Command line parameter

Returns:


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/octocatalog-diff/api/v1/override.rb', line 24

def self.create_from_input(input, key = nil)
  # Normally the input will be a string in the format key=(data type)value where the data
  # type is optional and the parentheses are literal. Example:
  #   foo=1            (auto-determine data type - in this case it would be a fixnum)
  #   foo=(fixnum)1    (will be a fixnum)
  #   foo=(string)1    (will be '1' the string)
  # If input is not a string, we can still construct the object if the key is given.
  # That input would come directly from code and not from the command line, since inputs
  # from the command line are always strings.
  # Also support regular expressions for the key name, if delimited by //.
  if key.nil? && input.is_a?(String)
    unless input.include?('=')
      raise ArgumentError, "Fact override '#{input}' is not in 'key=(data type)value' format"
    end
    k, v = input.strip.split('=', 2).map(&:strip)
    new(key: k, value: v)
  elsif key.nil?
    message = "Define a key when the input is not a string (#{input.class} => #{input.inspect})"
    raise ArgumentError, message
  else
    new(key: key, value: input)
  end
end