Sha256: 77094cc4c21c5c12ce9ae9cf4f4f74cf52ac809d2a89df79d9858918dbe17de5

Contents?: true

Size: 811 Bytes

Versions: 1

Compression:

Stored size: 811 Bytes

Contents

module Interactive
  class OptionsShortcuts
    def initialize(minifiable_string)
      @minifiable_string = minifiable_string
    end

    def minify
      if has_only_one_number_or_none?
        @minifiable_string
      else
        non_numerical_options.inject("#{numerical_options.min}..#{numerical_options.max}") do |accum, item|
          "#{accum}/#{item}"
        end
      end
    end

    private

    def has_only_one_number_or_none?
      numerical_options.min == numerical_options.max
    end

    def numerical_options
      options.select {|option| numeric?(option)}
    end

    def non_numerical_options
      options.reject {|option| numeric?(option)}
    end

    def options
      @minifiable_string.split("/")
    end

    def numeric?(option)
      option.match(/^\d+$/)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
interactive-0.6.2 lib/interactive/options_shortcuts.rb