Module: SvgConform::Profiles

Defined in:
lib/svg_conform/profiles.rb

Constant Summary collapse

@@cache =
{}

Class Method Summary collapse

Class Method Details

.available_profilesObject



20
21
22
23
24
25
# File 'lib/svg_conform/profiles.rb', line 20

def self.available_profiles
  return @@cache.keys.map(&:to_sym) if @@cache.any?

  profile_files = Dir.glob(File.join(Profile::PROFILES_DIR, "*.yml"))
  profile_files.map { |file| File.basename(file, ".yml").to_sym }
end

.clear_cache!Object



35
36
37
# File 'lib/svg_conform/profiles.rb', line 35

def self.clear_cache!
  @@cache.clear
end

.get(profile_id) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/svg_conform/profiles.rb', line 9

def self.get(profile_id)
  case profile_id
  when Symbol, String
    load_profile(profile_id.to_s)
  when Profile
    profile_id
  else
    raise ProfileError, "Invalid profile: #{profile_id}"
  end
end

.listObject



31
32
33
# File 'lib/svg_conform/profiles.rb', line 31

def self.list
  available_profiles.map(&:to_s)
end

.load(profile_name) ⇒ Object



27
28
29
# File 'lib/svg_conform/profiles.rb', line 27

def self.load(profile_name)
  load_profile(profile_name.to_s)
end

.load_profile(profile_name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/svg_conform/profiles.rb', line 39

def self.load_profile(profile_name)
  return @@cache[profile_name] if @@cache[profile_name]

  profile_file = File.join(Profile::PROFILES_DIR, "#{profile_name}.yml")

  unless File.exist?(profile_file)
    raise ProfileError,
          "Profile not found: #{profile_name} (expected: #{profile_file})"
  end

  begin
    profile = Profile.load_from_file(profile_file)
    @@cache[profile_name] = profile
    profile
    # rescue => e
    #   raise e
    #   raise ProfileError, "Failed to load profile #{profile_name}: #{e.message}"
  end
end