Class: Toys::Templates::Rspec

Inherits:
Object
  • Object
show all
Includes:
Toys::Template
Defined in:
lib/toys/templates/rspec.rb

Overview

A template for tools that run rspec

Constant Summary collapse

DEFAULT_GEM_VERSION_REQUIREMENTS =

Default version requirements for gem names.

Returns:

  • (Hash{String=>Array<String>})
{
  "rspec" => ["~> 3.1"].freeze,
}.freeze
DEFAULT_TOOL_NAME =

Default tool name

Returns:

  • (String)
"spec"
DEFAULT_LIBS =

Default set of library paths

Returns:

  • (Array<String>)
["lib"].freeze
DEFAULT_ORDER =

Default order type

Returns:

  • (String)
"defined"
DEFAULT_FORMAT =

Default format code

Returns:

  • (String)
"p"
DEFAULT_PATTERN =

Default spec file glob

Returns:

  • (String)
"spec/**/*_spec.rb"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Toys::Template

create

Constructor Details

#initialize(name: nil, rspec: nil, gem_version: nil, gems: nil, libs: nil, options: nil, order: nil, format: nil, out: nil, backtrace: false, pattern: nil, warnings: true, bundler: false, context_directory: nil) ⇒ Rspec

Create the template settings for the RSpec template.

Note that arguments related to gem and bundler settings are defaults that can be overridden by command line arguments.

Parameters:

  • name (String) (defaults to: nil)

    Name of the tool to create. Defaults to DEFAULT_TOOL_NAME.

  • rspec (String, Array<String>) (defaults to: nil)

    Version requirements for the "rspec" gem, used if bundler is not enabled. Optional. If not provided, defaults to the value given in DEFAULT_GEM_VERSION_REQUIREMENTS.

  • gem_version (String, Array<String>) (defaults to: nil)

    Deprecated alias for the rspec argument.

  • gems (Hash{String=>String|Array<String>|true}) (defaults to: nil)

    Include the given gems with the given version requirements. Used if bundler is not enabled. If the version requirement is set to true, then the default in DEFAULT_GEM_VERSION_REQUIREMENTS is used. If there is no default available, then no particular version requirements are imposed.

  • libs (Array<String>) (defaults to: nil)

    An array of library paths to add to the ruby require path. Defaults to DEFAULT_LIBS.

  • options (String) (defaults to: nil)

    The path to a custom options file, if any.

  • order (String) (defaults to: nil)

    The order in which to run examples. Default is DEFAULT_ORDER.

  • format (String) (defaults to: nil)

    The formatter code. Default is DEFAULT_FORMAT.

  • out (String) (defaults to: nil)

    Write output to a file instead of stdout.

  • backtrace (boolean) (defaults to: false)

    Enable full backtrace (default is false).

  • pattern (String) (defaults to: nil)

    A glob indicating the spec files to load. Defaults to DEFAULT_PATTERN.

  • warnings (boolean) (defaults to: true)

    If true, runs specs with Ruby warnings. Defaults to true.

  • bundler (boolean, Hash) (defaults to: false)

    If false (the default), bundler is not used unless enabled via command line argument. If true or a Hash of options, bundler is enabled by default unless disabled via a command line argument. See the documentation for the bundler mixin for information on available options. Note that any :setup option is ignored; the bundle, if enabled, is always installed and set up at the start of the tool execution.

  • context_directory (String) (defaults to: nil)

    A custom context directory to use when executing this tool.



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
# File 'lib/toys/templates/rspec.rb', line 92

def initialize(name: nil,
               rspec: nil,
               gem_version: nil,
               gems: nil,
               libs: nil,
               options: nil,
               order: nil,
               format: nil,
               out: nil,
               backtrace: false,
               pattern: nil,
               warnings: true,
               bundler: false,
               context_directory: nil)
  @name = name
  @libs = libs
  @options = options
  @order = order
  @format = format
  @out = out
  @backtrace = backtrace
  @pattern = pattern
  @warnings = warnings
  @bundler = bundler
  @context_directory = context_directory
  @gem_dependencies = {}
  update_version_spec("rspec", rspec || gem_version)
  update_gems(gems) if gems
end

Instance Attribute Details

#backtrace=(value) ⇒ boolean

Whether to enable full backtraces.

Parameters:

  • value (boolean)

Returns:

  • (boolean)


210
211
212
# File 'lib/toys/templates/rspec.rb', line 210

def backtrace=(value)
  @backtrace = value
end

#bundler=(value) ⇒ boolean, Hash (writeonly)

Set the bundler state and options for this tool.

Pass false to disable bundler. Pass true or a hash of options to enable bundler. See the documentation for the bundler mixin for information on the options that can be passed.

Parameters:

  • value (boolean, Hash)

Returns:

  • (boolean, Hash)


248
249
250
# File 'lib/toys/templates/rspec.rb', line 248

def bundler=(value)
  @bundler = value
end

#context_directory=(value) ⇒ String

Custom context directory for this tool.

Parameters:

  • value (String)

Returns:

  • (String)


235
236
237
# File 'lib/toys/templates/rspec.rb', line 235

def context_directory=(value)
  @context_directory = value
end

#format=(value) ⇒ String?

The formatter code. If set to nil, defaults to DEFAULT_FORMAT.

Parameters:

  • value (String, nil)

Returns:

  • (String, nil)


193
194
195
# File 'lib/toys/templates/rspec.rb', line 193

def format=(value)
  @format = value
end

#libs=(value) ⇒ Array<String>?

An array of directories to add to the Ruby require path. If set to nil, defaults to DEFAULT_LIBS.

Parameters:

  • value (Array<String>, nil)

Returns:

  • (Array<String>, nil)


167
168
169
# File 'lib/toys/templates/rspec.rb', line 167

def libs=(value)
  @libs = value
end

#name=(value) ⇒ String

Name of the tool to create.

Parameters:

  • value (String)

Returns:

  • (String)


128
129
130
# File 'lib/toys/templates/rspec.rb', line 128

def name=(value)
  @name = value
end

#options=(value) ⇒ String?

Path to the custom options file, or nil for none.

Parameters:

  • value (String, nil)

Returns:

  • (String, nil)


175
176
177
# File 'lib/toys/templates/rspec.rb', line 175

def options=(value)
  @options = value
end

#order=(value) ⇒ String?

The order in which to run examples. If set to nil, defaults to DEFAULT_ORDER.

Parameters:

  • value (String, nil)

Returns:

  • (String, nil)


184
185
186
# File 'lib/toys/templates/rspec.rb', line 184

def order=(value)
  @order = value
end

#out=(value) ⇒ String?

Path to a file to write output to. If set to nil, writes output to standard out.

Parameters:

  • value (String, nil)

Returns:

  • (String, nil)


202
203
204
# File 'lib/toys/templates/rspec.rb', line 202

def out=(value)
  @out = value
end

#pattern=(value) ⇒ String?

A glob indicating the spec files to load. If set to nil, defaults to DEFAULT_PATTERN.

Parameters:

  • value (String, nil)

Returns:

  • (String, nil)


219
220
221
# File 'lib/toys/templates/rspec.rb', line 219

def pattern=(value)
  @pattern = value
end

#warnings=(value) ⇒ boolean

Whether to run with Ruby warnings.

Parameters:

  • value (boolean)

Returns:

  • (boolean)


227
228
229
# File 'lib/toys/templates/rspec.rb', line 227

def warnings=(value)
  @warnings = value
end

Instance Method Details

#rspec=(value) ⇒ Object Also known as: gem_version=

Version requirements for the rspec gem. Used if bundler is not used. If set to true or nil, a default version requirement is used.

Parameters:

  • value (String, Array<String>, true, nil)


155
156
157
# File 'lib/toys/templates/rspec.rb', line 155

def rspec=(value)
  update_version_spec("rspec", value)
end

#update_gems(gems) ⇒ self

Update the gems and version requirements that are used if bundler is not enabled.

Parameters:

  • gems (Hash{String=>String|Array<String>|true|false|nil})

    A mapping from gem name to either the version requirements, true to use a default, or false or nil to remove the gem from the list. (Note that it is not possible to remove the rspec gem, and setting it to nil will simply restore the default version requirements.)

Returns:

  • (self)


142
143
144
145
146
147
# File 'lib/toys/templates/rspec.rb', line 142

def update_gems(gems)
  gems.each do |gem_name, version_requirements|
    update_version_spec(gem_name, version_requirements)
  end
  self
end

#use_bundler(**opts) ⇒ self

Activate bundler for this tool.

See the documentation for the bundler mixin for information on the options that can be passed.

Parameters:

  • opts (keywords)

    Options for bundler

Returns:

  • (self)


260
261
262
263
# File 'lib/toys/templates/rspec.rb', line 260

def use_bundler(**opts)
  @bundler = opts
  self
end