はじめに
NotionRubyMapping 解説の第9回目です。これまで RichText object のサブクラスをいくつか紹介してきました。これらをまとめて取り扱うことができるものが、今日紹介する RichTextArray になります。基本的にほとんどのテキストを取り扱うブロックは、すべて RichTextArray を要素に持ちます。今日は RichTextArray の作成方法などだけの解説なので、ページは特に用意しませんでした。
RichTextArray の作成
RichTextArray には通常のコンストラクタである new があるのですが、これを直接使うことはなく、rich_text_array というコンストラクタを用います。使い方は以下のようになります。
RichTextArray.rich_text_array key, text_info
text_info
の部分は以下のようになります。
- a String like as
text
(String) - an Array of Strings (Array of Strings)
- a RichTextObject (RichTextObject)
- an Array of RichTextObjects (Array of RichTextObjects)
- a RichTextArray (RichTextArray)
順番に確認してみようと思います。まず、text_info を設定しなかった場合は、空の RichTextArray が生成されます。
irb(main):003> nullString = RichTextArray.rich_text_array "title"; nullString => #<NotionRubyMapping::RichTextArray:0x00000001137d8480 @key="title", @rich_text_objects=[], @will_update=false>
単に文字列だけを渡した場合、TextObject が自動的に設定されます。
irb(main):005> aString = RichTextArray.rich_text_array "title", "A string"; aStr ing => #<NotionRubyMapping::RichTextArray:0x0000000113cf2038 @key="title", @rich_text_objects= [#<NotionRubyMapping::TextObject:0x0000000113d31738 @options={"plain_text"=>"A string"}, @text="A string", @type="text", @will_update=false>], @will_update=false>
文字列の Array を渡すとそれぞれを TextObject に変換したものが設定されます。例で示したようにとりあえず TextObject を複数設定した上で、特定の場所だけ option を変更するような使い方をするためです。
irb(main):006> twoStrings = RichTextArray.rich_text_array "title", %W[ABC\\n DEF ] irb(main):007> twoStrings[0].bold = true => true irb(main):008> twoStrings => #<NotionRubyMapping::RichTextArray:0x0000000113a53318 @key="title", @rich_text_objects= [#<NotionRubyMapping::TextObject:0x0000000113555820 @options={"plain_text"=>"ABC\\n", "bold"=>true}, @text="ABC\\n", @type="text", @will_update=true>, #<NotionRubyMapping::TextObject:0x00000001135557d0 @options={"plain_text"=>"DEF"}, @text="DEF", @type="text", @will_update=false>], @will_update=false>
TextObject を渡すこともできます。事前に option が設定された TextObject を登録したい場合などに使います。
irb(main):012> aTextObject = RichTextArray.rich_text_array "title", TextObject.n ew("A TextObject", "italic" => true); aTextObject => #<NotionRubyMapping::RichTextArray:0x00000001139f8260 @key="title", @rich_text_objects= [#<NotionRubyMapping::TextObject:0x0000000113734bf0 @options={"plain_text"=>"A TextObject", "italic"=>true}, @text="A TextObject", @type="text", @will_update=false>], @will_update=false>
RichTextObject を複数格納した配列を入れることも可能です。
irb(main):014> textMentionObjects = RichTextArray.rich_text_array "title", [Text Object.new("A TextObject", "italic" => true), MentionObject.new(user_id: "ABC")] ; textMentionObjects => #<NotionRubyMapping::RichTextArray:0x00000001139fb938 @key="title", @rich_text_objects= [#<NotionRubyMapping::TextObject:0x0000000113592680 @options={"plain_text"=>"A TextObject", "italic"=>true}, @text="A TextObject", @type="text", @will_update=false>, #<NotionRubyMapping::MentionObject:0x00000001139fb988 @options={:user_id=>"ABC"}, @type="mention">], @will_update=false>
既存の RichTextArray object を渡した場合には、新規に RichTextArray を作るのではなく、渡されたものをそのまま返します。textMentionObjects と richTextArray は同じオブジェクトになっていることがわかります。
irb(main):016> richTextArray = RichTextArray.rich_text_array "title", textMentio nObjects; richTextArray => #<NotionRubyMapping::RichTextArray:0x00000001139fb938 @key="title", @rich_text_objects= [#<NotionRubyMapping::TextObject:0x0000000113592680 @options={"plain_text"=>"A TextObject", "italic"=>true}, @text="A TextObject", @type="text", @will_update=false>, #<NotionRubyMapping::MentionObject:0x00000001139fb988 @options={:user_id=>"ABC"}, @type="mention">], @will_update=false> irb(main):017> textMentionObjects === richTextArray => true
その他のメソッド
[index]: 該当する index の RichTextObject を取得
すでに上で使っていましたが、[] は内包する @rich_text_objects
への delegate になっています。このため、該当する RichTextObject が取得できます。
irb(main):018> richTextArray[0] => #<NotionRubyMapping::TextObject:0x0000000113592680 @options={"plain_text"=>"A TextObject", "italic"=>true}, @text="A TextObject", @type="text", @will_update=false>
<< String または << RichTextObject : 末尾に RichTextObject を追加
末尾に Rich Text を追加します。文字列が渡された時には、TextObject に変換されて登録されます。RichTextArray が変更されたので、 @will_update
が true になっています。
irb(main):019> richTextArray << "add String" => #<NotionRubyMapping::TextObject:0x000000011373ef10 @options={"plain_text"=>"add String"}, @text="add String", @type="text", @will_update=false> irb(main):020> richTextArray => #<NotionRubyMapping::RichTextArray:0x00000001139fb938 @key="title", @rich_text_objects= [#<NotionRubyMapping::TextObject:0x0000000113592680 @options={"plain_text"=>"A TextObject", "italic"=>true}, @text="A TextObject", @type="text", @will_update=false>, #<NotionRubyMapping::MentionObject:0x00000001139fb988 @options={:user_id=>"ABC"}, @type="mention">, #<NotionRubyMapping::TextObject:0x000000011373ef10 @options={"plain_text"=>"add String"}, @text="add String", @type="text", @will_update=false>], @will_update=true>
delete_at(index): index の場所の RichTextObject を削除
これも @rich_text_objects
への delegate です。index の位置の RichTextObject を削除します。上で追加したものを消してみましょう。
irb(main):021> richTextArray.delete_at 2 => #<NotionRubyMapping::TextObject:0x000000011373ef10 @options={"plain_text"=>"add String"}, @text="add String", @type="text", @will_update=false> irb(main):022> richTextArray => #<NotionRubyMapping::RichTextArray:0x00000001139fb938 @key="title", @rich_text_objects= [#<NotionRubyMapping::TextObject:0x0000000113592680 @options={"plain_text"=>"A TextObject", "italic"=>true}, @text="A TextObject", @type="text", @will_update=false>, #<NotionRubyMapping::MentionObject:0x00000001139fb988 @options={:user_id=>"ABC"}, @type="mention">], @will_update=true>
full_text: テキストのみを取得
irb(main):024> twoStrings.full_text => "ABC\\nDEF"
each: RichTextObject の繰り返し
each も @rich_text_objects
を繰り返す処理が行われます。each が実装されているので、自動的に派生するメソッドも使えるようになります。ここでは、map を適用しています。同様に count も動作します。
irb(main):028> twoStrings.map(&:text) => ["ABC\\n", "DEF"] irb(main):029> twoStrings.count => 2
rich_text_objects = text_info : テキスト情報の置き換え
rich_text_objects
に代入するとテキスト情報を丸ごと置き換えます。この text_info は上記コンストラクタと同じで文字列・文字配列・RichTextObject・RichTextObject配列などが利用可能です。
irb(main):030> richTextArray.rich_text_objects = "only one" => "only one" irb(main):031> richTextArray => #<NotionRubyMapping::RichTextArray:0x00000001139fb938 @key="title", @rich_text_objects= [#<NotionRubyMapping::TextObject:0x0000000110b842a8 @options={"plain_text"=>"only one"}, @text="only one", @type="text", @will_update=false>], @will_update=true>
おわりに
RichTextArray は JSON の記述が非常に面倒なのですが、RichTextArray はその辺りをほとんど気にしないでいいように作成しています。今後もたくさん出現するので忘れたら下のマニュアルを見ていただければと思います。
RichTextArray のマニュアルはこちら。