gist

2013年1月11日金曜日

Rails 3.2.11へのアップデート方法メモ(緊急)

2012年12月18日火曜日

Django 1.4の日本語ドキュメントをビルドするメモ

2012年10月14日日曜日

Mac OS X Mountain Lion (10.8.2) に Rails 3.2.8 をインストールする

約100日ぶりの更新です。
RailsとiOS/Androidアプリとクラウド3点セットのお仕事が増えてきましたので、少しNode.jsから離れて、更新を続けようと思います。

Moutain LionにRailsをインストールしたときの作業のログです。
Rubyのバージョン管理ツールにrbenvを使用しています。

事前準備

  • AppStoreでXcode 4.5.1 をインストールしておく
  • Xcode 4.5.1の設定からCommand Line Tools をインストールしておく

Homebrewをインストールする

rbenvをインストールする

.bash_profileを編集して以下の一行を追加します。

$ vim ~/.bash_profile

再読込を忘れずに

ruby-buildをインストール

rbenv install -l でインストール可能なバージョンのリストが表示されればOKです。

Rubyをインストールする

使用しているrubyのバージョンが1.9.3-p194でパスが変更されていればOK。

Railsをインストールする

Railsアプリを作成する

ブラウザから http://localhost:3000/ にアクセスして以下の画面が表示されたRailsのインストールは完了です。

2012年7月2日月曜日

CoffeeScriptで学ぶ Underscore.js 15(Chain編)

CoffeeScriptで学ぶUnderscore.js(Chain編)。今回で15回目、ようやく最終回です。


chain

_.chain(object)

chainは、underscoreのメソッドを次々と連結して実行できる便利なメソッドです。

_ = require 'underscore'

users = [
 {name : 'curly', age : 25}, 
 {name : 'moe', age : 21}, 
 {name : 'larry', age : 23}
]

youngest = 
 _.chain(users)
 .sortBy (user)->
  user.age
 .map (user)->
  user.name + ' is ' + user.age
 .first()
 .value()

console.log youngest
実行結果
$ coffee chain.coffee 
moe is 21

_.chain(users)でchainに配列を与えています。次にsortByでage順に並べます。次のmapで「◯◯ is 年齢」の出力を作成します。firstで最初の行を取得し、最後にvalueで値にしています。

Underscore.jsでは、

_.map [1,2,3], (n)->
 n * 2

は、以下のようにも記述できます。

_([1,2,3]).map (n)->
 n * 2

ですので、上記のchainの部分は以下のように分解できます。

sorted = _(users).sortBy (user)->
 user.age
mapped = _(sorted).map (user)->
 user.name + ' is ' + user.age
first = _(mapped).first()
value = _(first).value()

value

_(obj).value()

valueは、objを展開します。chainの最後に負荷することで値として展開できます。

_ = require 'underscore'

console.log _([3,2,6]).value()
実行結果
$ coffee value.coffee 
[ 3, 2, 6 ]

Underscore.jsは、JavaScriptを強力にサポートするパッケージです。是非とも使いこなしてみましょう。


関連ページ

2012年6月30日土曜日

CoffeeScriptで学ぶ Underscore.js 14(Template編)

CoffeeScriptで学ぶ、Underscore.js。このエントリーで14回目です。今回はTemplate編。Underscore.jsが最も普及したのも、このtemplateがあったからのように思えます。templateは、公式サイトでも気合の入った説明がされています。


template

_.template(templateString,[data],[settings])

templateは、JavaScriptテンプレートをコンパイルし、レンダリングします。JSONデータソースからHTMLの一部をレンダリングするのに使われています。Template関数では、変数を出力する場合は、<%= ... %>を使います。JavaScriptのコードを実行するには、<% ... %>を使います。HTMLエスケープしたい場合、<%- ... %>を使います。

_ = require 'underscore'

template = _.template "hello <%= name %>"
console.log template {name: 'miku'}

list = '''
<% _.each(people, function(name) { %>
<li><%= name %></li>
<% }); %> 
'''
template = _.template list, {people:['MIKU', 'RIN', 'LEN']}
console.log template

template = _.template '<b><%- value %></b>'
console.log template {value: '<script>'}
実行結果
$ coffee template.coffee 
hello miku

  • MIKU
  • RIN
  • LEN
  • &lt;script&gt;

    templateSettings関数を使うと、テンプレートを書き換えることができます。テンプレートとしてメジャーなMustache.jsスタイルのテンプレートにするには以下のように指定します。

    _ = require 'underscore'
    
    _.templateSettings =
      interpolate : /\{\{(.+?)\}\}/g
    
    template = _.template "Hello {{ name }}!"
    console.log template {name : "Mustache"}
    
    実行結果
    $ coffee templateSettings.coffee 
    Hello Mustache!
    

    関連ページ

    2012年6月27日水曜日

    CoffeeScriptで学ぶ Underscore.js 13(Utility編)

    いよいよUtility編。大詰めです。


    noConflict

    _.noConflict

    noConflictは、変数である「_」(アンダースコア)を別の変数に置き換えることができます。Underscoreのオブジェクトを返します。

    _ = require 'underscore'
    
    underscore = _.noConflict()
    
    _ = 10
    
    console.log _
    console.log underscore
    
    実行結果
    $ coffee noConflict.coffee 
    10
    { [Function]
      _: [Circular],
      VERSION: '1.3.3',
      forEach: [Function],
      each: [Function],
      collect: [Function],
      map: [Function],
    ...
    

    identity

    _.identity(value)

    identityは、valueを与えたとき、返り値としてそのままvalueを返します。underscoreの初期のイテレータに利用されているそうな。

    _ = require 'underscore'
    
    moe = {name:'moe'}
    
    console.log moe is _.identity moe
    
    実行結果
    $ coffee identity.coffee 
    true
    

    times

    _.times(n, iterator)

    timesは、iteratorをn回実行します。

    _ = require 'underscore'
    
    _.times 10, ()->
     console.log 'hello'
    
    実行結果
    $ coffee times.coffee 
    hello
    hello
    hello
    hello
    hello
    hello
    hello
    hello
    hello
    hello
    

    mixin

    _.mixin(object)

    mixinは、Underscoreにカスタマイズした関数を追加することができます。{関数名:関数} というオブジェクトを追加することで、Underscoreのオブジェクトとして定義できます。

    _ = require 'underscore'
    
    _.mixin {
     capitalize:(string)->
      string.charAt(0).toUpperCase() + string.substring(1).toLowerCase()
    }
    
    console.log _.capitalize "underscore"
    
    実行結果
    $ coffee mixin.coffee 
    Underscore
    

    uniqueId

    _.uniqueId([prefix])

    uniqueIdは、グローバル域で一意なIDを生成します。引数にプレフィックスを与えることができます。

    _ = require 'underscore'
    
    console.log _.uniqueId()
    console.log _.uniqueId()
    console.log _.uniqueId()
    console.log _.uniqueId()
    console.log _.uniqueId('CB')
    
    実行結果
    $ coffee uniqueId.coffee 
    0
    1
    2
    3
    CODE4
    

    escape

    _.escape(string)

    escapeは、HTMLなどの&, <, >, &quat;, ', / をエスケープ処理して返します。

    _ = require 'underscore'
    
    console.log _.escape '&,<,>,&quat;,\',/'
    
    実行結果
    $ coffee escape.coffee 
    &amp;,&lt;,&gt;,&quot;,&#x27;,&#x2F;
    

    result

    _.result(object, property)

    resultは、指定したobjectのプロパティを返します。プロパティが値の場合はそのまま値が、関数の場合、その関数を実行します。

    _ = require 'underscore'
    
    hoge = 
     foo: 'hello'
     bar: ()->
      'good morning'
    
    console.log _.result hoge, 'foo'
    console.log _.result hoge, 'bar'
    
    実行結果
    $ coffee result.coffee 
    hello
    good morning
    

    関連ページ