Class: Nanoc::Core::Configuration

Inherits:
Object
  • Object
show all
Includes:
ContractsSupport
Defined in:
lib/nanoc/core/configuration.rb

Overview

Represents the site configuration.

Constant Summary collapse

DEFAULT_DATA_SOURCE_CONFIG =

The default configuration for a data source. A data source’s configuration overrides these options.

{
  type: 'filesystem',
  items_root: '/',
  layouts_root: '/',
  config: {},
  identifier_type: 'full',
}.freeze
DEFAULT_CONFIG =

The default configuration for a site. A site’s configuration overrides these options: when a Site is created with a configuration that lacks some options, the default value will be taken from ‘DEFAULT_CONFIG`.

{
  text_extensions: %w[adoc asciidoc atom css erb haml htm html js less markdown md php rb sass scss tex txt xhtml xml coffee hb handlebars mustache ms slim rdoc].sort,
  lib_dirs: %w[lib],
  commands_dirs: %w[commands],
  output_dir: 'output',
  data_sources: [{}],
  index_filenames: ['index.html'],
  enable_output_diff: false,
  prune: { auto_prune: false, exclude: ['.git', '.hg', '.svn', 'CVS'] },
  string_pattern_type: 'glob',
  action_provider: 'rule_dsl',
}.freeze
ENVIRONMENTS_CONFIG_KEY =

Configuration environments property key

:environments
NANOC_ENV =
'NANOC_ENV'
NANOC_ENV_DEFAULT =
'default'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ContractsSupport

enabled?, included, setup_once, warn_about_performance

Constructor Details

#initialize(hash: {}, dir:, env_name: nil) ⇒ Configuration

Returns a new instance of Configuration.



48
49
50
51
52
53
54
# File 'lib/nanoc/core/configuration.rb', line 48

def initialize(hash: {}, dir:, env_name: nil)
  @env_name = env_name
  @wrapped = hash.__nanoc_symbolize_keys_recursively
  @dir = dir

  validate
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



40
41
42
# File 'lib/nanoc/core/configuration.rb', line 40

def dir
  @dir
end

#env_nameString? (readonly)

Returns The active environment for the configuration.

Returns:

  • (String, nil)

    The active environment for the configuration



37
38
39
# File 'lib/nanoc/core/configuration.rb', line 37

def env_name
  @env_name
end

Instance Method Details

#[](key) ⇒ Object



95
96
97
# File 'lib/nanoc/core/configuration.rb', line 95

def [](key)
  @wrapped[key]
end

#[]=(key, value) ⇒ Object



118
119
120
# File 'lib/nanoc/core/configuration.rb', line 118

def []=(key, value)
  @wrapped[key] = value
end

#action_providerObject



157
158
159
# File 'lib/nanoc/core/configuration.rb', line 157

def action_provider
  self[:action_provider].to_sym
end

#attributesObject



85
86
87
# File 'lib/nanoc/core/configuration.rb', line 85

def attributes
  to_h
end

#dig(*keys) ⇒ Object



100
101
102
# File 'lib/nanoc/core/configuration.rb', line 100

def dig(*keys)
  @wrapped.dig(*keys)
end

#eachObject



139
140
141
142
# File 'lib/nanoc/core/configuration.rb', line 139

def each
  @wrapped.each { |k, v| yield(k, v) }
  self
end

#fetch(key, fallback = Nanoc::Core::UNDEFINED, &_block) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/nanoc/core/configuration.rb', line 105

def fetch(key, fallback = Nanoc::Core::UNDEFINED, &_block)
  @wrapped.fetch(key) do
    if !Nanoc::Core::UNDEFINED.equal?(fallback)
      fallback
    elsif block_given?
      yield(key)
    else
      raise KeyError, "key not found: #{key.inspect}"
    end
  end
end

#freezeObject



145
146
147
148
149
# File 'lib/nanoc/core/configuration.rb', line 145

def freeze
  super
  @wrapped.__nanoc_freeze_recursively
  self
end

#inspectObject



175
176
177
# File 'lib/nanoc/core/configuration.rb', line 175

def inspect
  "<#{self.class}>"
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/nanoc/core/configuration.rb', line 90

def key?(key)
  @wrapped.key?(key)
end

#merge(hash) ⇒ Object



123
124
125
# File 'lib/nanoc/core/configuration.rb', line 123

def merge(hash)
  self.class.new(hash: merge_recursively(@wrapped, hash.to_h), dir: @dir, env_name: @env_name)
end

#output_dirObject



152
153
154
# File 'lib/nanoc/core/configuration.rb', line 152

def output_dir
  make_absolute(self[:output_dir]).freeze
end

#output_dirsObject



162
163
164
165
166
# File 'lib/nanoc/core/configuration.rb', line 162

def output_dirs
  envs = @wrapped.fetch(ENVIRONMENTS_CONFIG_KEY, {})
  res = [output_dir] + envs.values.map { |v| make_absolute(v[:output_dir]) }
  res.uniq.compact
end

#referenceObject

Returns an object that can be used for uniquely identifying objects.

Returns:

  • (Object)

    An unique reference to this object



171
172
173
# File 'lib/nanoc/core/configuration.rb', line 171

def reference
  'configuration'
end

#to_hObject



79
80
81
# File 'lib/nanoc/core/configuration.rb', line 79

def to_h
  @wrapped
end

#update(hash) ⇒ Object



133
134
135
136
# File 'lib/nanoc/core/configuration.rb', line 133

def update(hash)
  @wrapped.update(hash)
  self
end

#with_defaultsObject



57
58
59
60
61
62
63
64
# File 'lib/nanoc/core/configuration.rb', line 57

def with_defaults
  new_wrapped = DEFAULT_CONFIG.merge(@wrapped)
  new_wrapped[:data_sources] = new_wrapped[:data_sources].map do |ds|
    DEFAULT_DATA_SOURCE_CONFIG.merge(ds)
  end

  self.class.new(hash: new_wrapped, dir: @dir, env_name: @env_name)
end

#with_environmentObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/nanoc/core/configuration.rb', line 66

def with_environment
  return self unless @wrapped.key?(ENVIRONMENTS_CONFIG_KEY)

  # Set active environment
  env_name = @env_name || ENV.fetch(NANOC_ENV, NANOC_ENV_DEFAULT)

  # Load given environment configuration
  env_config = @wrapped[ENVIRONMENTS_CONFIG_KEY].fetch(env_name.to_sym, {})

  self.class.new(hash: @wrapped, dir: @dir, env_name: env_name).merge(env_config)
end

#without(key) ⇒ Object



128
129
130
# File 'lib/nanoc/core/configuration.rb', line 128

def without(key)
  self.class.new(hash: @wrapped.reject { |k, _v| k == key }, dir: @dir, env_name: @env_name)
end