class Radiosonde::DSL::Converter

Public Class Methods

convert(exported, opts = {}) click to toggle source
# File lib/radiosonde/dsl/converter.rb, line 5
def convert(exported, opts = {})
  self.new(exported, opts).convert
end
new(exported, options = {}) click to toggle source
# File lib/radiosonde/dsl/converter.rb, line 10
def initialize(exported, options = {})
  @exported = exported
  @options = options
end

Public Instance Methods

convert() click to toggle source
# File lib/radiosonde/dsl/converter.rb, line 15
def convert
  @exported.each.map {|alarm_name, alarm_attrs|
    output_alarm(alarm_name, alarm_attrs)
  }.join("\n")
end

Private Instance Methods

duplicated?(list) click to toggle source
# File lib/radiosonde/dsl/converter.rb, line 99
def duplicated?(list)
  list.length != list.uniq.length
end
format_dimensions(attrs) click to toggle source
# File lib/radiosonde/dsl/converter.rb, line 64
def format_dimensions(attrs)
  dimensions = attrs[:dimensions] || []
  return nil if dimensions.empty?
  names = dimensions.map {|i| i[:name] }

  if duplicated?(names)
    dimensions.inspect
  else
    dimension_hash = {}

    dimensions.each do |dimension|
      name = dimension[:name]
      value = dimension[:value]
      dimension_hash[name] = value
    end

    unbrace(dimension_hash.inspect)
  end
end
format_threshold(attrs) click to toggle source
# File lib/radiosonde/dsl/converter.rb, line 84
def format_threshold(attrs)
  threshold = attrs[:threshold]
  operator = attrs[:comparison_operator]
  operator = Radiosonde::DSL::ComparisonOperator.conv_to_alias(operator)

  [
    operator.inspect,
    threshold.inspect,
  ].join(', ')
end
output_alarm(name, attrs) click to toggle source
# File lib/radiosonde/dsl/converter.rb, line 23
  def output_alarm(name, attrs)
    name = name.inspect
    description = attrs[:description]
    description = "description #{description.inspect}\n  " if description
    namespace = attrs[:namespace].inspect
    metric_name = attrs[:metric_name].inspect
    dimensions = format_dimensions(attrs)
    dimensions = "dimensions #{dimensions}\n  " if dimensions
    period = attrs[:period].inspect
    statistic = Radiosonde::DSL::Statistic.conv_to_alias(attrs[:statistic]).inspect
    threshold = format_threshold(attrs)
    evaluation_periods = attrs[:evaluation_periods].inspect
    actions_enabled = attrs[:actions_enabled].inspect
    alarm_actions = attrs[:alarm_actions].inspect
    ok_actions = attrs[:ok_actions].inspect
    insufficient_data_actions = attrs[:insufficient_data_actions].inspect

    if unit = attrs[:unit]
      unit = Radiosonde::DSL::Unit.conv_to_alias(unit).inspect
      unit = "unit #{unit}\n"
    end

    <<-EOS
alarm #{name} do
  #{description
  }namespace #{namespace}
  metric_name #{metric_name}
  #{dimensions
  }period #{period}
  statistic #{statistic}
  threshold #{threshold}
  evaluation_periods #{evaluation_periods}
  #{unit
  }actions_enabled #{actions_enabled}
  alarm_actions #{alarm_actions}
  ok_actions #{ok_actions}
  insufficient_data_actions #{insufficient_data_actions}
end
    EOS
  end
unbrace(str) click to toggle source
# File lib/radiosonde/dsl/converter.rb, line 95
def unbrace(str)
  str.sub(/\A\s*\{/, '').sub(/\}\s*\z/, '')
end