Class: RailsDesignProfiles::ProfileStore

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_design_profiles/profile_store.rb

Constant Summary collapse

CONFIG_PATH =
"config/design_profiles.yml"

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ ProfileStore

Returns a new instance of ProfileStore.



10
11
12
# File 'lib/rails_design_profiles/profile_store.rb', line 10

def initialize(root)
  @root = Pathname(root)
end

Instance Method Details

#activate!(name) ⇒ Object



56
57
58
59
60
61
# File 'lib/rails_design_profiles/profile_store.rb', line 56

def activate!(name)
  config = data
  profile_named(name, config)
  config["active"] = name
  write(config)
end

#active_profileObject



14
15
16
17
18
# File 'lib/rails_design_profiles/profile_store.rb', line 14

def active_profile
  profile_named(data.fetch("active"))
rescue KeyError
  raise ConfigurationError, "Missing active profile in #{config_path}"
end

#css_variablesObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rails_design_profiles/profile_store.rb', line 20

def css_variables
  tokens = active_profile.fetch("tokens", {})
  unless tokens.is_a?(Hash)
    raise ConfigurationError, "Profile tokens must be a mapping in #{config_path}"
  end

  tokens.filter_map do |name, value|
    next unless valid_token?(name, value)

    "--rdp-#{name}: #{value};"
  end.join
end

#install_reference!(slug, contents) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rails_design_profiles/profile_store.rb', line 33

def install_reference!(slug, contents)
  validate_slug!(slug)
  reference_path = references_path.join("#{slug}.md")
  raise ConfigurationError, "Reference already exists: #{reference_path}" if reference_path.exist?

  config = data
  profiles = config.fetch("profiles")
  if profiles.key?(slug)
    raise ConfigurationError, "Profile already exists: #{slug}"
  end

  FileUtils.mkdir_p(reference_path.dirname)
  reference_path.write(contents)
  profiles[slug] = {
    "reference" => reference_path.relative_path_from(@root).to_s,
    "tokens" => {}
  }
  write(config)
rescue StandardError
  FileUtils.rm_f(reference_path) if defined?(reference_path) && reference_path&.exist?
  raise
end