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

NONE =
Object.new.freeze
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

Constructor Details

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

Returns a new instance of Configuration.



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

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.



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

def dir
  @dir
end

#env_nameString? (readonly)

Returns The active environment for the configuration.

Returns:

  • (String, nil)

    The active environment for the configuration



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

def env_name
  @env_name
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



120
121
122
# File 'lib/nanoc/core/configuration.rb', line 120

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

#action_providerObject



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

def action_provider
  self[:action_provider].to_sym
end

#attributesObject



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

def attributes
  to_h
end

#dig(*keys) ⇒ Object



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

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

#eachObject



141
142
143
144
# File 'lib/nanoc/core/configuration.rb', line 141

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

#fetch(key, fallback = NONE, &_block) ⇒ Object



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

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

#freezeObject



147
148
149
150
151
# File 'lib/nanoc/core/configuration.rb', line 147

def freeze
  super
  @wrapped.__nanoc_freeze_recursively
  self
end

#inspectObject



177
178
179
# File 'lib/nanoc/core/configuration.rb', line 177

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#merge(hash) ⇒ Object



125
126
127
# File 'lib/nanoc/core/configuration.rb', line 125

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

#output_dirObject



154
155
156
# File 'lib/nanoc/core/configuration.rb', line 154

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

#output_dirsObject



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

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



173
174
175
# File 'lib/nanoc/core/configuration.rb', line 173

def reference
  'configuration'
end

#to_hObject



81
82
83
# File 'lib/nanoc/core/configuration.rb', line 81

def to_h
  @wrapped
end

#update(hash) ⇒ Object



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

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

#with_defaultsObject



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

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



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

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



130
131
132
# File 'lib/nanoc/core/configuration.rb', line 130

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