Class: Omnizip::Models::ConversionResult

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/models/conversion_result.rb

Overview

Model for format conversion results

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_path:, target_path:, source_format:, target_format:, source_size:, target_size:, duration:, entry_count:, warnings: []) ⇒ ConversionResult

Initialize conversion result

Parameters:

  • source_path (String)

    Source file path

  • target_path (String)

    Target file path

  • source_format (Symbol)

    Source format

  • target_format (Symbol)

    Target format

  • source_size (Integer)

    Source file size in bytes

  • target_size (Integer)

    Target file size in bytes

  • duration (Float)

    Conversion duration in seconds

  • entry_count (Integer)

    Number of entries converted

  • warnings (Array<String>) (defaults to: [])

    Conversion warnings



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/omnizip/models/conversion_result.rb', line 21

def initialize(
  source_path:,
  target_path:,
  source_format:,
  target_format:,
  source_size:,
  target_size:,
  duration:,
  entry_count:,
  warnings: []
)
  @source_path = source_path
  @target_path = target_path
  @source_format = source_format
  @target_format = target_format
  @source_size = source_size
  @target_size = target_size
  @duration = duration
  @entry_count = entry_count
  @warnings = warnings
  @compression_ratio = calculate_compression_ratio
end

Instance Attribute Details

#compression_ratioObject (readonly)

Returns the value of attribute compression_ratio.



7
8
9
# File 'lib/omnizip/models/conversion_result.rb', line 7

def compression_ratio
  @compression_ratio
end

#durationObject (readonly)

Returns the value of attribute duration.



7
8
9
# File 'lib/omnizip/models/conversion_result.rb', line 7

def duration
  @duration
end

#entry_countObject (readonly)

Returns the value of attribute entry_count.



7
8
9
# File 'lib/omnizip/models/conversion_result.rb', line 7

def entry_count
  @entry_count
end

#source_formatObject (readonly)

Returns the value of attribute source_format.



7
8
9
# File 'lib/omnizip/models/conversion_result.rb', line 7

def source_format
  @source_format
end

#source_pathObject (readonly)

Returns the value of attribute source_path.



7
8
9
# File 'lib/omnizip/models/conversion_result.rb', line 7

def source_path
  @source_path
end

#source_sizeObject (readonly)

Returns the value of attribute source_size.



7
8
9
# File 'lib/omnizip/models/conversion_result.rb', line 7

def source_size
  @source_size
end

#target_formatObject (readonly)

Returns the value of attribute target_format.



7
8
9
# File 'lib/omnizip/models/conversion_result.rb', line 7

def target_format
  @target_format
end

#target_pathObject (readonly)

Returns the value of attribute target_path.



7
8
9
# File 'lib/omnizip/models/conversion_result.rb', line 7

def target_path
  @target_path
end

#target_sizeObject (readonly)

Returns the value of attribute target_size.



7
8
9
# File 'lib/omnizip/models/conversion_result.rb', line 7

def target_size
  @target_size
end

#warningsObject (readonly)

Returns the value of attribute warnings.



7
8
9
# File 'lib/omnizip/models/conversion_result.rb', line 7

def warnings
  @warnings
end

Instance Method Details

#larger?Boolean

Check if conversion resulted in larger file

Returns:

  • (Boolean)

    True if target is larger



68
69
70
# File 'lib/omnizip/models/conversion_result.rb', line 68

def larger?
  target_size > source_size
end

#processing_speedFloat

Get average processing speed

Returns:

  • (Float)

    MB/s processing speed



80
81
82
83
84
85
86
87
# File 'lib/omnizip/models/conversion_result.rb', line 80

def processing_speed
  return 0.0 if duration.zero? || source_size.zero?

  speed = source_size / duration / 1_048_576.0
  rounded = speed.round(2)
  # Return actual speed if rounding would give 0 but speed is positive
  rounded.zero? && speed.positive? ? speed : rounded
end

#size_ratioFloat

Get size ratio

Returns:

  • (Float)

    Target size as percentage of source size



54
55
56
57
58
# File 'lib/omnizip/models/conversion_result.rb', line 54

def size_ratio
  return 0.0 if source_size.zero?

  (target_size.to_f / source_size * 100).round(2)
end

#size_reductionFloat

Get size reduction percentage

Returns:

  • (Float)

    Size reduction as percentage



46
47
48
49
50
# File 'lib/omnizip/models/conversion_result.rb', line 46

def size_reduction
  return 0.0 if source_size.zero?

  ((source_size - target_size).to_f / source_size * 100).round(2)
end

#smaller?Boolean

Check if conversion resulted in smaller file

Returns:

  • (Boolean)

    True if target is smaller



62
63
64
# File 'lib/omnizip/models/conversion_result.rb', line 62

def smaller?
  target_size < source_size
end

#to_hHash

Convert to hash

Returns:

  • (Hash)

    Result as hash



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/omnizip/models/conversion_result.rb', line 91

def to_h
  {
    source_path: source_path,
    target_path: target_path,
    source_format: source_format,
    target_format: target_format,
    source_size: source_size,
    target_size: target_size,
    size_reduction: size_reduction,
    size_ratio: size_ratio,
    duration: duration,
    entry_count: entry_count,
    processing_speed: processing_speed,
    warnings: warnings,
  }
end

#to_sString

Format as human-readable string

Returns:

  • (String)

    Formatted result



110
111
112
113
114
# File 'lib/omnizip/models/conversion_result.rb', line 110

def to_s
  "Converted #{source_path} (#{format_size(source_size)}) to " \
    "#{target_path} (#{format_size(target_size)}) in #{duration.round(2)}s. " \
    "#{size_reduction.positive? ? "Saved #{size_reduction.to_i}%" : "Increased #{(-size_reduction).to_i}%"}"
end

#warnings?Boolean

Check if there were warnings

Returns:

  • (Boolean)

    True if warnings exist



74
75
76
# File 'lib/omnizip/models/conversion_result.rb', line 74

def warnings?
  !warnings.empty?
end