Module: Datasizes

Defined in:
lib/datasizes/exceptions.rb,
lib/datasizes.rb

Overview

© 2012 Martin Kozák ([email protected])

Defined Under Namespace

Classes: InvalidMagnitude, InvalidSpecification

Constant Summary collapse

MAGNITUDES =

Magnitudes list.

Hash[{
    nil => 0,
    :K => 1,
    :M => 2,
    :G => 3,
    :T => 4,
    :P => 5,
    :E => 6,
    :Z => 7,
    :Y => 8,
}.to_a.reverse]
ANALYSER =

Contains analyser query.

/^(\d+)([KMGTPEZY])?$/u

Class Method Summary collapse

Class Method Details

.analyze_specification(spec) ⇒ Array

Analyzes the given specification.

Parameters:

  • spec (String)

    specification

Returns:

  • (Array)

    amount (Integer) and magnitude identifier (Symbol)



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/datasizes.rb', line 105

def self.analyze_specification(spec)
    matches = spec.match(self::ANALYSER)
    
    if matches.nil?
        raise Datasizes::InvalidSpecification
    else
        size = matches[1].to_i 
        if not matches[2].nil?
            magnitude = matches[2].to_sym
        end
        
        return [size, magnitude]
    end
end

.count_bytes(amount, magnitude = nil) ⇒ Integer

Counts bytes for given amount and magnitude.

Parameters:

  • size (Integer)

    size in bytes

  • magnitude (Symbol, String) (defaults to: nil)

    magnitude name

Returns:

  • (Integer)

    size in bytes



53
54
55
56
57
# File 'lib/datasizes.rb', line 53

def self.count_bytes(amount, magnitude = nil)
    magnitude = magnitude.to_sym if magnitude.kind_of? String
    magnitude = self::MAGNITUDES[magnitude]
    return amount * (1024 ** magnitude)
end

.from_bytes(size) ⇒ String

Converts given size to nearest reasonable magnitude specification.

Parameters:

  • size (Integer)

    size in bytes

Returns:

  • (String)

    specification



86
87
88
89
90
91
92
93
94
95
# File 'lib/datasizes.rb', line 86

def self.from_bytes(size)
    self::MAGNITUDES.each_key do |magnitude|
        amount = self.count_bytes(1, magnitude)
        remain = size % amount
        
        if remain == 0
            return (size / amount).to_s + magnitude.to_s
        end
    end
end

.to_bytes(spec) ⇒ Integer

Converts specification to bytes.

Parameters:

  • spec (String)

Returns:

  • (Integer)

    size in bytes



40
41
42
43
# File 'lib/datasizes.rb', line 40

def self.to_bytes(spec)
    amount, magnitude = self::analyze_specification(spec)
    return self.count_bytes(amount, magnitude)
end

.to_magnitude(size, magnitude = nil) ⇒ String

Converts given size in bytes to given magnitude.

Parameters:

  • size (Integer)

    size in bytes

  • magnitude (Symbol, String) (defaults to: nil)

    magnitude name

Returns:

  • (String)

    specification



67
68
69
70
71
72
73
74
75
76
# File 'lib/datasizes.rb', line 67

def self.to_magnitude(size, magnitude = nil)
    magnitude = magnitude.to_sym if magnitude.kind_of? String
    factor = self::MAGNITUDES[magnitude]
    
    if factor.nil?
        raise Datasizes::InvalidMagnitude::new("Invalid magnitude '#{magnitude}'.")
    end
    
    (size / (1024 ** factor)).to_s + magnitude.to_s
end