GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Fork of deimos1986/book_mdar
Description: Life On The Edge With Merb, DataMapper & RSpec
Homepage: http://blog.new-bamboo.co.uk
Clone URL: git://github.com/kwatch/book_mdar.git
Translation finished of 4-1-1_Models.
kwatch (author)
Fri Sep 26 21:38:09 -0700 2008
commit  ca67fbcc78dc7a720d02406205272de4e3dac83b
tree    fb6abef0670b2461f91c948ed70b20047adab37c
parent  cb47573c49f86d03a6298d5856cf21125f7a3ba1
...
815
816
817
818
 
819
820
821
...
823
824
825
 
 
 
 
826
827
828
...
845
846
847
 
 
 
 
 
848
849
850
851
852
853
 
 
 
 
854
855
856
...
858
859
860
 
 
 
 
 
861
862
863
 
 
864
865
866
...
871
872
873
 
 
 
 
 
 
874
875
 
 
876
877
878
...
881
882
883
884
 
 
 
 
 
885
886
 
887
888
889
890
891
 
 
 
 
892
893
894
...
896
897
898
 
899
900
901
...
903
904
905
 
906
907
908
...
914
915
916
 
917
918
919
920
921
922
923
 
924
925
926
...
930
931
932
 
 
 
 
 
 
933
934
935
936
937
938
 
 
 
 
 
 
 
939
940
941
942
943
 
 
944
945
946
 
 
 
 
947
948
949
...
951
952
953
 
 
 
 
 
954
955
956
...
961
962
963
964
 
965
966
967
...
969
970
971
 
 
 
 
 
972
973
974
...
976
977
978
979
 
 
 
 
 
980
981
982
983
 
 
 
984
985
986
...
989
990
991
 
 
 
992
 
 
 
...
815
816
817
 
818
819
820
821
...
823
824
825
826
827
828
829
830
831
832
...
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
...
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
...
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
...
909
910
911
 
912
913
914
915
916
917
 
918
919
920
921
922
923
924
925
926
927
928
929
930
...
932
933
934
935
936
937
938
...
940
941
942
943
944
945
946
...
952
953
954
955
956
957
958
959
960
961
 
962
963
964
965
...
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
 
1000
1001
1002
1003
1004
1005
1006
...
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
...
1023
1024
1025
 
1026
1027
1028
1029
...
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
...
1043
1044
1045
 
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
...
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
0
@@ -815,7 +815,7 @@ It doesn't delete columns, and it can't detect renaming them.
0
 ただし、いくつか制限があります。
0
 カラムは削除しませんし、カラム名の変更も検出できません。
0
 
0
-#### Migration Files
0
+#### Migration Files (マイグレーションファイル)
0
 
0
 Whilst the preceding commands and tasks can keep the database schema in
0
 perfect sync with the models, they can also wipe out any data you might have in
0
@@ -823,6 +823,10 @@ the database, or fail to remove columns which are no longer needed. To avoid
0
 this, AR style migrations are also supported. These are stored in `schema/migrations`
0
 and are ruby files.
0
 
0
+先ほど紹介したコマンドとタスクは、データベーススキーマとモデルとを完全に同期させるものでしたが、データベース中のデータをすべて消し去ってしまったり、もう必要のなくなったカラムが削除されないという問題がありました。
0
+これを避けるために、AR スタイルのマイグレーションもサポートされています。
0
+これらは Ruby ファイルであり、`schema/migrations` に格納されます。
0
+
0
     migration(1, :add_homepage_to_comments ) do
0
       up do
0
         modify_table :comments do
0
@@ -845,12 +849,21 @@ applied. This number doesn't have to be unique, although a migration mustn't
0
 have a higher number than a migration it depends on. You shouldn't define
0
 modifications to a table to happen before that table is made, for example.
0
 
0
+ファイルの最初の行は、マイグレーションを一意に特定するためのものであり、2 つのコンポーネントから構成されています。
0
+〔訳注: 2 つのうち〕より重要なのは `:add_homepage_to_comments` という名前のほうであり、これはデータベースに対して適用されたすべてのマイグレーションにおいて一意である必要があります。
0
+数字のほうは一意である必要はありませんが、そのマイグレーションが依存する別のマイグレーションより高い〔訳注: 小さい〕値であってはいけません。
0
+たとえば、あるテーブルに対する修正を、そのテーブルが作成されるより前に定義することはできません。
0
+
0
 The `up` and `down` blocks describe the actual behaviour of the migration. `up`
0
 is what happens when the migration is applied, and `down` happens when the
0
 migration is 'undone'. This might not mean undone in the literal sense - if you
0
 migrate to remove a column, and add it back in the `down` migration, while the
0
 column will be there, all the data will be lost.
0
 
0
+`up` と `down` のブロックには、マイグレーションの実際の動作を記述します。
0
+`up` のほうはマイグレーションが適用されるときに実行され、`down` のほうはマイグレーションを 「元に戻す」ときに実行されます。
0
+これは、文字通りの意味で元に戻されるわけではありません - もしカラムを削除するようなマイグレーションを作成し、かつそれを追加し直すような `down` マイグレーションを作成した場合、〔訳注: `down` マイグレーションで元に戻そうとしても〕そのカラムのデータは失われてしまうでしょう。
0
+
0
 In this case, as the name suggested, we add a homepage column to comments.
0
 It's specified much like a property (and should match up with the relevant
0
 property in the model.rb file). It also takes many of the same options -
0
@@ -858,9 +871,16 @@ essentially all those which are just database features: `:length`, `:nullable`
0
 are valid, but `:private`, which is a pure ruby option is not allowed. The
0
 `down` migration is the opposite - it removes the column.
0
 
0
+今回の場合、名前を見ればわかりますが、comments テーブルに homepage カラムを追加しています。
0
+これはプロパティによく似た感じで指定されます (また model.rb ファイルの中で関係のあるプロパティと一致しなければいけません)。
0
+カラムの追加にはたくさんの〔訳注: プロパティと〕同じオプションを指定可能です - 本質的にはデータベースの機能であるものすべてが使用可能です: たとえば `:length` と `:nullable` が使用可能ですが、純粋に Ruby のオプションである `:private` は使用できません。
0
+`down` マイグレーションは逆であり、カラムを削除します。
0
+
0
 To apply the migrations, there are a couple of rake tasks available through
0
 merb_datamapper
0
 
0
+マイグレーションを適用するために、merb_datamapper によって 2 つの Rake タスクが用意されてます。
0
+
0
     rake dm:db:migrate:up # migrates the database up
0
     rake dm:db:migrate:down # migrates the database down
0
 
0
@@ -871,8 +891,16 @@ and down migrations, the version determines the highest order that will be
0
 reflected in the table, either by applying `up` migrations until the level is
0
 complete or applying all the `down` migrations greater than the given level.
0
 
0
+これらはすべてのマイグレーションを順に適用するか、削除します。
0
+すべての up マイグレーション (または down マイグレーション) を適用したくはない場合もあるでしょう。
0
+その場合、〔訳注: Rake コマンドに〕`VERSION=2` を追加するか、`rake dm:db:migrate:up[2]` のようにしてタスクを起動するかしてください。
0
+up と down のマイグレーションの両方において、テーブルに対して反映されるいちばん高いレベル番号が、バージョンによって決定されます〔訳注: ???〕。
0
+`up` マイグレーションの場合はそのレベルに達するまで、また `down` マイグレーションの場合は指定されたレベルより大きいマイグレーションが、すべて適用されます。
0
+
0
 There are a couple of generators to make migrations
0
 
0
+マイグレーションを作成するためのジェネレータが 2 つ用意されています。
0
+
0
     merb-gen migration name_of_migration # an empty migration
0
     merb-gen resource_migration Post # a migration for the post class
0
 
0
@@ -881,14 +909,22 @@ The first creates an empty migration stub with the name defined and an `up` and
0
 does it's best to construct the appropriate migration from the properties of the
0
 model. It currently doesn't generate anything to do with relationships, however.
0
 
0
-### Other Misc Things
0
+最初のほうは、マイグレーションのスタブを `up` と `down` のブロックが空の状態で作成し、それに指定された名前をつけます。
0
+2 番目のほうは、該当するクラスを app/models から読み込み、そのモデルのプロパティから適切なマイグレーションを構築するよう最大限努力します。
0
+ただし現在のところ、関連づけについては何も生成してくれません。
0
+
0
+### Other Misc Things (その他の雑多なこと)
0
 
0
-#### Callbacks
0
+#### Callbacks (コールバック)
0
 
0
 Callbacks in DataMapper > 0.9 are very powerful. In any DataMapper::Resource
0
 you can set before and after callbacks on any instance/class method. There are
0
 a couple of different ways to define callbacks:
0
 
0
+DataMapper 0.9 以降では、コールバックが非常に強力です。
0
+どの DataMapper::Resource のどのインスタンスメソッド/クラスメソッドに対しても、呼び出し前と後のコールバックを設定することが可能です。
0
+コールバックを設定する方法は 2 つあります:
0
+
0
     class Post
0
       include DataMapper::Resource
0
 
0
@@ -896,6 +932,7 @@ a couple of different ways to define callbacks:
0
       property :title, String, :length => 200
0
 
0
       # before save call the instance method make_permalink
0
+ # save メソッドを呼び出す前に、インスタンスメソッド make_permalink を呼び出す
0
       before :save, :make_permalink
0
 
0
       def make_permalink
0
@@ -903,6 +940,7 @@ a couple of different ways to define callbacks:
0
       end
0
 
0
       #callbacks can be defined for any method
0
+ # コールバックはどのメソッドに対しても定義できる
0
       after :publish, :send_message
0
 
0
       def publish
0
@@ -914,13 +952,14 @@ a couple of different ways to define callbacks:
0
       end
0
 
0
       # defining a callback on a class method, passing in a block to run before its created.
0
+ # 作成前に実行されるブロックを渡すことで、コールバックをクラスメソッドに定義する。
0
       before_class_method :create do
0
         # do something before a record is created
0
       end
0
 
0
     end
0
 
0
-#### Bulk Operations
0
+#### Bulk Operations (大量のデータ操作)
0
 
0
 Sometimes, you have to operate on a large number of records at once, to do
0
 exactly the same thing to each of them. The example earlier for deleting old
0
@@ -930,20 +969,38 @@ could just go `Comments.all(:date.lt => Date.today - 20).destroy!` and it would
0
 produce an appropriate query to do it in one operation and without loading all
0
 those posts which are about to be deleted?
0
 
0
+ときどき、一度に大量のレコードに対して、まったく同じ操作をしなければならない場合もあります。
0
+先の例では、古い投稿を each で消していました。
0
+この場合、`SELECT` 文を何回かと、大量の `DELETE` 文が実行されました。
0
+この `DELETE` 文は、データベースの大きさによっては数百回実行される可能性が潜在的にあります。
0
+もし `Comments.all(:date.lt => Date.today - 20).destroy!` みたいなことができて、それが適切なクエリーが発行されることによって 1 回の操作で済み、かつ削除すべきデータをすべて読み込むようなことを避けるようになってくれてたらいいと思いませんか?
0
+
0
 Well, that's what happens. `Collection`s are 'lazily evaluated', which is to
0
 say, they don't do anything until they've been 'kicked'. `.each`, mentioned
0
 earlier, is a kicker method. It issues a `SELECT` appropriate to the conditions.
0
 `.destroy!` is another one, except it issues a `DELETE`. The other bulk method is
0
 `update!`, which looks like (example taken from the DataMapper source)
0
 
0
+ええ、その通りに動作します。
0
+`Collection`s は「遅延評価」されます。
0
+これはいうなれば、「開始する (kick)」までは〔訳注: データベースに対して〕何もしないということです。
0
+前に出てきた `.each` は「開始する」メソッドであり、条件に対して適切な `SELECT` 文を発行します。
0
+`.destroy!` は別の「開始する」メソッドであり、〔訳注: `SELECT` 文のかわりに〕`DELETE` 文を発行すること以外は同じです。
0
+〔訳注: こういったメソッドは bulk メソッドといい、そして〕他の bulk メソッドとしては `update!` があり、これは次のように使います (DataMapper のソースから持ってきた例です):
0
+
0
     Person.all(:age.gte => 21).update!(:allow_beer => true)
0
 
0
 This command would update the `allow_beer` attribute of all people aged 21 or
0
 older in the database, all in one `UPDATE` statement.
0
 
0
+このコマンドは、データベースにある 21 歳以上の人たち全員の `allow_beer` 属性を、1 回の `UPDATE` 分で更新します。
0
+
0
 Note: ActiveRecord has a well known `Model.delete_all` class method to erase all table entries. In DataMapper to delete all instances of an Object in the database, you would do `Model.all.destroy!`
0
 
0
-#### Aggregates
0
+注意: ActiveRecord は、よく知られているように `Model.delete_all` クラスメソッドがあり、これでテーブルのエントリをすべて消去することができます。
0
+DataMapper では、データベース中のオブジェクトのインスタンスをすべて削除するには `Model.all.destroy!` を使います。
0
+
0
+#### Aggregates (集計)
0
 
0
 DataMapper by default does not provide aggregator methods, but dm-aggregates
0
 in dm-more does. After adding `dependency "dm-aggregates"` to your merb `init.rb`
0
@@ -951,6 +1008,11 @@ file, your resource model will have aggregator methods including `count`, `min`,
0
 `max`, `avg`, and `sum`. You can pass conditions to any of these aggregator
0
 methods the same as Resource.first or Resource.all
0
 
0
+DataMapper は、デフォルトでは集計用メソッドを提供していません。
0
+しかし dm-more に含まれる dm-aggregates がそれらを提供してくれます。
0
+Merb の設定ファイル `init.rb` に `dependency "dm-aggregates"` を追加すると、モデルクラスで `count`, `min`, `max`, `avg`, `sum` という集計用メソッドが使えるようになります。
0
+これらの集計用メソッドには、Resource.first や Resource.all と同じように、条件を渡すことができます。
0
+
0
     Post.count :title.like => "%hello world%"
0
 
0
     # you can also do a count on an association:
0
@@ -961,7 +1023,7 @@ methods the same as Resource.first or Resource.all
0
     Post.sum(:comments_count)
0
 
0
 
0
-#### Each
0
+#### Each (Each メソッド)
0
 
0
 Each works like like expected iterating over a number of rows and you can pass
0
 a block to it. The difference between `Comments.all.each` and `Comments.each`
0
@@ -969,6 +1031,11 @@ is that instead of retrieving all the rows at once, each works in batches
0
 instantiating a few objects at a time and executing the block on them (so is less
0
 resource intensive). Each is similar to a finder as it can also take options:
0
 
0
+Each は、たくさんの行に対して繰り返しを行ないます〔訳注: "like like" ってなんじゃ???〕。
0
+また Each にブロックを渡すことができます。
0
+`Comments.all.each` と `Comments.each` の違いは、前者が一度にすべての行を取り出すのに対し、後者はオブジェクトを少しずつインスタンス化してブロックを実行するという点です (そのため後者のほうがリソースの消費が劇的に少なくて済みます)。
0
+Each はオプションを取ることができるので、finder に似ています:
0
+
0
     Comments.all.each(:date.lt => Date.today - 20).each do |c|
0
       c.destroy
0
     end
0
@@ -976,11 +1043,18 @@ resource intensive). Each is similar to a finder as it can also take options:
0
 NB: This isn't currently working in DataMapper. It instead fetches all the
0
 records. However, it will be reimplemented soon.
0
 
0
-#### Changing the Table Name
0
+NB: これは、まだ DataMapper では動作しません。
0
+かわりにすべてのレコードをフェッチします。
0
+しかしながら、まもなく再実装される予定です。
0
+
0
+#### Changing the Table Name (テーブル名の変更)
0
 
0
 You can set the name of the database table in your model if it is called
0
 something different by overriding a method in the class:
0
 
0
+モデルの中で、データベーステーブル名を設定することができます。
0
+そのためには、クラスの中でメソッドをオーバーライドします:
0
+
0
     def default_storage_name
0
       'list_of_posts'
0
     end
0
@@ -989,4 +1063,10 @@ This is only necessary if you are using an already existing database. If you
0
 have a lot of tables to rename, consider instead a `NamingConvention`, detailed
0
 later.
0
 
0
+これは、既存のデータベースを使う場合にのみ必要です。
0
+もし名前を変更しなければいけないテーブルがたくさんある場合は、次に説明する `NamingConvention` を使うことを検討してください。
0
+
0
 TODO: Write NamingConventions section.
0
+
0
+TODO: NamingConventions の章を書く。
0
+

Comments

    No one has commented yet.