Sha256: 0d4ad9f86478e5914346ea8a5f4faf9a650ad19a7654a2d98d2c034752cb3f98

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

# Builds a sorted array of job ids given a job array spec string
# 
# Job array spec strings:
#   1         Single id
#   1-10      Range
#   1-10:2    Range with step
#   1-10,13   Compound (range with single id)
#   
# Note that Ranges are expected to be inclusive
module OodCore
  module Job
    class ArrayIds
      attr_reader :ids
      def initialize(spec_string)
        @ids = []
        parse_spec_string(spec_string)
      end

      protected
      def parse_spec_string(spec_string)
        @ids = get_components(spec_string).map{
          |component| process_component(component)
        }.reduce(:+).sort
      end

      def get_components(spec_string)
        discard_percent_modifier(spec_string).split(',')
      end

      # A few adapters use percent to define an arrays maximum number of
      # simultaneous tasks. The percent is expected to come at the end.
      def discard_percent_modifier(spec_string)
        spec_string.split('%').first
      end

      def process_component(component)
        is_range?(component) ? get_range(component) : [ component.to_i ]
      end

      def get_range(component)
        raw_range, raw_step = component.split(':')
        start, stop = raw_range.split('-').map(&:to_i)
        range = Range.new(start, stop)
        step = raw_step.to_i
        step = 1 if step == 0
        
        range.step(step).to_a
      end

      def is_range?(component)
        component.include?('-')
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ood_core-0.9.0 lib/ood_core/job/array_ids.rb
ood_core-0.8.0 lib/ood_core/job/array_ids.rb