Module: Omnizip::Profile

Defined in:
lib/omnizip/profile.rb,
lib/omnizip/profile/fast_profile.rb,
lib/omnizip/profile/text_profile.rb,
lib/omnizip/profile/binary_profile.rb,
lib/omnizip/profile/custom_profile.rb,
lib/omnizip/profile/archive_profile.rb,
lib/omnizip/profile/maximum_profile.rb,
lib/omnizip/profile/balanced_profile.rb,
lib/omnizip/profile/profile_detector.rb,
lib/omnizip/profile/profile_registry.rb,
lib/omnizip/profile/compression_profile.rb

Overview

Compression profile management

This module provides a high-level API for working with compression profiles. Profiles encapsulate compression settings and allow users to easily select optimal compression strategies for different file types.

Defined Under Namespace

Classes: ArchiveProfile, BalancedProfile, BinaryProfile, CompressionProfile, CustomProfile, FastProfile, MaximumProfile, ProfileDetector, ProfileRegistry, TextProfile

Class Method Summary collapse

Class Method Details

.define(name, base: nil) {|builder| ... } ⇒ CustomProfile

Define a custom profile

Examples:

Define a custom profile

Omnizip::Profile.define(:my_profile) do |p|
  p.algorithm = :lzma2
  p.level = 7
  p.filter = :bcj_x86
  p.solid = true
  p.description = "My custom profile"
end

Extend an existing profile

Omnizip::Profile.define(:my_fast, base: :fast) do |p|
  p.level = 2
  p.description = "Slightly better than fast"
end

Parameters:

  • name (Symbol)

    Profile name

  • base (Symbol, nil) (defaults to: nil)

    Base profile to extend

Yields:

  • (builder)

    Yields a builder for profile configuration

Yield Parameters:

Returns:



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/omnizip/profile.rb', line 62

def define(name, base: nil)
  base_profile = base ? registry.get(base) : nil
  builder = CustomProfile::Builder.new(name, base_profile)

  yield builder if block_given?

  builder.valid?
  profile = builder.build

  registry.register!(profile)
  profile
end

.detect(file_path, options = {}) ⇒ CompressionProfile

Auto-detect optimal profile for a file

Parameters:

  • file_path (String)

    Path to the file

  • options (Hash) (defaults to: {})

    Detection options

Returns:



107
108
109
# File 'lib/omnizip/profile.rb', line 107

def detect(file_path, options = {})
  detector.detect(file_path, options)
end

.detectorProfileDetector

Get the profile detector

Returns:



114
115
116
# File 'lib/omnizip/profile.rb', line 114

def detector
  @detector ||= ProfileDetector.new(registry)
end

.for_file_type(file_type) ⇒ CompressionProfile?

Get recommended profile for a file type

Parameters:

  • file_type (String, Symbol)

    MIME type string or category symbol

Returns:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/omnizip/profile.rb', line 86

def for_file_type(file_type)
  # If file_type is a symbol (category), find first suitable profile
  return find_profile_for_category(file_type) if file_type.is_a?(Symbol)

  # If file_type is a MIME string, find suitable profiles
  if file_type.is_a?(String)
    suitable = registry.suitable_for(file_type)
    return registry.get(:balanced) if suitable.empty?

    return select_best_profile_for_mime(suitable, file_type)
  end

  # Default to balanced
  registry.get(:balanced)
end

.get(name) ⇒ CompressionProfile?

Get a profile by name

Parameters:

  • name (Symbol)

    Profile name

Returns:



36
37
38
# File 'lib/omnizip/profile.rb', line 36

def get(name)
  registry.get(name)
end

.listArray<Symbol>

List all available profile names

Returns:

  • (Array<Symbol>)

    List of profile names



78
79
80
# File 'lib/omnizip/profile.rb', line 78

def list
  registry.names
end

.registryProfileRegistry

Get the global profile registry

Returns:



26
27
28
29
30
# File 'lib/omnizip/profile.rb', line 26

def registry
  @registry ||= ProfileRegistry.new.tap do |reg|
    register_built_in_profiles(reg)
  end
end

.reset!void

This method returns an undefined value.

Reset the global registry (mainly for testing)



121
122
123
124
# File 'lib/omnizip/profile.rb', line 121

def reset!
  @registry = nil
  @detector = nil
end