class Hookit::Converginator

Public Class Methods

new(map, list) click to toggle source
# File lib/hookit/converginator.rb, line 4
def initialize(map, list)
  @map = map
  @list = list
end

Public Instance Methods

converge!() click to toggle source
# File lib/hookit/converginator.rb, line 9
def converge!
  output = {}
  @map.each do |key, template|
    if @list.key? key
      output[key] = converge_value template, @list[key]
    else
      output[key] = template[:default]
    end
  end
  output
end
converge_hash(template, value) click to toggle source
# File lib/hookit/converginator.rb, line 21
def converge_hash(template, value)
  temp = {}
  template.each do |k, t|
    if value.key? k
      temp[k] = converge_value t, value[k]
    else
      temp[k] = t[:default]
    end
  end
  temp
end
converge_value(template, value) click to toggle source
# File lib/hookit/converginator.rb, line 33
def converge_value(template, value)
  if template[:type] == :array
    value = sanitize_array(template, value)
  end

  if template[:type] == :array and template[:of] == :hash and template[:template]
    value.map! {|v| converge_hash(template[:template], v)}
  end

  if template[:type] == :hash and template[:template]
    value = converge_hash(template[:template], value)
  end

  if valid? template, value
    value
  else
    template[:default]
  end
end
sanitize_array(template, value) click to toggle source
# File lib/hookit/converginator.rb, line 53
def sanitize_array(template, value)

  case template[:of]
  when :byte
    value = [value] if ( valid_byte? value )
  when :file
    value = [value] if ( valid_file? value )
  when :folder
    value = [value] if ( valid_folder? value )
  when :hash
    value = [value] if ( valid_hash? value )
  when :integer
    value = [value] if ( valid_integer? value )
  when :on_off
    value = [value] if ( valid_on_off? value )
  when :string
    value = [value] if ( valid_string? value )
  end
  value
end
valid?(template, value) click to toggle source
# File lib/hookit/converginator.rb, line 74
def valid?(template, value)
  valid_type?(template, value) and valid_value?(template, value)
end
valid_array?(template, value) click to toggle source
# File lib/hookit/converginator.rb, line 114
def valid_array?(template, value)

  return false if not value.is_a? Array

  return true if not template.key? :of

  case template[:of]
  when :byte
    !( value.map {|element| valid_byte? element} ).include? false
  when :file
    !( value.map {|element| valid_file? element} ).include? false
  when :folder
    !( value.map {|element| valid_folder? element} ).include? false
  when :integer
    !( value.map {|element| valid_integer? element} ).include? false
  when :hash
    !( value.map {|element| valid_hash? element} ).include? false
  when :on_off
    !( value.map {|element| valid_on_off? element} ).include? false
  when :string
    !( value.map {|element| valid_string? element} ).include? false
  else
    true
  end
end
valid_byte?(value) click to toggle source
# File lib/hookit/converginator.rb, line 160
def valid_byte?(value)
  value.to_s =~ /^\d+[BbKkMmGgTt]?$/
end
valid_file?(value) click to toggle source
# File lib/hookit/converginator.rb, line 148
def valid_file?(value)
  value =~ /^\/?[^\/]+(\/[^\/]+)*$/
end
valid_folder?(value) click to toggle source
# File lib/hookit/converginator.rb, line 152
def valid_folder?(value)
  value =~ /^\/?[^\/]+(\/[^\/]+)*\/?$/
end
valid_hash?(value) click to toggle source
# File lib/hookit/converginator.rb, line 140
def valid_hash?(value)
  value.is_a? Hash
end
valid_integer?(value) click to toggle source
# File lib/hookit/converginator.rb, line 144
def valid_integer?(value)
  value.is_a? Integer || (value.to_i.to_s == value.to_s)
end
valid_on_off?(value) click to toggle source
# File lib/hookit/converginator.rb, line 156
def valid_on_off?(value)
  ['true', 'false', 'On', 'on', 'Off', 'off', '1', '0'].include? value.to_s
end
valid_string?(element) click to toggle source
# File lib/hookit/converginator.rb, line 110
def valid_string?(element)
  element.is_a? String
end
valid_type?(template, value) click to toggle source
# File lib/hookit/converginator.rb, line 78
def valid_type?(template, value)
  case template[:type]
  when :array
    valid_array? template, value
  when :byte
    valid_byte? value
  when :file
    valid_file? value
  when :folder
    valid_folder? value
  when :hash
    valid_hash? value
  when :integer
    valid_integer? value
  when :on_off
    valid_on_off? value
  when :string
    valid_string? value
  end
end
valid_value?(template, value) click to toggle source
# File lib/hookit/converginator.rb, line 99
def valid_value?(template, value)

  return true if not template.key? :from

  if template[:type] == :array
    !( value.map {|element| template[:from].include? element} ).include? false
  else
    template[:from].include? value
  end
end