経理からエンジニア転向した人のメモ

元経理マンがエンジニアに転向して現在

Macでwgetでエラーが出ていたのを解消

maxOS High Sierra 10.13.6

簡単なクローラーを体験しようとしてwgetをインストールしようとしたらできなかった。 似たようなエラーはネットで見つかったが、どれもライブラリが若干違っていた。

// インストールしていない場合はHomebrewをインストール。
$ brew --version
Homebrew 1.8.6
Homebrew/homebrew-core (git revision 91f90; last commit 2019-01-08)
Homebrew/homebrew-cask (git revision 366f9; last commit 2019-01-07)

$ brew install wget

$ wget
dyld: Library not loaded: /usr/local/opt/pcre/lib/libpcre.1.dylib
  Referenced from: /usr/local/bin/wget
  Reason: image not found
Abort trap: 6

解決方法

単純にprecライブラリがなかったので、$ brew install precで解決。

$ brew install pcre
==> Downloading https://homebrew.bintray.com/bottles/pcre-8.42.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring pcre-8.42.high_sierra.bottle.tar.gz
🍺  /usr/local/Cellar/pcre/8.42: 204 files, 5.3MB

$ wget
wget: URLがありません
使い方: wget [オプション]... [URL]...

詳しいオプションは `wget --help' を実行してください

使うときは$ wget -O- http://~でダウンロードできる。

atomのerbファイルのスニペットを有効にする。

atomのerbファイルの色が気持ち悪かったので、色々調べていたら以下の記事が見つかった。

qiita.com

ちなみに気持ち悪い色のやつ(ERBタイプ) f:id:ryomoyr:20181030144802p:plain:w500

ついでに<%%>とかも- + Tabでできることを知って導入。

いちいちatomの下の、ファイルタイプをERBからHTML(Rails)に変更するのがめんどくさかったので、 常時HTML(Rails)で読み込んでくれるように設定をしたかった。

File-Typesをインストール

atom.io

[Atom] -> [Config] でconfig.csonファイルが開かれるので、

"*":
  "file-types":
    "*.erb": "text.html.ruby"

の記述をすれば、常時HTML(Rails)ファイルで読み込んでくれるようになった。

f:id:ryomoyr:20181030145215p:plain:w500

こっちのほうがよろしい。

Railsでconcernsを使えば、application_controller.rbを汚さずに共通化できる

通常?今までRailsでapplication_controller.rbに共通化した処理を書いていたが、
エラー処理を書いてあるのでできればそちらを使わずに共通化をしたい。ということで、concernsを使ってリファクタリングする。 そもそもconcernsで共通化するのでスタンダード?かも。

お題としては、
- トップページには最新の記事を5件だけ表示したい。(about等複数ページ)
- 記事一覧ページでは最新の記事を20件表示したい。
- 記事は@latest_entriesというインスタンス変数でViewに渡したい。

こちらをリファクタリングしていく様子を記述する。

通常の無駄な記述 (リファクタリング前)

home_controller.rb

class HomeController < ApplicationController
  def index
    @latest_articles = Article.limit(5).order("created_at DESC")
  end
end

articles_controller.rb

class ArticleController < ApplicationController
  def index
    @latest_articles = Article.limit(20).order("created_at DESC")
  end
end

DRYの哲学があるので、共通化をする。

application_controller.rbでリファクタリング(今まで)

application_controller.rb

class ApplicationController < ApplicationController

  def error_hogehoge
  end

  def error_fugafuga
  end

  def latest_articles(num)
    @latest_articles = Article.limit(num).order("created_at DESC")
  end
end

error_hogehogeはエラー処理のメソッドとして、サンプルとして記述してある。

home_controller.rb

class HomeController < ApplicationController
  before_action -> { latest_articles(5) }, only: [:index]

  def index
  end
end

articles_controller.rb

class ArticleController < ApplicationController
  before_action -> { latest_articles(20) }, only: [:index]

  def index
  end
end

こんな感じで共通化していく。 が、application_controller.rbはエラー処理の記述があって、できれば汚したくない! という場合にconcernsを使って共通化もできる。

concernsでリファクタリング

latest_articles.rb

module LatestArticles
  extend ActiveSupport::Concern

  def latest_articles(num)
    @latest_articles = Article.limit(num).order("created_at DESC")
  end

end

application_controller.rb

class ApplicationController < ApplicationController

  def error_hogehoge
  end

  def error_fugafuga
  end

end

ここには何も書かなくても、大丈夫。

home_controller.rb

class HomeController < ApplicationController
  include LatestArticles
  before_action -> { latest_articles(5) }, only: [:index]

  def index
  end
end

articles_controller.rb

class ArticleController < ApplicationController
  include LatestArticles
  before_action -> { latest_articles(20) }, only: [:index]

  def index
  end
end

concernsの方にクラスとかメソッドを記述して、 controllerの方で、include クラス名を記述してあげれば利用できる。

これだけだとあまり有り難みがないかもしれないが、
application_controller.rbに記述しなくても、
concernsで共通化ができる。

使いようによってはもっと複雑なこともできる。はず。