Class: Cuprum::Cli::Files::Generator
- Parent Namespace
- Cuprum::Cli::Files
- Inherited Classes
- Cuprum::Command
>
Object
- Extended Modules
- Cuprum::Cli::Options::ClassMethods
- Included Modules
- Cuprum::Cli::Dependencies::StandardIo::Helpers, Cuprum::Cli::Options::Quiet, Cuprum::Cli::Options::Verbose, Plumbum::Consumer, Plumbum::Parameters
- Defined In
- lib/cuprum/cli/files/generator.rb
Table Of Contents
Overview
Command for generating templated files.
Back To Top
Direct Subclasses
Cuprum::Cli::Files::Generators::RSpecGenerator, Cuprum::Cli::Files::Generators::RubyGenerator
Back To Top
Defined Under Namespace
- Classes
- AbstractGeneratorError,
Output,
OutputAlreadyExistsError
Back To Top
Constants
FALSY_VALUES
= Set.new(%w[f false n no]).freeze
- Inherited From
- Cuprum::Cli::Dependencies::StandardIo::Helpers
String input values that will be mapped to a boolean false.
INTEGER_PATTERN
= /\A-?\d+([\d_,]+\d)?\z/
- Inherited From
- Cuprum::Cli::Dependencies::StandardIo::Helpers
Pattern matching a valid integer input.
TRUTHY_VALUES
= Set.new(%w[t true y yes]).freeze
- Inherited From
- Cuprum::Cli::Dependencies::StandardIo::Helpers
String input values that will be mapped to a boolean true.
Back To Top
Class Methods
.abstract => Object
Marks the generator as abstract.
.abstract? => true, false
Returns
- (true, false) — true if the generator is abstract and should not
be assigned matchers or outputs; otherwise false.
.match_file(pattern) => void
.match_file(&block) => void
Overloads
.match_file(pattern) => void
Registers the pattern as a valid match for the generator.
The registered matches are checked when the generator checks whether a
file path can be generated by the generator.
- If the given pattern is a regular expression, the generator will
match the pattern against the file path.
- If the pattern is a string, the generator will check if the file
path ends with the pattern string.
Parameters
- pattern (Regexp, String) — the pattern to register.
Returns
.match_file(&block) => void
Registers the block as a valid match for the generator.
The registered matches are checked when the generator checks whether a
file path can be generated by the generator. The given block will be
called with the file path and generator options; if the block returns
a truthy value, the match check returns true.
Yield Parameters
- file_path (String) — the input file path provided by the
user.
- options (Hash) — additional options for the generator.
Returns
.matches?(file_path, **options) => true, false
Also known as:
match?
Checks if the generator matches the given file path and options.
the given file type, path, and options; otherwise false.
Parameters
- file_path (String) — the input file path provided by the user.
- options (Hash) — additional options for the generator.
Returns
- (true, false) — true if the generator can generate files with
.option(name, aliases: [], default: nil, description: nil, required: false, type: :string, **options) => Object
- Inherited From
- Cuprum::Cli::Options::ClassMethods
Defines an option for the command class.
Parameters
- name (String, Symbol) — the name of the option.
- aliases (Array<String, Symbol>) — aliases for the option when
parsing options from the command line.
- default (Object, Proc) — the default value for the option. If given
and the value of the option is nil, sets the option value to the
default value.
- description (String) — a short, human-readable description of the
option.
- required (true, false) — if true, raises an exception if the option
is not provided to the command.
- type (Class, String, Symbol) — the expected type of the option
value as a Class or class name. If given, raises an exception if the
option value is not an instance of the type. Defaults to :string.
- options (Hash) — additional options for defining the option.
Options Hash (options)
- define_method (true, false) — if true, defines a reader
method for the option. Defaults to false for boolean options and true
for all other options.
- define_predicate (true, false) — if true, defines a
predicate method for the option, which returns true if the option is
not nil and not empty. Defaults to true for boolean options and false
for all other options.
.option_value(option, value) => void
- Inherited From
- Cuprum::Cli::Options::ClassMethods
Assigns a predefined option value for the command.
Parameters
- option (String, Symbol) — the option to set.
- value (Object) — the option value to append.
Returns
.option_values => Hash{Symbol => Object}
- Inherited From
- Cuprum::Cli::Options::ClassMethods
Returns
- (Hash{Symbol => Object}) — predefined option values for the command.
- Inherited From
- Cuprum::Cli::Options::ClassMethods
The defined options, including options defined on ancestor classes.
Returns
.output(path, key: :default, template: nil) => void
Registers an output file for the generator.
The output path parameter is a String that can include format strings,
which will be resolved from the options passed to the generator. In
addition, the format strings can reference the following parameters
derived from the input file_path. The following examples use an input
file path of “lib/path/to/file.rb”:
- base_name: The last segment of the file path, including the file
extension. Example: “file.rb”.
- dir_name: The directory path from the file path, relative to the
working directory. Example: “lib/path/to”.
- ext_name: The extension of the file path, including the leading
period character. Example: “.rb”.
- file_path: The full file path. Example: “lib/path/to/file.rb”.
- relative_path: The second and later segments of the directory path,
or an empty String if the path is too short. Example: “path/to”.
- root_path: The first segment of the directory path, or an empty
String if the path is too short. Example: “lib”.
- short_name: The last segment of the file path, excluding the file
extension. Example: “file”.
Parameters
- path (String) — the file path for the generated file.
- key (String, Symbol) — a unique key used to identify the output.
Defaults to :default.
- template (String) — the path to the template file for the output.
If not provided, the user must provide a template for that output.
Returns
.outputs => Hash{Symbol => Output}
Returns
- (Hash{Symbol => Output}) — the configured outputs for the
generator.
.resolve_options(**values) => Hash
- Inherited From
- Cuprum::Cli::Options::ClassMethods
Validates the given option values against the defined class options.
Also applies any default values from the defined options.
Parameters
- values (Hash) — the option values to resolve.
Returns
- (Hash) — the option values with applied defaults.
Raises
Back To Top
Constructor
#initialize(file_path, **options) => Object
Overloads
#initialize(file_path, **options) => Object
Parameters
- file_path (String, nil) — the input file path provided by the user.
- options (Hash) — additional options for the generator.
Options Hash (options)
- dry_run (true, false) — if true, simulates file generation
but does not perform the actual file system operations. Defaults to
false.
- quiet (true, false) — if true, does not print generated
file names status to STDOUT. Defaults to false.
- verbose (true, false) — if true, prints the contents of
generated files to STDOUT. Defaults to false.
Returns
- (Generator) — a new instance of Generator
Back To Top
Instance Attributes
#file_path => String (readonly)
Returns
- (String) — the input file path provided by the user.
#options => Hash (readonly)
Returns
- (Hash) — additional options for the generator.
Back To Top
Instance Methods
#ask(prompt = nil, caret: true, format: nil, strip: true, **options) => String, Integer, true, false, nil
- Inherited From
- Cuprum::Cli::Dependencies::StandardIo::Helpers
Requests an input from the input stream.
Parameters
- prompt (String, nil) — the prompt to display to the user, if any.
- options (Hash) — options for requesting the input.
Options Hash (options)
- caret (true, false) — if true, prints a caret "> " to the
output stream after the prompt. Defaults to true when the newline
option is true, otherwise false.
- format (String, Symbol) — the expected format of the
input. Valid values are :string (the default), :boolean, and :integer.
The input string will be transformed into the given format, or an
exception raised if the value cannot be transformed.
- newline (true, false) — if true, a newline will be printed
after the prompt if a prompt is given.
- strip (true, false) — if true, strips the trailing newline
from the input. Defaults to true.
Returns
- (String, Integer, true, false, nil) — the received and formatted
input value, or nil if the input value was empty.
#file_parameters => Hash
Returns
- (Hash) — parameters extracted from the input file name.
#parameters => Hash
Returns
- (Hash) — parameters used to resolve output file paths and contents.
#say(message, newline: true, quiet: false, verbose: false, **options) => nil
- Inherited From
- Cuprum::Cli::Options::Verbose
Prints a message to the output stream.
Parameters
- message (String) — the message to print.
- options (Hash) — options for printing the message.
Options Hash (options)
- newline (true, false) — if true, appends a newline to the
message if the message does not end with a newline. Defaults to true.
- quiet (true, false) — if true, prints the message even if
the command has the :quiet option enabled. Defaults to false. Ignored
if
the command does not support the :quiet option.
- verbose (true, false) — if true, prints the message only
if the command has the :verbose option enabled. Defaults to false.
Ignored if the command does not support the :verbose option.
Returns
#warn(message, **options) => nil
- Inherited From
- Cuprum::Cli::Dependencies::StandardIo::Helpers
Prints a message to the error stream.
Parameters
- message (String) — the message to print.
- options (Hash) — options for printing the message.
Options Hash (options)
- newline (true, false) — if true, appends a newline to the
message if the message does not end with a newline. Defaults to true.
Returns
Back To Top
Back to
Documentation |
Reference
|
Cuprum
|
Cuprum::Cli
|
Cuprum::Cli::Files