XCode6で Bundle Identifier の名前とかを変更する

Bundle Identifierの値は、素直な感じに変更できない。

Unkoとかのままにしたくない。

Product Nameの Unkoを Lazy に変更する。

そうすると、Bundle Identifier が変更される。

ただ、まだUnkoにまみれてる。

右上の、xcodeproje ファイルの名前を変更すると、以下のように他変更する提案をしてきてきて、素敵。

いうても、まだ Unko いっぱいいるので、手動で Lazyに全部何も考えずrenameしていく。
UnkoTests.swift のファイル名だけではなく、中身のクラス名もrenameするのわすれずに。
だいたい、画面からはUnkoがきえた。

ファイルシステムから、調べる。うようよいる。置換をコンソールからガンガンやってみる。

バイナリーのファイル以外は、全部なおした。バイナリーのは、まぁ、適当にほっとく。

起動成功した!

これで、気兼ねなく、適当に名前をつけて、後で変更できるね。
自己責任だけどね!

Cacheクラス

前回作った、configクラスを使いつつ、シングルトン化し、
ブロックの結果をキャッスする関数を生やしたかんじ。

ソース

require 'singleton'
require 'memcache'
require 'bei/config'
require 'pp'

module Bei
  class Cache < Memcache
  include Singleton

  def initialize
    super ( { :servers => Bei::Config.instance.get(:memcached_servers) } )
  end


  def cacheable(key,expire=60*3)
    raise "NO_BLOCK_ERROR" unless block_given?

    unless( value = self.get(key) )
      #pp 'from value'
      value = yield()
      self.set(key,value,expire)
    else
      #pp 'from cache'
    end

    return value
  end

  end

使い方

こんな
かん

 cache = Bei::Cache.instance() 
# Memcache の継承なので、既にある関数はそのまま使える。
 cache.set('hi','hihihi')
 hi = cache.get('hi')

 # blockからデータ取得
 my_data = cache.cachable('my/3/data', 60 * 1) { { :user_id => 3 } } # 3 

 # memcachedからデータ取得
 my_data = cache.cachable('my/3/data', 60 * 1) { { :user_id => 2 } } # 3 (2じゃない)
 

自分用のConfigクラス

railsでどうすればいいのかわからなかったので、取り急ぎ作った。

require 'singleton'

module Bei
  class Config
    include Singleton

    def initialize
      file  = Rails.root.to_s + '/config/environments/config_' + ENV['RAILS_ENV'] + '.rb'
      @config = eval ( open(file).read )
    end

    def get(key)
      return Marshal::load(Marshal.dump(@config[key])) #deep clone
    end

  end 
end

config/environments/config_test.rb

{

  :twitter_consumer => {
    :key => 'foo',
    :secrete => 'bar'
  }
    
}  

使い方。

   config = Bei::Config.instance().get(:twitter_consumer)

validationエラーをif分等で対応するのがだるいのでraiseで対応した

check関数を、modelに実装

checkを呼ぶと、validationがうまく行ってない場合、raiseし死んじゃうようにした。

module Bei
  require 'pp'
  require 'bei/exception'
  class ActiveRecord < ActiveRecord::Base
    self.abstract_class = true

    def check
      if self.invalid?
        e = Bei::Exception::Validation.new()
        e.errors = self.errors
        raise e
      end

    end

    def check_and_save
      self.check
      self.save
    end

  end
end

Exceptionクラス

Exceptionをちょっと継承した感じ。

module Bei
  class Exception < ::Exception

    class Validation < ::Exception
      attr_accessor :errors
    end

  end
end

Controllerのベースクラスを拡張

エラーをキャッチして、いい感じに、自動でJSONで出力。今回はAPI用に作ってるので、HTMLは考慮してない。

class ApplicationController < ActionController::Base
  require 'pp'
  require 'bei/exception'

  rescue_from Bei::Exception, :with => :catch_exception
  rescue_from Bei::Exception::Validation , :with => :catch_validation_exception

  def render_json_ok(args)
    render :json => { 'error' => 0 , 'data' => args }
  end

  def render_json_error(code,args)
    render :json => { 'error' => 1,'error_code' => code , 'data' => args }, :status => 400
  end

  private


  def catch_exception(e)
    code =  e.message
    render_json_error(code,{})
  end

  def catch_validation_exception(e)
    # TODO errors.messages の最適化
    render_json_error('VALIDATION_ERROR',e.errors.messages)
  end

end


これで、validation毎に、if条件分を書かなくてすむので楽チンなはず!