Rails チュートリアル 1章 勉強メモ8

Rails チュートリアルの勉強メモ

railstutorial.jp

1章続き rails new 何やってんの?

set_default_accessors!

- rails/generators/app_base.rb @ line 169 Rails::Generators::AppBase#set_default_accessors!:

    167: def set_default_accessors! # :doc:
 => 168:   self.destination_root = File.expand_path(app_path, destination_root)
    169:   self.rails_template = \
    170:     case options[:template]
    171:     when /^https?:\/\//
    172:       options[:template]
    173:     when String
    174:       File.expand_path(options[:template], Dir.pwd)
    175:     else
    176:       options[:template]
    177:     end
    178: end

create_root

- rails/generators/app_base.rb @ line 156 Rails::Generators::AppBase#create_root:

    154: def create_root # :doc:
 => 155:   valid_const?
    156: 
    157:   empty_directory "."
    158:   FileUtils.cd(destination_root) unless options[:pretend]
    159: end
  • valid_const
- rails/generators/rails/app/app_generator.rb @ line 463 Rails::Generators::AppGenerator#valid_const?:

    462: def valid_const?
 => 463:   if app_const =~ /^\d/
    464:     raise Error, "Invalid application name #{app_name}. Please give a name which does not start with numbers."
    465:   elsif RESERVED_NAMES.include?(app_name)
    466:     raise Error, "Invalid application name #{app_name}. Please give a " \
    467:                  "name which does not match one of the reserved rails " \
    468:                  "words: #{RESERVED_NAMES.join(", ")}"
    469:   elsif Object.const_defined?(app_const_base)
    470:     raise Error, "Invalid application name #{app_name}, constant #{app_const_base} is already in use. Please choose another application name."
    471:   end
    472: end
  • アプリの名前の先頭に数字を入れたらダメ
  • RESERVED_NAMES に含まれている名前はダメ
    • => ["application", "destroy", "plugin", "runner", "test"]
  • すでに定義されている定数を使ったらダメ

  • empty_directory "."

    • 空の #{app_name} ディレクトリを作っている
      • 掘ってみると empty_directory => EmptyDirectory.new => self.destination = destination => @destination = ::File.expand_path(@given_destination, base.destination_root) で "." を実行PATH/app_name に変換していた
  • FileUtils.cd(destination_root) unless options[:pretend]
    • 面倒だから追わないけど多分作った app_name のディレクトリに移動している

create_root_files

- rails/generators/rails/app/app_generator.rb @ line 250 Rails::Generators::AppGenerator#create_root_files:

    249: def create_root_files
 => 250:   build(:readme)
    251:   build(:rakefile)
    252:   build(:configru)
    253:   build(:gitignore)   unless options[:skip_git]
    254:   build(:gemfile)     unless options[:skip_gemfile]
    255:   build(:version_control)
    256: end
- /rails/generators/app_base.rb @ line 151 Rails::Generators::AppBase#build:

    150: def build(meth, *args) # :doc:
 => 151:   builder.send(meth, *args) if builder.respond_to?(meth)
    152: end
  • meth のメソッドがあれば実行(今回でいえば readme
- rails/generators/rails/app/app_generator.rb @ line 49 Rails::AppBuilder#readme:

    48: def readme
 => 49:   copy_file "README.md", "README.md"
    50: end
  • 少し追った感じだと rails/generators/rails/app/templates/README.md をコピーしている
    def rakefile
      template "Rakefile"
    end
  • template は source_paths から template を探して app_name 直下に生成している
  • build(:configru) では template の config.ru から生成
  • build(:gitignore) でも同様(skip_git が true なら skipできる
  • build(:gemfile) では Gemfile を生成
  • build(:version_control)
- rails/generators/rails/app/app_generator.rb @ line 65 Rails::AppBuilder#version_control:

    64: def version_control
 => 65:   if !options[:skip_git] && !options[:pretend]
    66:     run "git init"
    67:   end
    68: end
  • skip_git や pretend が オプションになければ git init する

create_app_files

- rails/generators/rails/app/app_generator.rb @ line 260 Rails::Generators::AppGenerator#create_app_files:

    259: def create_app_files
 => 260:   build(:app)
    261: end
- rails/generators/rails/app/app_generator.rb @ line 71 Rails::AppBuilder#app:

    70: def app
 => 71:   directory "app"
    72: 
    73:   keep_file "app/assets/images"
    74:   empty_directory_with_keep_file "app/assets/javascripts/channels" unless options[:skip_action_cable]
    75: 
    76:   keep_file  "app/controllers/concerns"
    77:   keep_file  "app/models/concerns"
    78: end
- ruby/gems/2.6.0/gems/railties-5.1.6/lib/rails/generators/app_base.rb @ line 438 Rails::Generators::AppBase#keep_file:

    437: def keep_file(destination)
 => 438:   create_file("#{destination}/.keep") if keeps?
    439: end
  • keeps? は skip_keeps が true かどうかチェックしている
  • skip_keeps が false なら "app/assets/images" の下に .keep を生成
    • これなんだろう?(ファイルの中身は空
  • empty_directory_with_keep_file "app/assets/javascripts/channels" unless options[:skip_action_cable]
- ruby/gems/2.6.0/gems/railties-5.1.6/lib/rails/generators/app_base.rb @ line 433 Rails::Generators::AppBase#empty_directory_with_keep_file:

    432: def empty_directory_with_keep_file(destination, config = {})
 => 433:   empty_directory(destination, config)
    434:   keep_file(destination)
    435: end
  • 空のディレクトリを作ってそこにkeepファイルを置いている

create_bin_files

      def create_bin_files
        build(:bin)
      end
    def bin
      directory "bin" do |content|
        "#{shebang}\n" + content
      end
      chmod "bin", 0755 & ~File.umask, verbose: false
    end
  • directory "app" のときとほぼ同様に bin を template から生成
    • 生成したファイルの先頭に "#{shebang}\n" を付け足している
      • 自分の環境では
#!/usr/bin/env ruby
  • パーミッションを変更 verbose: false は変更を出力しないオプション

  • 同じような処理が続きそうな気がするが一旦今日は終了

    • 大体同じであればそろそろ rails new は終わる予感