Class: Dapp::Downloader::BytesCount

Inherits:
Object
  • Object
show all
Defined in:
lib/dapp/downloader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes, total_bytes_count: nil) ⇒ BytesCount

Returns a new instance of BytesCount.



11
12
13
14
# File 'lib/dapp/downloader.rb', line 11

def initialize(bytes, total_bytes_count: nil)
  @bytes = bytes.to_f
  @total_bytes_count = total_bytes_count
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/dapp/downloader.rb', line 45

def method_missing(method, *args, &blk)
  case method
  when :to_f, :to_i
    @bytes.send(method, *args, &blk)
  else
    raise
  end
end

Instance Attribute Details

#bytesObject (readonly)

Returns the value of attribute bytes.



9
10
11
# File 'lib/dapp/downloader.rb', line 9

def bytes
  @bytes
end

#total_bytes_countObject (readonly)

Returns the value of attribute total_bytes_count.



9
10
11
# File 'lib/dapp/downloader.rb', line 9

def total_bytes_count
  @total_bytes_count
end

Instance Method Details

#to_s(*a) ⇒ Object



16
17
18
19
20
# File 'lib/dapp/downloader.rb', line 16

def to_s(*a)
  max_bytes = @total_bytes_count || self
  width = sprintf("%.2f", max_bytes.bytes/1024/1024).bytesize
  sprintf("%#{width}.2f", @bytes/1024/1024)
end