Sha256: 88594d24b07b48415c3c6e84eb2f2d14d7093abec4a3719ec0bb93653f890a06

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

module Vagrant
  module Disksize
    class Config < Vagrant.plugin('2', :config)
      attr_accessor :size

      SIZE_REGEX = /^(?<number>[0-9]+)\s?(?<scale>KB|MB|GB|TB)?$/

      def initialize
        @size = UNSET_VALUE
      end

      def finalize!
        if @size == UNSET_VALUE
          @size = nil
        else
          # Convert from human to machine readable
          size_str = @size.to_s.strip
          matches = SIZE_REGEX.match(size_str)
          if matches
            number = matches[:number]
            scale = matches[:scale]
            @size = number.to_i
            if scale
              pos = %w(KB MB GB TB).index(scale)
              mult = 1 << 10*(pos+1)
              @size *= mult
            end
          end
          # Convert size from bytes to MB
          size_in_mb = (@size.to_i + (1<<20)-1) / (1<<20)
          @size = size_in_mb
        end
      end

      def validate(machine)
        errors = _detected_errors

        unless @size.nil? || @size.is_a?(Integer)
          errors << "'#{@size}' is not a valid specification of disk size"
        end

        return { 'Disksize configuration' => errors }
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vagrant-disksize-0.1.3 lib/vagrant/disksize/config.rb
vagrant-disksize-0.1.2 lib/vagrant/disksize/config.rb