40歳からのキャリアチェンジ

20代はエンジニア・PM、30代はWeb系エンジニア向けのキャリアアドバイザー。40代の今はフリーランスで開発含めて色々やってます。技術ネタとしてはRuby/RailsとJavaScript関連あたり

043-MVCのControllerの役割を見直すその2

今までEntryクラスでエントリ一覧のキャッシュの保存と読み出しをこんな感じするようにしていました

Entry = function(){//略};
Entry.prototype = {
  saveCache:function(json,page_number){
    /*
     ブロガー名+現在のページ数.jsonというファイル名で
     ローカルにキャッシュ
     */

    var fileName = this.blogger + page_number + ".json";
    var file_path = Titanium.Filesystem.resourcesDirectory
	+ '/cache/'
	+ fileName;
    var file  = Titanium.Filesystem.getFile(file_path);
    file.write(json);
    return true;
  },
  loadCache:function(filename){
    var file  = Titanium.Filesystem.getFile(filename);
    var json = file.read();
    var data = JSON.parse(json);
    return data;

  },
};

キャッシュする時にブロガーの名前や現在のページ数を引数に与える必要があるため、Controllerにこの処理を移すべきか、キャッシュを管理するモデルとしてCacheクラスを作って、ControllerからこのCacheとEntryを管理するべきなのかちょっと迷ってます。

今のところは、Controllerに実装する方針にしてひとまずこんな感じにテストコードを修正しました

//test_controller_entry.js
Ti.include('controller/entry.js');

module("Controller");
    var entry_list = {
      "0":{
	"permalink":"http://blog.pasonatech.co.jp/hibi/299/1814.html",
	"title":"\uff0b\u30de\u30de\uff0bSOHO\uff1a\u521d\u30a8\u30f3\u30c8\u30ea\u30fc",
	"html_body":"</p>testtest<p>",
	"_id":"4d57ba74e93ac1e360000001",
	"blogger":"hibi"
      },
      "1":{
	"permalink":"http://blog.pasonatech.co.jp/hibi/299/1814.html",
	"title":"\u30a8\u30f3\u30c8\u30ea\u30fc",
	"html_body":"<p>u305b\u3066\u9802\u304d\u307e\u3059\uff01</p>\n\n<p>",
	"_id":"4d57ba74e93ac1e360000001",
	"blogger":"hibi"
      },
      "2":{
	"permalink":"http://blog.pasonatech.co.jp/hibi/299/1814.html",
	"title":"IT\u7cfb\u30d9\u30f3\u30c1\u30e3\u30fc\uff0b\u30de\u30de\uff0bSOHO\uff1a\u521d\u30a8\u30f3\u30c8\u30ea\u30fc",
	"html_body":"<p>\u7b46\u3055\u305b\u3066\u9802\u304d\u307e\u3059\uff01</p>\n\n<p>",
	"_id":"4d57ba74e93ac1e360000001",
	"blogger":"hibi"
      },
      "3":{
	"permalink":"http://blog.pasonatech.co.jp/hibi/299/1814.html",
	"title":"\u30a8\u30f3\u30c8\u30ea\u30fc",
	"html_body":"<p>\u306f\u3058\u3081\u307e\u3057\u3066\u3002<br>\n\u672c\u65e5\u304b\u3089\u57f7\u7b46\u3055\u305b\u3066\u9802\u304d\u307e\u3059\uff01</p>\n\n<p>",
	"_id":"4d57ba74e93ac1e360000001",
	"blogger":"hibi"
      },
      "4":{
	"permalink":"http://blog.pasonatech.co.jp/hibi/299/1814.html",
	"title":"IT",
	"html_body":"<p>\u306f\u3058\u3081\u307e\u3057\u3066\u3002<br>",
	"_id":"4d57ba74e93ac1e360000001",
	"blogger":"hibi"
      }

    };


test("#2 should be handle",function(){//略});
test("#4 page should be handle",function(){//略});
test("handle Entry List",function(){//略});
test("has Cached??",function(){//略});
test("load Entry cache",
  function(){
    var c = new Controller('hibi');
    c.init();
    // ダミーとなるエントリ一覧を5ページ分生成。
    // cache/hibi1.json,cache/hibi2・・となる
    var entries1 = c.receive(entry_list,1);
    var entries2 = c.receive(entry_list,2);
    var entries3 = c.receive(entry_list,3);
    var entries4 = c.receive(entry_list,4);
    var entries5 = c.receive(entry_list,5);
    var result = c.loadCache();
    //
    equals(result.length,20);
  }
);
テストケース自体の誤りがないように、本質的な処理にはなってないけどこんな形で仮実装。
>|javascript|
Titanium.include(Titanium.App.appURLToPath("app://model/entry.js"));
Titanium.include(Titanium.App.appURLToPath("app://view/styles.js"));
Titanium.include(Titanium.App.appURLToPath("app://view/create_table_view.js"));

Controller = function(blogger){
  this.blogger = blogger;
  this.table_view = new CreateTableView();
  this.entry = new Entry(this.blogger);
  this.page_number = 1; // スタートページとして初期値で1を設定
  this.entry_cache = [];
};
Controller.prototype = {

  loadCache:function(){
     /*
      * 現在のページを取得して、そのページを1つづつさかのぼって
      * 1ページ目にたどりつくまでループ。
      * ループ中でエントリ情報をキャッシュから読み込み、union()
      * で結合して最終的にthis.entry_cacheに代入
      */
    //var current_page = this.getCurrentPage();
    var current_page = 5;
    var first_page = 1;
    while(first_page < current_page){
      this.entry_cache.push(current_page + ":0");
      this.entry_cache.push(current_page + ":1");
      this.entry_cache.push(current_page + ":2");
      this.entry_cache.push(current_page + ":3");
      this.entry_cache.push(current_page + ":4");
      current_page = current_page -1;

    }
    return this.entry_cache;

上記実行すると

[INFO] << << <

となったので、テストコード自体はこれでOK。明日にしっかりと実装しようと思います。(その前にgit commit忘れていたので、それをやらないと。)