Module: Nanoc::Core::Feature

Defined in:
lib/nanoc/core/feature.rb

Overview

Examples:

Defining a feature and checking its enabledness


Nanoc::Core::Feature.define('environments', version: '4.3')
Nanoc::Core::Feature.enabled?(Nanoc::Core::Feature::ENVIRONMENTS)

Class Method Summary collapse

Class Method Details

.all_outdatedEnumerable<String>

Returns Names of features that still exist, but should not be considered as experimental in the current version of Nanoc.

Returns:

  • (Enumerable<String>)

    Names of features that still exist, but should not be considered as experimental in the current version of Nanoc.



84
85
86
87
88
89
# File 'lib/nanoc/core/feature.rb', line 84

def self.all_outdated
  repo.keys.reject do |name|
    version = repo[name]
    Nanoc::Core::VERSION.start_with?(version)
  end
end

.define(name, version:) ⇒ void

This method returns an undefined value.

Defines a new feature with the given name, experimental in the given version. The feature will be made available as a constant with the same name, in uppercase, on the Nanoc::Core::Feature module.

Examples:

Defining Nanoc::Core::Feature::ENVIRONMENTS


Nanoc::Core::Feature.define('environments', version: '4.3')

Parameters:

  • name

    The name of the feature

  • version

    The minor version in which the feature is considered experimental.



25
26
27
28
# File 'lib/nanoc/core/feature.rb', line 25

def self.define(name, version:)
  repo[name] = version
  const_set(name.upcase, name)
end

.enable(feature_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/nanoc/core/feature.rb', line 51

def self.enable(feature_name)
  raise ArgumentError, 'no block given' unless block_given?

  if enabled?(feature_name)
    yield
  else
    begin
      enabled_features << feature_name
      yield
    ensure
      enabled_features.delete(feature_name)
    end
  end
end

.enabled?(feature_name) ⇒ Boolean

Returns Whether or not the feature with the given name is enabled.

Parameters:

Returns:

  • (Boolean)

    Whether or not the feature with the given name is enabled



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

def self.enabled?(feature_name)
  enabled_features.include?(feature_name) ||
    enabled_features.include?('all')
end

.enabled_featuresObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



72
73
74
# File 'lib/nanoc/core/feature.rb', line 72

def self.enabled_features
  @_enabled_features ||= Set.new(ENV.fetch('NANOC_FEATURES', '').split(','))
end

.repoObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



77
78
79
# File 'lib/nanoc/core/feature.rb', line 77

def self.repo
  @_repo ||= {}
end

.reset_cachesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



67
68
69
# File 'lib/nanoc/core/feature.rb', line 67

def self.reset_caches
  @_enabled_features = nil
end

.undefine(name) ⇒ void

This method returns an undefined value.

Undefines the feature with the given name. For testing purposes only.

Parameters:

  • name

    The name of the feature



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

def self.undefine(name)
  repo.delete(name)
  remove_const(name.upcase)
end