Class: Filesize

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/filters/bytes2human/filesize.rb

Overview

This module was based on and modified from: github.com/dominikh/filesize/blob/master/lib/filesize.rb Copyright © 2015 Robin Clarke

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size, type = BINARY) ⇒ Filesize

Returns a new instance of Filesize.

Parameters:

  • size (Number)

    A file size, in bytes.

  • type (SI, BINARY) (defaults to: BINARY)

    Which type to use for conversions.



51
52
53
54
# File 'lib/logstash/filters/bytes2human/filesize.rb', line 51

def initialize(size, type = BINARY)
  @bytes = size.to_i
  @type  = type
end

Class Method Details

.from(arg) ⇒ Filesize

Parses a string, which describes a file size, and returns a Filesize object.

Parameters:

  • arg (String)

    A file size to parse.

Returns:

Raises:

  • (ArgumentError)

    Raised if the file size cannot be parsed properly.



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/logstash/filters/bytes2human/filesize.rb', line 150

def from(arg)
  parts  = parse(arg)
  prefix = parts[:prefix]
  size   = parts[:size]
  type   = parts[:type]

  raise ArgumentError, "Unparseable filesize" unless type

  offset = (type[:prefixes].map { |s| s[0].downcase }.index(prefix.downcase) || -1) + 1

  new(size * (type[:multiplier] ** (offset)), type)
end

.parse(string) ⇒ Hash<:prefix, :size, :type>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Hash<:prefix, :size, :type>)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/logstash/filters/bytes2human/filesize.rb', line 165

def parse(string)
  type = nil
  # in this order, so we prefer si :)
  [SI, BINARY].each { |_type|
    if string =~ _type[:regexp]
      type    =  _type
      break
    end
  }

  prefix = $2 || ''
  size   = ($1 || 0).to_f

  return { :prefix => prefix, :size => size, :type => type}
end

Instance Method Details

#*(other) ⇒ Filesize

Returns:



118
119
120
# File 'lib/logstash/filters/bytes2human/filesize.rb', line 118

def *(other)
  self.class.new(@bytes * other.to_i, @type)
end

#+(other) ⇒ Filesize

Returns:



108
109
110
# File 'lib/logstash/filters/bytes2human/filesize.rb', line 108

def +(other)
  self.class.new(@bytes + other.to_i, @type)
end

#-(other) ⇒ Filesize

Returns:



113
114
115
# File 'lib/logstash/filters/bytes2human/filesize.rb', line 113

def -(other)
  self.class.new(@bytes - other.to_i, @type)
end

#/(other) ⇒ Filesize

Returns:



123
124
125
126
127
128
129
130
# File 'lib/logstash/filters/bytes2human/filesize.rb', line 123

def /(other)
  result = @bytes / other.to_f
  if other.is_a? Filesize
    result
  else
    self.class.new(result, @type)
  end
end

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/logstash/filters/bytes2human/filesize.rb', line 133

def ==(other)
  other.is_a?(self.class) && other.to_i == self.to_i
end

#coerce(other) ⇒ Array<self, other>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Array<self, other>)


139
140
141
# File 'lib/logstash/filters/bytes2human/filesize.rb', line 139

def coerce(other)
  return self, other
end

#prettyString

Same as #to_s but with an automatic determination of the most sensible unit.

Returns:

  • (String)

See Also:



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/logstash/filters/bytes2human/filesize.rb', line 93

def pretty
  size = @bytes
  if size < @type[:multiplier]
    unit = "B"
  else
    pos = (Math.log(size) / Math.log(@type[:multiplier])).floor
    pos = @type[:prefixes].size-1 if pos > @type[:prefixes].size - 1

    unit = @type[:prefixes][pos-1] + "B"
  end

  to_s(unit)
end

#to(unit = 'B') ⇒ Float Also known as: to_f

Returns the size in a given unit.

Parameters:

  • unit (String) (defaults to: 'B')

    Which unit to convert to.

Returns:

  • (Float)

    Returns the size in a given unit.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/logstash/filters/bytes2human/filesize.rb', line 64

def to(unit = 'B')
  to_parts = self.class.parse(unit)
  prefix   = to_parts[:prefix]

  if prefix == 'B' or prefix.empty?
    return to_i.to_f
  end

  to_type = to_parts[:type]
  size    = @bytes

  pos = (@type[:prefixes].map { |s| s[0].downcase }.index(prefix.downcase) || -1) + 1

  size = size/(to_type[:multiplier].to_f**(pos)) unless pos < 1
end

#to_iNumber Also known as: to_int

Returns the size in bytes.

Returns:

  • (Number)

    Returns the size in bytes.



57
58
59
# File 'lib/logstash/filters/bytes2human/filesize.rb', line 57

def to_i
  @bytes
end

#to_s(unit = 'B') ⇒ String

Returns Same as #to_f, but as a string, with the unit appended.

Parameters:

  • unit (String) (defaults to: 'B')

    Which unit to convert to.

Returns:

  • (String)

    Same as #to_f, but as a string, with the unit appended.

See Also:



84
85
86
# File 'lib/logstash/filters/bytes2human/filesize.rb', line 84

def to_s(unit = 'B')
  "%.2f %s" % [to(unit).to_f.to_s, unit]
end