Module: Ncio::Support::Transform

Included in:
App
Defined in:
lib/ncio/support/transform.rb

Overview

Helper methods for transforming a backup data structure. The transformation methods match groups against class names. If there's a match, the rules and the matching class paramter values are transformed, mapping one hostname to another.

Instance Method Summary collapse

Instance Method Details

#class_matches?(class_name) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/ncio/support/transform.rb', line 17

def class_matches?(class_name)
  opts[:matcher].match(class_name)
end

#group_matches?(group) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
# File 'lib/ncio/support/transform.rb', line 9

def group_matches?(group)
  classes = group['classes'].keys
  name = classes.find { |class_name| class_matches?(class_name) }
  msg = name ? 'Matched' : 'Did not match'
  debug(msg + " group: #{group['name']}, classes: #{JSON.dump(classes)}")
  name ? true : false
end

#transform_group(group) ⇒ Object

Parameters:

  • group (Hash)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ncio/support/transform.rb', line 23

def transform_group(group)
  # Transform rules if they're present in the 'rule' key
  group['rule'] = transform_rules(group['rule']) if group['rule']
  # Transform class parameters if there are classes with parameters
  classes = group['classes']
  group['classes'] = classes.each_with_object({}) do |(name, params), hsh|
    hsh[name] = if class_matches?(name) then transform_params(params)
                else params
                end
    hsh
  end
  # Return the updated group
  group
end

#transform_params(params) ⇒ Hash<String, String>

Transform class parameters, which are a simple key / value JSON hash. The values of each hash are routed through the hostname "smart map"

Returns:

  • (Hash<String, String>)

    transformed parameter hash map



61
62
63
64
65
66
67
68
69
70
# File 'lib/ncio/support/transform.rb', line 61

def transform_params(params)
  params.each_with_object({}) do |(key, val), hsh|
    hsh[key] = case val
               when Array then val.map { |v| opts[:hostname_map][v] }
               when String then opts[:hostname_map][val]
               else val
               end
    hsh
  end
end

#transform_rules(rules) ⇒ Array

Recursively transform an array of rules

Parameters:

  • rules (Array)

    Array of rule objects. See the Rule Grammar

Returns:

  • (Array)

    transformed rules



45
46
47
48
49
50
51
52
53
54
# File 'lib/ncio/support/transform.rb', line 45

def transform_rules(rules)
  rules.map do |rule|
    case rule
    when Array
      transform_rules(rule)
    when String
      opts[:hostname_map][rule]
    end
  end
end