Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Como Fazer Minha Gem e o Rails Conversarem Bem

Como Fazer Minha Gem e o Rails Conversarem Bem

Vamos ver como é importante fazer sua gem saber gerar códigos para a instalação dela mesmo junto ao Rails e como é importante importante documentar bem.

Breno Oliveira

May 21, 2017
Tweet

More Decks by Breno Oliveira

Other Decks in Programming

Transcript

  1. Initializer: initializer $ rails g --help Running via Spring preloader

    in process 95922 Usage: rails generate GENERATOR [args] [options] General options: -h, [--help] # Print generator's options and usage -p, [--pretend] # Run but do not make any changes -f, [--force] # Overwrite files that already exist -s, [--skip] # Skip files that already exist -q, [--quiet] # Suppress status output Please choose a generator below. … … ?
  2. lib/generators/my_gem_name/initializer_generator.rb module MyGemName module Generators class InitializerGenerator < Rails::Generators::NamedBase def

    create_initializer_file create_file "config/initializers/initializer2.rb", “# Seu código aqui" end end end end
  3. $ rails g --help Running via Spring preloader in process

    95922 Usage: rails generate GENERATOR [args] [options] General options: -h, [--help] # Print generator's options and usage -p, [--pretend] # Run but do not make any changes -f, [--force] # Overwrite files that already exist -s, [--skip] # Skip files that already exist -q, [--quiet] # Suppress status output Please choose a generator below. … … MyGemName: my_gem_name:initializer
  4. module MyGemName module Generators class InitializerGenerator < Rails::Generators::NamedBase def create_initializer_file

    desc "This generator creates an initializer file at config/initializers” create_file "config/initializers/initializer2.rb", “# Seu código aqui" end end end end lib/generators/my_gem_name/initializer_generator.rb
  5. $ rails g my_gem_name:initializer --help Running via Spring preloader in

    process 98283 Usage: rails generate my_gem_name:initializer NAME [options] Options: [--skip-namespace], [--no-skip-namespace] # Skip namespace (affects only isolated applications) Runtime options: -f, [--force] # Overwrite files that already exist -p, [--pretend], [--no-pretend] # Run but do not make any changes -q, [--quiet], [--no-quiet] # Suppress status output -s, [--skip], [--no-skip] # Skip files that already exist This generator creates an initializer file at config/initializers
  6. lib/generators/my_gem_name/USAGE Description: Explicação sobre o que minha Gem faz Example:

    rails generate my_gem_name:initializer This will create: config/initializers/my_gem_initializer.rb
  7. $ rails g my_gem_name:initializer --help Running via Spring preloader in

    process 98741 Usage: rails generate my_gem_name:initializer NAME [options] Options: [--skip-namespace], [--no-skip-namespace] # Skip namespace (affects only isolate applications) Runtime options: -f, [--force] # Overwrite files that already exist -p, [--pretend], [--no-pretend] # Run but do not make any changes -q, [--quiet], [--no-quiet] # Suppress status output -s, [--skip], [--no-skip] # Skip files that already exist Description: Explicação sobre o que minha Gem faz Example: rails generate my_gem_name:initializer This will create: config/initializers/my_gem_initializer.rb
  8. SimpleHubspot.configure do |config| config.hapikey= "YOUR API KEY" config.portal_id= "YOUR PORTAL

    ID" end # Seu código aqui lib/generators/my_gem_name/templates/my_gem_initializer.rb
  9. module MyGemName module Generators class InitializerGenerator < Rails::Generators::NamedBase source_root File.expand_path('../templates',

    __FILE__) # desc "This generator creates an initializer file at config/initializers" def copy_initializer copy_file "my_gem_initializer.rb", "config/initializers/my_gem_initializer.rb" end end end end lib/generators/my_gem_name/initializer_generator.rb