Node.js向けORM
Bookshelfの紹介を参照させていただきながら、Bookshelf触ってみている。
ページではsqlite3だけど、自分はmysqlで使っている。
外部キー制約の部分。
1
|
t.integer('post_id').notNull().references('id').inTable('posts');
|
がmysqlではCannot add foreign key constraintが出て動かなかった。
これで動いた。
1
|
t.integer('post_id').unsigned().notNullable().references('id').inTable('posts');
|
例えば外部システムとユーザの情報を持ち合っている時。
外部システムのuser _id(コード内ではuser _external _id)から、システム内のユーザIDを導き出して、そのデータにパッチを当てるスクリプト。
active _recordとかは単品で扱ってる(database.yml’読み込み)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#!/usr/bin/env ruby
# coding: utf-8
require 'csv'
require 'optparse'
require "active_record"
params = ARGV.getopts('', 'csv:user.csv', 'ENV:development', 'RAILS_ROOT:./')
p params
config = YAML.load_file( params['RAILS_ROOT'] + 'config/database.yml' )
raise ArgumentError, 'ENVが設定されていません' if params['ENV'].nil?
raise ArgumentError, 'RAILS_ROOTが指定されていません' if params['RAILS_ROOT'].nil?
raise ArgumentError, 'database.ymlを読み出せませんでした' if config[params['ENV']].nil?
raise ArgumentError, 'csvファイルが存在しません' unless File.exists?(params['csv'])
ActiveRecord::Base.establish_connection(config[params['ENV']])
class User < ActiveRecord::Base
end
column = %w(user_external_id name birthday)
CSV.foreach(params['csv']) do |row|
# 一行を全部hashに入れる
row_hash = {}
row.each_with_index do |r,idx|
row_hash[column[idx]] = r
end
user = User.find_by_external_id(row_hash['user_external_id'])
if !user.nil?
user.update_attribute(:name => row_hash['name'])
end
end
|
実行はターミナルで
お客様から、半角カナを全角カナにしておくれと依頼がきていたので、railsのrootから半角カナを含むファイルを抜き出したくてスクリプト書きました。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#!/usr/bin/env ruby
require 'find'
except_ext = %w(.log .yml .jpg .JPG .jpeg .png .pid .ttf .woff .md .json .ico .eot .gif) # 除外するファイル拡張子を設定
except_pattern = %w(.git test .DS_Store migrate hankaku_kana_checker RackMultipart sprockets letter_opener underscore cache) # 除外するパスに含まれる文字列を指定
# チェックする半角カナの配列
han_kana = %w{ア イ ウ エ オ カ キ ク ケ コ サ シ ス セ ソ タ チ ツ テ ト ナ ニ ヌ ネ ノ ハ ヒ フ ヘ ホ マ ミ ム メ モ ヤ ユ ヨ ラ リ ル レ ロ ワ ヲ ン ァ ィ ゥ ェ ォ ャ ュ ョ ッ ゙ ゚ 、 。 ー 「 」}
Find.find(File.expand_path('./')) do |path|
next if File.directory?(path)
next if except_ext.include?(File.extname(path)) # 調査しない拡張子
next if path.match(Regexp.new(except_pattern.join("|")))
begin
File.readlines(path).each.with_index(1) do |line, idx|
@idx, @line = idx, line
# CP932のファイルが混在してたのでダメならCP932で変換
include_han_kana = begin
line.match(Regexp.new(han_kana.join("|")))
rescue
line.encode("UTF-8", "CP932").match(Regexp.new(han_kana.join("|")))
end
if include_han_kana
p [idx, path, line].join(": ")
end
end
rescue => ex
p "####################################################################################################"
p [@idx, @line].join(": ")
p [ path, ex.message ].join(":")
p ex.backtrace.first
p "####################################################################################################"
end
end
|
再帰的にディレクトリをくだって、探します。
含めたくないファイル名やディレクトリ名。拡張子を指定できます。
朝これを見た。
iTermでsshするときに、接続先に応じて背景色を変える方法
http://qiita.com/k_kinukawa/items/3e5665325a4954e33019
いいなと思った。
でも普段僕はMacの標準のTerminalを使っている。
Terminalで同じこと出きないかなとぐぐったらすぐあった。
https://github.com/dlobraico/dotfiles/blob/master/bin/ssh-host-color
でも僕は透過率が75%にしていて透過率を設定したいのだけど。
osascriptで透過率の設定がよくわからない。
というか普段ターミナルの色とかはProの設定を使っているのでProの使いたいなーって思ってググってprofileを切り替えられるようにした。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
#!/bin/bash
#
# ssh into a machine and automatically set the background
# color of Mac OS X Terminal depending on the hostname.
#
# Installation:
# 1. Save this script to /some/bin/ssh-host-color
# 2. chmod 755 /some/bin/ssh-host-color
# 3. alias ssh=/some/bin/ssh-host-color
# 4. Configure your host colors below.
set_term_bgcolor() {
local R=$1
local G=$2
local B=$3
/usr/bin/osascript <<EOF
tell application "Terminal"
tell window 0
set the background color to {$(($R*65535/255)), $(($G*65535/255)), $(($B*65535/255)), 32767}
end tell
end tell
EOF
}
set_term_profile() {
local profile=$1
/usr/bin/osascript -e "tell application "Terminal " to set current settings of first window to settings set "$profile ""
}
# Host-specific background colors.
if [[ "$@" =~ production1.com ]]; then
set_term_profile "HomeBrew"
elif [[ "$@" =~ production2.com ]]; then
set_term_bgcolor 0 40 0
fi
ssh $@
# Default background color.
set_term_profile "Pro"
|
set _term _profileでプロファイルの名前を渡せばそれに切り替わる。
set _bg _colorも残してる。
めっちゃ簡単に出来るんですね。
全然知らなかった。
http://stackoverflow.com/questions/10418193/how-to-add-methods-to-a-has-many-collection
1
2
3
4
5
6
7
8
9
10
11
|
class Customer < ActiveRecord::Base
belongs_to :store
end
class Store < ActiveRecord::Base
has_many :customers do
def names
self.map(&:name)
end
end
end
|
1
2
|
rails console
Store.last.customers.names # => customersの名前の配列が取れる!
|
javascript力が弱くて、クラス使って何か書きたくて、しかも
coffeescriptで書きたくて、
CTRL + Z
というブラウザの標準の機能があるのに、jsで戻る、やり直しを行うことができるプラグインを作りました。
正直使い道がよくわかりません。どのあたりをターゲットにしているのかもちょっとわかりません。
こんな感じでエレメントを指定してformを対象にして
1
|
form = new FormObserver('form');
|
1
2
3
4
|
// 一つ前の操作に戻す
form.undo();
// やり直し
form.redo();
|
って感じで、操作できます。
IE10とchromeでは動きました。
よくテストはしてません。
バグがあったらすいませんすいません。
使って見たい方はこちらからどうぞ
https://github.com/YoshitsuguFujii/form-undo.js
clearなんてあったんや。
http://ref.xaio.jp/ruby/classes/array/clear
今まで
1
2
|
array = ["hoge", "fuga"]
array = []
|
ってやってたのが、
1
2
|
array = ["hoge", "fuga"]
array.clear # => []
|
って書ける。より直感的ですね。
ちなみに文字列もHashもあるみたいです。
1
2
3
4
5
|
hash = {hoge: "fuga"}
hash.clear # {}
str = "fugafufa"
str.clar # => ""
|
へー。
1行で書けてすっきり。
1
|
values = (option.value for option in document.getElementsByName("multi-select") when option.selected)
|
デバッグ文を仕込む場合に、私の環境ではインサートモードでCTRL+bするとデバッグ文を仕込むようにしています。
その際に言語毎(ファイルタイプ毎)に異なるデバッグ文を打ち込めるようにしたら便利すぎて辛いです。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
function! InsertDebugger()
if &filetype == 'coffee' || &filetype == 'javascript'
let debugger = "debugger"
elseif &filetype == 'ruby'
let debugger = "binding.pry"
elseif &filetype == 'eruby'
let debugger = "<% binding.pry %>"
elseif &filetype == 'haml'
let debugger = "- binding.pry"
else
let debugger = "binding.pry"
endif
return debugger
endfunction
inoremap <expr> <C-b> InsertDebugger()
|
編集中のファイルが
coffeescrptかjabascriptの場合はdebuggerを(chrome使ってるので)
rubyの場合はbinding.pryを
erbの場合は <% binding.pry % >を
hamlの場合は- binding.pryを打ち込みます。
ちなみに今編集中のファイルのファイル・タイプを知りたいばあいは該当のファイルを開いて
ってうつと表示されます
vimを使っていて。
ディレクトリはUniteBookmarkAdd使ってブックマークしてましたが。
集中的にある機能のみを重点的に編集する時に、ファイルのブックマークしたくて調べたらあった。
しかもデフォルトの機能っぽい。
Hack #16: 任意のファイルをブックマークする
http://vim-users.jp/2009/05/hack16/
例えば店舗とかっていう機能開発していて。
StoreControllerのコントロールファイルではmC
StoreのformのビューファイルではmF
StoreのindexファイルではmS
StoreのjsファイルではmJ
しとくと編集していて、すぐにコントローラ開きたい時は
`C
って押せばすぐ開く。
簡単に設定できて、即応性があるのでいい感じですね。
しかもブックマークとかにしちゃうと、設定後その機能の開発が終わったらブックマークを消さなきゃいけないと思いますが。
今なにが設定されているとか気にせずに、常に上書きで使う感じもいいです。
他にも色々便利機能ってあるのかな。
vimの本ちゃんと読まないとなー