Sha256: b7eae3cef9766e2cb18bd717005af21b1a72518bc1a86bbdb612d84e0657b435

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 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!
        # 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

      def validate(machine)
        errors = []

        unless @size.to_s =~ SIZE_REGEX
          errors << "'#{@size}' is not a valid specification of disk size"
        end

        return { 'Disksize configuration' => errors }
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vagrant-disksize-0.1.0 lib/vagrant/disksize/config.rb