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: [
    'adoc', 'asciidoc', 'atom', 'css', 'erb', 'haml', 'htm', 'html', 'js',
    'less', 'markdown', 'md', 'org', 'php', 'rb', 'sass', 'scss', 'tex',
    'txt', 'xhtml', 'xml', 'coffee', 'hb', 'handlebars', 'mustache', 'ms',
    'slim', 'rdoc'
  ].sort,
  lib_dirs: ['lib'],
  commands_dirs: ['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(dir:, hash: {}, env_name: nil) ⇒ Configuration

Returns a new instance of Configuration.



55
56
57
58
59
60
61
# File 'lib/nanoc/core/configuration.rb', line 55

def initialize(dir:, hash: {}, 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.



45
46
47
# File 'lib/nanoc/core/configuration.rb', line 45

def dir
  @dir
end

#env_nameString? (readonly)

Returns The active environment for the configuration.

Returns:

  • (String, nil)

    The active environment for the configuration



42
43
44
# File 'lib/nanoc/core/configuration.rb', line 42

def env_name
  @env_name
end

Instance Method Details

#==(other) ⇒ Object



204
205
206
# File 'lib/nanoc/core/configuration.rb', line 204

def ==(other)
  eql?(other)
end

#[](key) ⇒ Object



103
104
105
# File 'lib/nanoc/core/configuration.rb', line 103

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

#[]=(key, value) ⇒ Object



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

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

#action_providerObject



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

def action_provider
  self[:action_provider].to_sym
end

#attributesObject



93
94
95
# File 'lib/nanoc/core/configuration.rb', line 93

def attributes
  to_h
end

#dig(*keys) ⇒ Object



108
109
110
# File 'lib/nanoc/core/configuration.rb', line 108

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

#eachObject



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

def each(&)
  @wrapped.each(&)
  self
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


209
210
211
212
213
# File 'lib/nanoc/core/configuration.rb', line 209

def eql?(other)
  other.is_a?(self.class) &&
    @dir == other.dir &&
    @env_name == other.env_name
end

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



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/nanoc/core/configuration.rb', line 115

def fetch(key, fallback = Nanoc::Core::UNDEFINED, &)
  @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



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

def freeze
  super
  @wrapped.__nanoc_freeze_recursively
  self
end

#hashObject



199
200
201
# File 'lib/nanoc/core/configuration.rb', line 199

def hash
  [@dir, @env_name].hash
end

#inspectObject



194
195
196
# File 'lib/nanoc/core/configuration.rb', line 194

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/nanoc/core/configuration.rb', line 98

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

#merge(hash) ⇒ Object



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

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

#output_dirObject



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

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

#output_dirsObject



180
181
182
183
184
185
# File 'lib/nanoc/core/configuration.rb', line 180

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



190
191
192
# File 'lib/nanoc/core/configuration.rb', line 190

def reference
  'configuration'
end

#to_hObject



87
88
89
# File 'lib/nanoc/core/configuration.rb', line 87

def to_h
  @wrapped
end

#update(hash) ⇒ Object



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

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

#with_defaultsObject



64
65
66
67
68
69
70
71
# File 'lib/nanoc/core/configuration.rb', line 64

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



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/nanoc/core/configuration.rb', line 73

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:).merge(env_config)
end

#without(key) ⇒ Object



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

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