Sha256: 587f0c25474bdec22af1a4056ee386d05c9a17bf00d70c80fb0f4b85cd1df32c

Contents?: true

Size: 949 Bytes

Versions: 24

Compression:

Stored size: 949 Bytes

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 :spec_string

      def initialize(spec_string)
        @spec_string = spec_string
      end

      def ids
        @ids ||= parse_spec_string(spec_string)
      end

      protected

      def parse_spec_string(spec_string)
        return [] unless spec_string

        rx = /^(\d+)-?(\d+)?:?(\d+)?%?\d*$/
        spec_string.split(',').reduce([]) do |ids, spec|
          if rx =~ spec
            start = ($1 || 1).to_i
            finish = ($2 || start).to_i
            step = ($3 || 1).to_i
            ids.concat (start..finish).step(step).to_a
          end

          ids
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
ood_core-0.21.0 lib/ood_core/job/array_ids.rb
ood_core-0.20.2 lib/ood_core/job/array_ids.rb
ood_core-0.20.1 lib/ood_core/job/array_ids.rb
ood_core-0.20.0 lib/ood_core/job/array_ids.rb
ood_core-0.19.0 lib/ood_core/job/array_ids.rb
ood_core-0.18.1 lib/ood_core/job/array_ids.rb
ood_core-0.18.0 lib/ood_core/job/array_ids.rb
ood_core-0.17.6 lib/ood_core/job/array_ids.rb
ood_core-0.17.5 lib/ood_core/job/array_ids.rb
ood_core-0.17.4 lib/ood_core/job/array_ids.rb
ood_core-0.17.2 lib/ood_core/job/array_ids.rb
ood_core-0.17.1 lib/ood_core/job/array_ids.rb
ood_core-0.17.0 lib/ood_core/job/array_ids.rb
ood_core-0.16.1 lib/ood_core/job/array_ids.rb
ood_core-0.16.0 lib/ood_core/job/array_ids.rb
ood_core-0.15.1 lib/ood_core/job/array_ids.rb
ood_core-0.15.0 lib/ood_core/job/array_ids.rb
ood_core-0.14.0 lib/ood_core/job/array_ids.rb
ood_core-0.13.0 lib/ood_core/job/array_ids.rb
ood_core-0.12.0 lib/ood_core/job/array_ids.rb