Sha256: 635514671939b2aa8a19b8d95cbbe97887555e175131462ef64d246d9ec93773

Contents?: true

Size: 911 Bytes

Versions: 3

Compression:

Stored size: 911 Bytes

Contents

module Squib::Args

  module_function def extract_range(opts, deck)
    CardRange.new(opts[:range], deck_size: deck.size)
  end

  class CardRange
    include Enumerable

    def initialize(input, deck_size: 1)
      @range = validate(input, deck_size)
    end

    # Hook into enumerable by delegating to @range
    def each(&block)
      @range.each { |i| block.call(i) }
    end

    def size
      @range.size
    end

    private
    def validate(input, deck_size)
      input ||= :all # default
      input = 0..(deck_size - 1) if input == :all
      input = (input.to_i)..(input.to_i) if input.respond_to? :to_i
      raise ArgumentError.new("#{input} must be Enumerable (i.e. respond_to :each).") unless input.respond_to? :each
      raise ArgumentError.new("#{input} is outside of deck range of 0..#{deck_size - 1}") if (!input.max.nil?) && (input.max > (deck_size - 1))
      input
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
squib-0.19.0 lib/squib/args/card_range.rb
squib-0.19.0b lib/squib/args/card_range.rb
squib-0.19.0a lib/squib/args/card_range.rb