Class: Omnizip::Profile::ProfileRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/profile/profile_registry.rb

Overview

Thread-safe registry for compression profiles

This class manages both built-in and custom compression profiles, providing a central location for profile registration, lookup, and management.

Instance Method Summary collapse

Constructor Details

#initializeProfileRegistry

Initialize a new profile registry



12
13
14
15
# File 'lib/omnizip/profile/profile_registry.rb', line 12

def initialize
  @profiles = {}
  @mutex = Mutex.new
end

Instance Method Details

#allArray<CompressionProfile>

Get all registered profiles

Returns:



99
100
101
102
103
# File 'lib/omnizip/profile/profile_registry.rb', line 99

def all
  @mutex.synchronize do
    @profiles.values
  end
end

#clearvoid

This method returns an undefined value.

Clear all registered profiles



116
117
118
119
120
# File 'lib/omnizip/profile/profile_registry.rb', line 116

def clear
  @mutex.synchronize do
    @profiles.clear
  end
end

#countInteger

Get the number of registered profiles

Returns:

  • (Integer)

    Profile count



125
126
127
128
129
# File 'lib/omnizip/profile/profile_registry.rb', line 125

def count
  @mutex.synchronize do
    @profiles.size
  end
end

#each {|profile| ... } ⇒ void

This method returns an undefined value.

Iterate over all profiles

rubocop:disable Naming/BlockForwarding, Style/ArgumentsForwarding -- Ruby 3.0 compatibility

Yields:

  • (profile)

    Yields each profile

Yield Parameters:



137
138
139
# File 'lib/omnizip/profile/profile_registry.rb', line 137

def each(&block)
  all.each(&block)
end

#get(name) ⇒ CompressionProfile?

Get a profile by name

Parameters:

  • name (Symbol)

    Profile name

Returns:



71
72
73
74
75
# File 'lib/omnizip/profile/profile_registry.rb', line 71

def get(name)
  @mutex.synchronize do
    @profiles[name]
  end
end

#inspectString

String representation

Returns:

  • (String)


154
155
156
157
158
# File 'lib/omnizip/profile/profile_registry.rb', line 154

def inspect
  @mutex.synchronize do
    "#<#{self.class.name} profiles=#{@profiles.keys.join(', ')}>"
  end
end

#namesArray<Symbol>

Get all registered profile names

Returns:

  • (Array<Symbol>)

    List of profile names



90
91
92
93
94
# File 'lib/omnizip/profile/profile_registry.rb', line 90

def names
  @mutex.synchronize do
    @profiles.keys
  end
end

#register(profile) ⇒ CompressionProfile

Register a profile

Parameters:

Returns:

Raises:

  • (ArgumentError)

    if profile with same name already exists



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/omnizip/profile/profile_registry.rb', line 22

def register(profile)
  unless profile.is_a?(CompressionProfile)
    raise ArgumentError,
          "Profile must be a CompressionProfile instance"
  end

  @mutex.synchronize do
    if @profiles.key?(profile.name)
      raise ArgumentError,
            "Profile '#{profile.name}' is already registered"
    end

    @profiles[profile.name] = profile
  end

  profile
end

#register!(profile) ⇒ CompressionProfile

Register a profile, replacing if it exists

Parameters:

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/omnizip/profile/profile_registry.rb', line 44

def register!(profile)
  unless profile.is_a?(CompressionProfile)
    raise ArgumentError,
          "Profile must be a CompressionProfile instance"
  end

  @mutex.synchronize do
    @profiles[profile.name] = profile
  end

  profile
end

#registered?(name) ⇒ Boolean

Check if a profile is registered

Parameters:

  • name (Symbol)

    Profile name

Returns:

  • (Boolean)

    true if profile exists



81
82
83
84
85
# File 'lib/omnizip/profile/profile_registry.rb', line 81

def registered?(name)
  @mutex.synchronize do
    @profiles.key?(name)
  end
end

#suitable_for(mime_type) ⇒ Array<CompressionProfile>

Find profiles suitable for a MIME type

Parameters:

  • mime_type (String)

    MIME type string

Returns:



109
110
111
# File 'lib/omnizip/profile/profile_registry.rb', line 109

def suitable_for(mime_type)
  all.select { |profile| profile.suitable_for?(mime_type) }
end

#to_hHash{Symbol => Hash}

Get profile information as hash

Returns:

  • (Hash{Symbol => Hash})

    Profiles indexed by name



145
146
147
148
149
# File 'lib/omnizip/profile/profile_registry.rb', line 145

def to_h
  @mutex.synchronize do
    @profiles.transform_values(&:to_h)
  end
end

#unregister(name) ⇒ CompressionProfile?

Unregister a profile by name

Parameters:

  • name (Symbol)

    Profile name

Returns:



61
62
63
64
65
# File 'lib/omnizip/profile/profile_registry.rb', line 61

def unregister(name)
  @mutex.synchronize do
    @profiles.delete(name)
  end
end