Module: Dubs::Data

Defined in:
lib/dubs/data.rb

Constant Summary collapse

MUTEX =
Mutex.new

Class Method Summary collapse

Class Method Details

.adjective_groupsObject



15
16
17
# File 'lib/dubs/data.rb', line 15

def adjective_groups
  adjective_data.keys.map(&:to_sym)
end

.adjectives(group = :general) ⇒ Object



10
11
12
13
# File 'lib/dubs/data.rb', line 10

def adjectives(group = :general)
  key = group.to_s
  adjective_data.fetch(key) { raise Error, "Unknown adjective group: #{group}" }
end

.pattern(name) ⇒ Object



23
24
25
26
# File 'lib/dubs/data.rb', line 23

def pattern(name)
  key = name.to_s
  patterns.fetch(key) { raise PatternNotFound, "Unknown pattern: #{name}" }
end

.pattern_namesObject



28
29
30
# File 'lib/dubs/data.rb', line 28

def pattern_names
  patterns.keys.map(&:to_sym)
end

.patternsObject



19
20
21
# File 'lib/dubs/data.rb', line 19

def patterns
  pattern_data["patterns"]
end

.random_themeObject



57
58
59
# File 'lib/dubs/data.rb', line 57

def random_theme
  themes.values.sample
end

.register_theme(name:, display_name: nil, categories:, default_category:) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/dubs/data.rb', line 41

def register_theme(name:, display_name: nil, categories:, default_category:)
  key = name.to_s.tr("_", "-")
  data = {
    "name" => key,
    "display_name" => display_name || name.to_s,
    "categories" => categories.transform_keys(&:to_s).transform_values { |v| v.map(&:to_s) },
    "default_category" => default_category.to_s
  }
  MUTEX.synchronize { themes[key] = Theme.new(data) }
end

.register_theme_file(path) ⇒ Object



52
53
54
55
# File 'lib/dubs/data.rb', line 52

def register_theme_file(path)
  data = JSON.parse(File.read(path))
  MUTEX.synchronize { themes[data["name"]] = Theme.new(data) }
end

.theme(name) ⇒ Object



32
33
34
35
# File 'lib/dubs/data.rb', line 32

def theme(name)
  key = name.to_s.tr("_", "-")
  themes.fetch(key) { raise ThemeNotFound, "Unknown theme: #{name}. Available: #{theme_names.join(", ")}" }
end

.theme_namesObject



37
38
39
# File 'lib/dubs/data.rb', line 37

def theme_names
  themes.keys.map { |k| k.tr("-", "_").to_sym }
end