Class: Shojiku::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/shojiku/settings.rb

Overview

One client's resolved configuration, plus the collaborators built from it.

Config answers "what was configured"; this answers "what does THIS client use", which is the merge of the process-wide defaults with the arguments the client was constructed with. Keeping it out of Client keeps the precedence rules in one readable place instead of spread across a constructor.

Everything is built lazily and memoized: a bytes-first application never configures a template root, and demanding one at construction would refuse a legitimate client.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**overrides) ⇒ Settings

Returns a new instance of Settings.



18
19
20
21
# File 'lib/shojiku/settings.rb', line 18

def initialize(**overrides)
  @config = Shojiku.config.merge(overrides)
  @lang = @config.lang
end

Instance Attribute Details

#langObject (readonly)

Returns the value of attribute lang.



16
17
18
# File 'lib/shojiku/settings.rb', line 16

def lang
  @lang
end

Instance Method Details

#envObject



32
33
34
# File 'lib/shojiku/settings.rb', line 32

def env
  @env ||= Env.new(enabled: @config.env)
end

#font_dirsObject



48
49
50
# File 'lib/shojiku/settings.rb', line 48

def font_dirs
  @font_dirs ||= @config.font_dirs || env.paths("SHOJIKU_FONT_DIR")
end

#libraryObject



44
45
46
# File 'lib/shojiku/settings.rb', line 44

def library
  @library ||= Library.new(path: @config.library, env: env, log: log)
end

#locale_dirsObject



52
53
54
# File 'lib/shojiku/settings.rb', line 52

def locale_dirs
  @locale_dirs ||= @config.locale_dirs || env.paths("SHOJIKU_LOCALE_DIR")
end

#lockdownObject



40
41
42
# File 'lib/shojiku/settings.rb', line 40

def lockdown
  @lockdown ||= Lockdown.new(strict: @config.strict, providers: @config.providers)
end

#logObject



36
37
38
# File 'lib/shojiku/settings.rb', line 36

def log
  @log ||= Log.new(@config.logger)
end

#template_rootObject

The template root, or nil when nothing configured one.

defined? rather than ||= because nil is a legitimate answer here and would otherwise be re-resolved on every call.



60
61
62
63
64
65
# File 'lib/shojiku/settings.rb', line 60

def template_root
  return @template_root if defined?(@template_root)

  root = @config.templates || env["SHOJIKU_TEMPLATE_ROOT"]
  @template_root = root ? TemplateRoot.new(root) : nil
end

#with_lang(lang) ⇒ Object

A copy that renders in lang, for Client#with_lang. Everything already built — the opened library, the template root, the lockdown — is carried over by dup, so deriving a client re-opens nothing.



26
27
28
29
30
# File 'lib/shojiku/settings.rb', line 26

def with_lang(lang)
  copy = dup
  copy.override_lang(lang)
  copy
end