Class: Lookbook::ConfigStore

Inherits:
Object
  • Object
show all
Defined in:
lib/lookbook/stores/config_store.rb

Overview

Configuration store for Lookbook.

Config option values can be get/set using hash property access syntax or dot-notation syntax.

Examples:

:ruby

# Lookbook config access in Rails config files:
config.lookbook.ui_theme = :zinc

# Lookbook config access everywhere else:
Lookbook.config.ui_theme = :zinc

Constant Summary collapse

CONFIG_FILE =
"config/app.yml"
DEFAULT_FAVICONS =
{
  light: "assets/img/favicon_light.svg",
  dark: "assets/img/favicon_dark.svg"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ ConfigStore

Returns a new instance of ConfigStore.



26
27
28
# File 'lib/lookbook/stores/config_store.rb', line 26

def initialize(config = nil)
  @store = Store.new(config, recursive: true)
end

Instance Attribute Details

#storeObject (readonly)



24
25
26
# File 'lib/lookbook/stores/config_store.rb', line 24

def store
  @store
end

Class Method Details

.default_config(env: Rails.env) ⇒ Object



95
96
97
# File 'lib/lookbook/stores/config_store.rb', line 95

def self.default_config(env: Rails.env)
  ConfigLoader.call(CONFIG_FILE, env: env)
end

.init_from_config(env: Rails.env) ⇒ Object



91
92
93
# File 'lib/lookbook/stores/config_store.rb', line 91

def self.init_from_config(env: Rails.env)
  new(default_config(env: env))
end

Instance Method Details

#highlighter_options=(options = nil) ⇒ Object



58
59
60
# File 'lib/lookbook/stores/config_store.rb', line 58

def highlighter_options=(options = nil)
  store[:highlighter_options].merge!(options.to_h)
end

#listen_extensions=(extensions = nil) ⇒ Object



50
51
52
# File 'lib/lookbook/stores/config_store.rb', line 50

def listen_extensions=(extensions = nil)
  store[:listen_extensions].push(*extensions.to_a).uniq!
end

#markdown_options=(options = nil) ⇒ Object



54
55
56
# File 'lib/lookbook/stores/config_store.rb', line 54

def markdown_options=(options = nil)
  store[:markdown_options].merge!(options.to_h)
end

#page_extensions=(extensions = nil) ⇒ Object



46
47
48
# File 'lib/lookbook/stores/config_store.rb', line 46

def page_extensions=(extensions = nil)
  store[:page_extensions].push(*extensions.to_a).uniq!
end

#preview_disable_error_handling=(value) ⇒ Object



62
63
64
# File 'lib/lookbook/stores/config_store.rb', line 62

def preview_disable_error_handling=(value)
  store[:preview_disable_error_handling] = value
end

#project_nameObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lookbook/stores/config_store.rb', line 30

def project_name
  if store[:project_name].nil?
    if store[:project_logo].nil?
      Rails.application.class.module_parent.name.titleize
    else
      false
    end
  else
    store[:project_name]
  end
end

#project_name=(name) ⇒ Object



42
43
44
# File 'lib/lookbook/stores/config_store.rb', line 42

def project_name=(name)
  store[:project_name] = name
end

#ui_favicon_darkObject



79
80
81
# File 'lib/lookbook/stores/config_store.rb', line 79

def ui_favicon_dark
  @_ui_favicon_dark ||= get_favicon(:dark)
end

#ui_favicon_lightObject



75
76
77
# File 'lib/lookbook/stores/config_store.rb', line 75

def ui_favicon_light
  @_ui_favicon_light ||= get_favicon(:light)
end

#ui_theme=(name) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/lookbook/stores/config_store.rb', line 66

def ui_theme=(name)
  name = name.to_s
  if Theme.valid_theme?(name)
    store[:ui_theme] = name
  else
    raise ConfigError.new("'#{name}' is not a valid Lookbook theme. ", scope: "app.config")
  end
end

#ui_theme_overrides(&block) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/lookbook/stores/config_store.rb', line 83

def ui_theme_overrides(&block)
  if block
    yield store[:ui_theme_overrides]
  else
    store[:ui_theme_overrides]
  end
end