class Anodator::OutputSpec

Constants

TARGET_DATA
TARGET_ERROR
VALID_SYMBOL_ITEMS

Attributes

include_no_error[R]
items[R]
separator[RW]
target[R]
value_separator[RW]

Public Class Methods

new(items = [], options = { }) click to toggle source
# File lib/anodator/output_spec.rb, line 20
def initialize(items = [], options = { })
  @items            = items.to_a
  @target           = TARGET_ERROR
  @include_no_error = false
  @separator        = " "
  @value_separator  = ""

  options.each do |key, opt|
    case key
    when :target
      @target = opt
    when :include_no_error
      @include_no_error = !!opt
    when :separator, :value_separator
      @separator = opt.to_s
    else
      raise ArgumentError.new("unknown option #{key}.")
    end
  end

  unless [TARGET_DATA, TARGET_ERROR].include?(@target)
    raise ArgumentError.new("unknown target option value #{@target}.")
  end

  check_items
end

Public Instance Methods

generate(input_spec_with_values, check_results) click to toggle source
# File lib/anodator/output_spec.rb, line 68
def generate(input_spec_with_values, check_results)
  if @target == TARGET_DATA
    generate_data(input_spec_with_values, check_results)
  else # @target == TARGET_ERROR
    generate_error(input_spec_with_values, check_results)
  end
end
validate_configuration() click to toggle source
# File lib/anodator/output_spec.rb, line 47
def validate_configuration
  @items.each do |item|
    if item.is_a? String
      Validator::Base.values.spec_item_at_by_number(item)
    end
  end
rescue UnknownTargetExpressionError => e
  raise InvalidConfiguration.new(e.to_s)
end

Private Instance Methods

check_items() click to toggle source
# File lib/anodator/output_spec.rb, line 57
def check_items
  @items.each do |item|
    if item.is_a? Symbol
      unless VALID_SYMBOL_ITEMS.include?(item)
        raise ArgumentError.new("unknown item symbol #{item}")
      end
    end
  end
end
generate_data(input_spec_with_values, check_results) click to toggle source
# File lib/anodator/output_spec.rb, line 76
def generate_data(input_spec_with_values, check_results)
  buf = []
  buf << @items.map do |item|
    if item.is_a? Symbol
      case item
      when :error_count
        next check_results.map { |result|
          result.error? ? true : nil
        }.compact.size.to_s
      when :warning_count
        next check_results.map { |result|
          result.warning? ? true : nil
        }.compact.size.to_s
      when :error_and_warning_count
        next check_results.size.to_s
      else
        next ""
      end
    else # data
      next input_spec_with_values[item]
    end
  end

  return buf
end
generate_error(input_spec_with_values, check_results) click to toggle source
# File lib/anodator/output_spec.rb, line 103
def generate_error(input_spec_with_values, check_results)
  buf = []

  if check_results.size.zero?
    if @include_no_error
      buf << @items.map do |item|
        if item.is_a? Symbol
          case item
          when :error_count, :warning_count, :error_and_warning_count
            next "0"
          else
            next ""
          end
        else # data
          next input_spec_with_values[item]
        end
      end
    end
  else
    check_results.each do |check_result|
      buf << @items.map do |item|
        if item.is_a? Symbol
          case item
          when :target_numbers
            next check_result.target_numbers.join(@separator)
          when :target_names
            next check_result.target_numbers.map { |number|
              input_spec_with_values.spec_item_at_by_number(number).name
            }.join(@separator)
          when :target_values
            next check_result.target_numbers.map { |number|
              input_spec_with_values[number]
            }.join(@value_separator)
          when :error_message
            next check_result.message
          when :error_level
            next check_result.level.to_s
          when :error_count
            next check_results.map { |result|
              result.error? ? true : nil
            }.compact.size.to_s
          when :warning_count
            next check_results.map { |result|
              result.warning? ? true : nil
            }.compact.size.to_s
          when :error_and_warning_count
            next check_results.size.to_s
          else
            next ""
          end
        else # data
          next input_spec_with_values[item]
        end
      end
    end
  end

  return buf
end