Changeset 32893 for lang/ruby

Show
Ignore:
Timestamp:
04/28/09 16:34:07 (4 years ago)
Author:
isaisstillalive
Message:
  • 引き回すのをはてなidのみに変更
Location:
lang/ruby/hatena_binding
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/hatena_binding/lib/hatena/haiku/user.rb

    r32891 r32893  
    77      attr_reader :id 
    88       
    9       def initialize user 
    10         @user = user 
    11         @id = @user.id 
     9      attr_reader :name 
     10       
     11      def initialize id 
     12        if id.kind_of? Hash 
     13          init_properties id 
     14          id = id["id"] 
     15        end 
     16        @id = id.dup.freeze 
    1217      end 
    1318       
    1419      def entries 
    15         Entries.new "/statuses/user_timeline/#{@user.id}.json?" 
     20        Entries.new "/statuses/user_timeline/#{@id}.json?" 
    1621      end 
    1722       
    1823      def following 
    19         @following = Following.new self unless @following 
     24        @following = Following.new @id unless @following 
    2025        @following 
     26      end 
     27       
     28      private 
     29      def init_properties user 
     30        @name = user["name"].dup.freeze 
    2131      end 
    2232    end 
  • lang/ruby/hatena_binding/lib/hatena/haiku/user/following.rb

    r32891 r32893  
    33    class User 
    44      class Following 
    5         def initialize user 
    6           @user = user 
     5        def initialize id 
     6          @id = id.dup.freeze 
    77        end 
    88         
    99        def entries 
    10           Entries.new "/statuses/friends_timeline/#{@user.id}.json?" 
     10          Entries.new "/statuses/friends_timeline/#{@id}.json?" 
    1111        end 
    1212      end 
  • lang/ruby/hatena_binding/test/hatena/haiku/test_user.rb

    r32891 r32893  
    44class TestHatenaUser < Test::Unit::TestCase 
    55  def setup 
    6     @id = "hatena_user_id" 
    7     @hatena_user = Hatena::User.new @id 
    8     @user = Hatena::Haiku::User.new @hatena_user 
     6    @id = "hid" 
     7    @user = Hatena::Haiku::User.new @id 
    98  end 
    109   
     10  # Followingクラスがautoloadされるか 
    1111  def test_following 
     12    assert Hatena::Haiku::User.autoload?(:Following) 
    1213    assert_kind_of Class, Hatena::Haiku::User::Following 
    1314  end 
    1415   
    15   def test_user_should_have_hatena_user 
    16     assert_same @hatena_user, @user.__send__(:instance_variable_get, :@user) 
     16  # id文字列で初期化した場合、その文字列をフリーズして保持する 
     17  def test_initialize_with_id_should_keep_frozen_id 
     18    id = "hid" 
     19    user = Hatena::Haiku::User.new id 
     20     
     21    assert_not_same id, user.id 
     22    assert_equal    id, user.id 
     23    assert              user.id.frozen? 
    1724  end 
    1825   
    19   def test_id_should_return_hatena_user_id 
    20     assert_same @hatena_user.id, @user.id 
     26  # userハッシュで初期化した場合、そのハッシュのidを保持し、それ以外はinit_propertiesメソッドに任せる 
     27  def test_initialize_with_hash_should_frozen_id_and_call_init_properties 
     28    $called_args = [] 
     29    user_class = Class.new(Hatena::Haiku::User) do 
     30      def init_properties hash 
     31        $called_args << [:init_properties, hash] 
     32      end 
     33    end 
     34     
     35    hash = {"id" => "hid"} 
     36    user = user_class.new hash 
     37     
     38    assert_not_same hash["id"], user.id 
     39    assert_equal    hash["id"], user.id 
     40    assert                      user.id.frozen? 
     41     
     42    assert_equal 1, $called_args.size 
     43    assert_equal :init_properties, $called_args[0][0] 
     44    assert_equal hash, $called_args[0][1] 
    2145  end 
    2246   
     47  # init_propertiesメソッドは、プロパティを設定する 
     48  def test_init_properties_should_set_properies 
     49    hash = { 
     50      "id"   => "hid", 
     51      "name" => "hname", 
     52    } 
     53    @user.__send__(:init_properties, hash) 
     54     
     55    assert_not_same hash["name"], @user.name 
     56    assert_equal    hash["name"], @user.name 
     57    assert                        @user.name.frozen? 
     58  end 
     59   
     60  # entriesメソッドはEntriesインスタンスを返すこと 
    2361  def test_entries_should_return_entries_class 
    2462    entries = @user.entries 
     
    2664  end 
    2765   
    28   def test_entries_should_set_user_timeline 
     66  # entriesメソッドはユーザIDを含んだパスを設定すること 
     67  def test_entries_should_set_path_user_timeline 
    2968    entries = @user.entries 
    30     assert_equal "/statuses/user_timeline/hatena_user_id.json?", entries.__send__(:instance_variable_get, :@path) 
     69    assert_equal "/statuses/user_timeline/#{@id}.json?", entries.__send__(:instance_variable_get, :@path) 
    3170  end 
    3271   
     72  # followingメソッドはFollowingインスタンスを返すこと 
    3373  def test_following_should_return_following_class 
    3474    following = @user.following 
     
    3676  end 
    3777   
    38   def test_following_should_set_user 
     78  # followingメソッドはユーザIDを設定すること 
     79  def test_following_should_set_user_id 
    3980    following = @user.following 
    40     assert_same @user, following.__send__(:instance_variable_get, :@user) 
     81    assert_equal @user.id, following.__send__(:instance_variable_get, :@id) 
    4182  end 
    4283end 
  • lang/ruby/hatena_binding/test/hatena/haiku/user/test_following.rb

    r32891 r32893  
    44class TestHatenaUserFollowing < Test::Unit::TestCase 
    55  def setup 
    6     @id = "hatena_user_id" 
    7     @hatena_user = Hatena::User.new @id 
    8     @user = Hatena::Haiku::User.new @hatena_user 
    9     @following = Hatena::Haiku::User::Following.new(@user) 
     6    @id = "hid" 
     7    @following = Hatena::Haiku::User::Following.new @id 
    108  end 
    119   
    12   def test_following_should_have_user 
    13     assert_same @user, @following.__send__(:instance_variable_get, :@user) 
     10  # id文字列で初期化した場合、その文字列をフリーズして保持する 
     11  def test_initialize_with_id_should_keep_frozen_id 
     12    id = @following.__send__(:instance_variable_get, :@id) 
     13     
     14    assert_not_same @id, id 
     15    assert_equal    @id, id 
     16    assert               id.frozen? 
    1417  end 
    1518   
     19  # entriesメソッドはEntriesインスタンスを返すこと 
    1620  def test_entries_should_return_entries_class 
    1721    entries = @following.entries 
     
    1923  end 
    2024   
    21   def test_entries_should_return_entries_class 
     25  # entriesメソッドはユーザIDを含んだパスを設定すること 
     26  def test_entries_should_set_path_friends_timeline 
    2227    entries = @following.entries 
    23     assert_equal "/statuses/friends_timeline/hatena_user_id.json?", entries.__send__(:instance_variable_get, :@path) 
     28    assert_equal "/statuses/friends_timeline/#{@id}.json?", entries.__send__(:instance_variable_get, :@path) 
    2429  end 
    2530end