Module: Nanoc::Core::ContractsSupport

Defined Under Namespace

Modules: DisabledContracts, EnabledContracts Classes: Ignorer

Class Method Summary collapse

Class Method Details

.enabled?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/nanoc/core/contracts_support.rb', line 126

def self.enabled?
  setup_once
end

.included(base) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/nanoc/core/contracts_support.rb', line 130

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/nanoc/core/contracts_support.rb', line 91

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

  @_contracts_support__should_enable =
    contracts_loadable && !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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/nanoc/core/contracts_support.rb', line 145

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