Module: Nanoc::Core::ContractsSupport

Included in:
ActionSequenceBuilder, ActionSequenceBuilder::CannotCreateMultipleSnapshotsWithSameNameError, ActionSequenceStore, Assertions::AllItemRepsHaveCompiledContent, Assertions::PathIsAbsolute, BinaryCompiledContentCache, ChecksumCollection, ChecksumStore, CodeSnippet, CompilationContext, Nanoc::Core::CompilationContext::FilterNameAndArgs, Nanoc::Core::CompilationPhases::Abstract, Nanoc::Core::CompilationPhases::Cache, Nanoc::Core::CompilationPhases::MarkDone, Nanoc::Core::CompilationPhases::Notify, Nanoc::Core::CompilationPhases::Recalculate, Nanoc::Core::CompilationPhases::Resume, Nanoc::Core::CompilationPhases::Write, Nanoc::Core::CompilationStages::BuildReps, Nanoc::Core::CompilationStages::CompileReps, Nanoc::Core::CompilationStages::DetermineOutdatedness, Nanoc::Core::CompilationStages::ForgetOutdatedDependencies, Nanoc::Core::CompilationStages::LoadStores, Nanoc::Core::CompilationStages::Postprocess, Nanoc::Core::CompilationStages::StorePostCompilationState, Nanoc::Core::CompilationStages::StorePreCompilationState, CompiledContentCache, CompiledContentStore, Compiler, Configuration, Content, Dependency, DependencyProps, DependencyStore, DependencyTracker, DependencyTracker::Null, Document, Filter, Filter::FilterReturnedNilError, Filter::OutputNotWrittenError, Filter::UnknownFilterError, Identifier, InMemoryDataSource, ItemRep, ItemRepBuilder, ItemRepRouter, ItemRepWriter, LazyValue, OutdatednessRule, OutdatednessRules::AttributesModified, OutdatednessStore, Pattern, ProcessingActions::Snapshot, Pruner, Site, SnapshotDef, Store, TextualCompiledContentCache, View, ViewContextForCompilation, ViewContextForPreCompilation, ViewContextForShell
Defined in:
lib/nanoc/core/contracts_support.rb

Defined Under Namespace

Modules: DisabledContracts, EnabledContracts Classes: Ignorer

Class Method Summary collapse

Class Method Details

.enabled?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/nanoc/core/contracts_support.rb', line 114

def self.enabled?
  setup_once
end

.included(base) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/nanoc/core/contracts_support.rb', line 118

def self.included(base)
  should_enable = setup_once

  if should_enable
    unless base.include?(::Contracts::Core)
      base.include(::Contracts::Core)
      base.extend(EnabledContracts)
      base.const_set('C', ::Contracts)
    end
  else
    base.extend(DisabledContracts)
    base.const_set('C', DisabledContracts)
  end
end

.setup_onceObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/nanoc/core/contracts_support.rb', line 85

def self.setup_once
  @_contracts_support__setup ||= false
  return @_contracts_support__should_enable if @_contracts_support__setup

  @_contracts_support__setup = true

  contracts_loadable =
    begin
      require 'contracts'
      true
    rescue LoadError
      false
    end

  # FIXME: Do something better with contracts on Ruby 3.x
  @_contracts_support__should_enable = contracts_loadable && !RUBY_VERSION.start_with?('3') && !ENV.key?('DISABLE_CONTRACTS')

  if @_contracts_support__should_enable
    # FIXME: ugly
    ::Contracts.const_set('Named', EnabledContracts::Named)
    ::Contracts.const_set('IterOf', EnabledContracts::IterOf)
    ::Contracts.const_set('AbsolutePathString', EnabledContracts::AbsolutePathString)

    warn_about_performance
  end

  @_contracts_support__should_enable
end

.warn_about_performanceObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/nanoc/core/contracts_support.rb', line 133

def self.warn_about_performance
  return if ENV.key?('CI')
  return if ENV.key?('NANOC_DEV_MODE')

  puts '-' * 78
  puts 'A NOTE ABOUT PERFORMANCE:'
  puts 'The `contracts` gem is loaded, which enables extra run-time checks, but can drastically reduce Nanoc’s performance. The `contracts` gem is intended for development purposes, and is not recommended for day-to-day Nanoc usage.'
  puts

  if defined?(Bundler)
    puts 'To speed up compilation, remove `contracts` from the Gemfile and run `bundle install`.'
  else
    puts 'To speed up compilation, either uninstall the `contracts` gem, or use Bundler (https://bundler.io/) with a Gemfile that doesn’t include `contracts`.'
  end

  puts '-' * 78
end