Class: Fronde::Config::Store

Inherits:
Object
  • Object
show all
Includes:
Lisp, Singleton
Defined in:
lib/fronde/config.rb

Overview

Wrapper for configuration

This class is a singleton interface, which share the static website being build settings among different steps or tasks.

It expects the website author to holds their custom settings in a YAML file named ~config.yml~ available at the root of their project.

For example, with the given config file:

#+begin_src


title: My website author: Alice Doe #+end_src

Settings will be available like this:

#+begin_src Fronde::CONFIG.get(‘author’)

> “Alice Doe”

#+end_src

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Lisp

#write_org_lisp_config

Constructor Details

#initializeStore

Returns a new instance of Store.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fronde/config.rb', line 40

def initialize
  @default_settings = {
    'author' => ENV['USER'] || '',
    'domain' => '',
    'lang' => Fronde::Config::Helpers.extract_lang_from_env('en'),
    'html_public_folder' => 'public_html',
    'gemini_public_folder' => 'public_gmi',
    'templates' => [], 'theme' => 'default'
  }.freeze
  # Do not load sources now to avoid dependency loop on config
  @sources = nil
  @config = load_settings
end

Instance Attribute Details

#sourcesObject (readonly)

Returns the value of attribute sources.



38
39
40
# File 'lib/fronde/config.rb', line 38

def sources
  @sources
end

Instance Method Details

#get(setting, default = nil) ⇒ Object

Return a named setting.

~setting~ may be a ~String~ or an ~Array~.

If the given ~setting~ name is an Array, this method will behave as ~Hash::dig~.

If no value is found for the given setting, ~default~ will be returned.

Parameters:

  • setting (String, Array)

    the setting to get

  • default (defaults to: nil)

    the default value to use if ~setting~ is absent

Returns:

  • the setting value or nil



76
77
78
79
80
81
82
83
# File 'lib/fronde/config.rb', line 76

def get(setting, default = nil)
  if setting.is_a? Array
    value = @config.dig(*setting)
  else
    value = @config[setting]
  end
  value || default
end

#load_sourcesArray

Return the qualified projects sources list.

Returns:

  • (Array)

    the fully qualified projects sources list



120
121
122
123
124
# File 'lib/fronde/config.rb', line 120

def load_sources
  return @sources if @sources

  @sources = remove_inclusion(remove_duplicate(build_sources))
end

#load_test(config) ⇒ Fronde::Config::Store

Load the given settings as if they comes from the ~config.yml~ file.

This method is handy for testing purpose. Next call to get, sources or settings will use these new settings.

Parameters:

  • config (Hash)

    the settings to artificially load

Returns:



110
111
112
113
114
115
# File 'lib/fronde/config.rb', line 110

def load_test(config)
  @config = @default_settings.merge config
  @sources = nil
  @sources = load_sources
  self
end

#resetObject

Reset settings

This method is handy for testing purpose. Next call to get or settings will force reload the settings from the config file

Returns:

  • nil



93
94
95
96
97
98
# File 'lib/fronde/config.rb', line 93

def reset
  # Reload config, taking default settings into account
  @config = load_settings
  @sources = nil
  @sources = load_sources
end

#settingsHash

Access the current website settings

Returns:

  • (Hash)

    the website settings



59
60
61
# File 'lib/fronde/config.rb', line 59

def settings
  @config
end