W3cubDocs

/Padrino

Module: Padrino::Generators::Actions

Overview

Common actions needed to support project and component generation.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary

Instance Method Details

#already_exists?(name, project_name = nil) ⇒ Boolean

Returns true if constant name already exists.

Returns:

  • (Boolean)

#app_skeleton(app, tiny = false) ⇒ Object

Generates standard and tiny applications within a project.

Examples:

app_skeleton 'some_app'
app_skeleton 'sub_app', true

Parameters:

  • app (String) — Name of application.
  • tiny (Boolean) (defaults to: false) — Boolean to generate a tiny structure.

#apply_component_for(choice, component) ⇒ Object

Returns the related module for a given component and option.

Examples:

apply_component_for('rr', :mock)

Parameters:

  • choice (String) — The name of the component module.
  • component (Symbol) — The type of the component module.

#apply_default_fields(fields) ⇒ Array<String>

Apply default field types.

Parameters:

  • fields (Array<String>) — Field names for generators.

Returns:

  • (Array<String>) — fields with default types

#check_app_existence(app) ⇒ Object

Raise SystemExit if the app does not exist.

Examples:

check_app_existence 'app'

Parameters:

  • app (String) — Directory name of application.

#destination_root(*paths) ⇒ String

Returns the root for this Thor class (also aliased as destination root).

Examples:

destination_root('config/boot.rb')

Parameters:

  • paths (Array<String>) — The relative path from destination root.

Returns:

#empty_directory_with_keep_file(destination, config = {}) ⇒ Object

Creates an empty directory with .keep file

#execute_component_setup(component, choice) ⇒ Object

Performs the necessary generator for a given component choice.

Examples:

execute_component_setup(:mock, 'rr')

Parameters:

  • component (Symbol) — The type of component module.
  • choice (String) — The name of the component module choice.

#fetch_app_name(app = 'app') ⇒ String

Returns the app_name for the application at root.

Examples:

fetch_app_name('subapp')

Parameters:

  • app (String) (defaults to: 'app') — folder name of application.

Returns:

  • (String) — class name for application.

#fetch_component_choice(component) ⇒ String

Returns the component choice stored within the .component file of an application.

Examples:

fetch_component_choice(:mock)

Parameters:

  • component (Symbol) — The type of component module.

Returns:

  • (String) — Name of the component module.

#fetch_project_name(app = 'app') ⇒ String

Returns the namespace for the project.

Examples:

fetch_project_name

Parameters:

  • app (String) (defaults to: 'app') — folder name of application.

Returns:

  • (String) — namespace for application.

#in_app_root? ⇒ Boolean

Returns true if inside a Padrino application.

Returns:

  • (Boolean)

#include_component_module_for(component, choice = nil) ⇒ Object

Includes the component module for the given component and choice. It determines the choice using .components file.

Examples:

include_component_module_for(:mock)
include_component_module_for(:mock, 'rr')

Parameters:

  • component (Symbol) — The type of component module.
  • choice (String) (defaults to: nil) — The name of the component module.

#initializer(name, data = nil) ⇒ Object

Registers and creates initializer.

Examples:

initializer(:test, "some stuff here")
#=> generates 'lib/test_init.rb'

Parameters:

  • name (Symbol) — Name of the initializer.
  • data (String) (defaults to: nil) — Text to generate into the initializer file.

#inject_into_file(destination, *args, &block) ⇒ Object

Avoids editing destination file if it does not exist.

#insert_hook(include_text, where) ⇒ Object

Inserts an hook before or after load in our boot.rb.

Examples:

insert_hook("DataMapper.finalize", :after_load)

Parameters:

  • include_text (String) — Text to include into hooks in boot.rb.
  • where (Symbol) — method hook to call from Padrino, i.e :after_load, :before_load.

#insert_into_gemfile(name, options = {}) ⇒ Object

Inserts a required gem into the Gemfile to add the bundler dependency.

Examples:

insert_into_gemfile(name)
insert_into_gemfile(name, :group => 'test', :require => 'foo')
insert_into_gemfile(name, :group => 'test', :version => ">1.2.3")

Parameters:

  • name (String) — Name of gem to insert into Gemfile.
  • options (Hash) (defaults to: {}) — Options to generate into Gemfile for gem.

#insert_middleware(include_text, app = nil) ⇒ Object

Inserts a middleware inside app.rb.

Examples:

insert_middleware(ActiveRecord::ConnectionAdapters::ConnectionManagement)

Parameters:

  • include_text (String) — Text to include into hooks in boot.rb.

#invalid_fields(fields) ⇒ Array<String>

Returns the field with an unacceptable name(for symbol) else returns nil.

Examples:

invalid_fields ['foo:bar', 'hello:world']

Parameters:

  • fields (Array<String>) — Field names for generators.

Returns:

  • (Array<String>) — array of invalid fields

#keep_file(destination) ⇒ Object

Creates an empty .keep file

#middleware(name, source) ⇒ Object

Creates and inserts middleware.

Examples:

middleware(:hello, "class Hello\nend")
#=> generates 'lib/hello_middleware.rb'

Parameters:

  • name (Symbol, String) — Name of the middleware.
  • source (String) — Text to generate into the middleware file.

#recognize_path ⇒ Object

Recognizes the path of application.

#require_contrib(contrib) ⇒ Object

Insert the regired gem and add in boot.rb custom contribs.

Examples:

require_contrib('auto_locale')

Parameters:

  • contrib (String) — name of library from padrino-contrib

#require_dependencies(*gem_names) ⇒ Object

Adds all the specified gems into the Gemfile for bundler.

Examples:

require_dependencies('active_record')
require_dependencies('mocha', 'bacon', :group => 'test')
require_dependencies('json', :version => ">=1.2.3")

Parameters:

  • gem_names (Array<String>) — Splat of gems to require in Gemfile.
  • options (Hash) — The options to pass to gem in Gemfile.

#resolve_valid_choice(component) ⇒ String

Prompts the user if necessary until a valid choice is returned for the component.

Examples:

resolve_valid_choice(:mock)

Parameters:

  • component (Symbol) — The type of component module.

Returns:

  • (String) — Name of component if valid, otherwise ask for valid choice.

#retrieve_component_config(target) ⇒ Hash

Loads the component config back into a hash.

Examples:

retrieve_component_config(...)
# => { :mock => 'rr', :test => 'rspec', ... }

Parameters:

  • target (String) — Path to component config file.

Returns:

  • (Hash) — Loaded YAML file.

#run_bundler ⇒ Object

Run the bundler.

#store_component_choice(key, value) ⇒ Symbol

Set the component choice in the .component file of the application.

Examples:

store_component_choice(:renderer, :haml)

Parameters:

  • key (Symbol) — The type of component module.
  • value (Symbol) — The name of the component module.

Returns:

  • (Symbol) — The name of the component module.

#store_component_config(destination, opts = {}) ⇒ Object

Creates a component_config file at the destination containing all component options. Content is a YAMLized version of a hash containing component name mapping to chosen value.

Examples:

store_component_config('/foo/bar')

Parameters:

  • destination (String) — The file path to store the component config.

#test? ⇒ Boolean

Return true if our project has test component.

Returns:

  • (Boolean)

#tiny? ⇒ Boolean

Return true if we have a tiny skeleton.

Returns:

  • (Boolean)

#valid_choice?(component, choice) ⇒ Boolean

Returns true if the option passed is a valid choice for component.

Examples:

valid_choice?(:mock, 'rr')

Parameters:

  • component (Symbol) — The type of component module.
  • choice (String) — The name of the component module.

Returns:

  • (Boolean) — Boolean of whether the choice is valid.

#valid_constant?(name) ⇒ Exception

Ensures that project name is valid, else raise an NameError.

Examples:

valid_constant '1235Stuff'
valid_constant '#Abc'

Parameters:

  • name (String) — Name of project.

Returns:

  • (Exception) — Exception with error message if not valid.

#validate_namespace(name) ⇒ Object

Validates namespace name (controller name, etc.) or fails with an error.

Examples:

validate_namespace 'Project_One1' #=> pass
validate_namespace 'Erroneous/name' #=> fail

Parameters:

  • name (String) — Name of namespace

© 2010–2019 Padrino
Licensed under the MIT License.
https://www.rubydoc.info/github/padrino/padrino-framework/Padrino/Generators/Actions