はじめに
NotionRubyMapping 解説の第33回目です。今日はプロパティの追加です。昨日使ったデータベースを今日も使いますが、そもそもページを分ける意味もないことに気づきました。というわけで、今日も同じページで解説をします。今後は()の中の数字だけが変わっていく形になります。
そんなわけで、データベースの取得は毎日変わりません。昨日と URL が変わっているのは、同期ブロックを解除した時にデータベースが作り直されたためのようです。
irb(main):002> db = Database.find "https://www.notion.so/hkob/cad035c49e5346e78331cb5d05cbc754?v=a03e96ed79ad4d35808e7a08583396a0&pvs=4" => NotionRubyMapping::Database-cad035c49e5346e78331cb5d05cbc754
add_property
データベースのプロパティを追加するには、add_property メソッドを利用します。今日は、オプションがないプロパティのみを解説し、細かいオプションがあるプロパティは明日以降に解説します。オプションがないプロパティは簡単で、プロパティのクラスと付けたい名前を記述するだけです。ここでは、CheckboxProperty を追加してみました。
irb(main):003> db.add_property CheckboxProperty, "Check box" => #<NotionRubyMapping::CheckboxProperty:0x000000011e32c9f8 @base_type=:database, @create=false, @json={}, @name="Check box", @new_name=nil, @property_cache=PropertyCache, @property_id=nil, @query=nil, @remove=nil, @will_update=false> irb(main):004> db.save => NotionRubyMapping::Database-cad035c49e5346e78331cb5d05cbc754
実行すると以下のようになりました。
せっかくなので、オプションのないプロパティを一括で追加してみましょう。
irb(main):011* [CreatedByProperty, CreatedTimeProperty, DateProperty, EmailProperty, FilesProperty, irb(main):012* LastEditedByProperty, LastEditedTimeProperty, PeopleProperty, PhoneNumberProperty, irb(main):013* RichTextProperty, StatusProperty, UrlProperty].each do |klass| irb(main):014* db.add_property klass, klass.to_s.gsub(/NotionRubyMapping::/, '').gsub(/Property/, '') irb(main):015> end => [NotionRubyMapping::CreatedByProperty, NotionRubyMapping::CreatedTimeProperty, NotionRubyMapping::DateProperty, NotionRubyMapping::EmailProperty, NotionRubyMapping::FilesProperty, NotionRubyMapping::LastEditedByProperty, NotionRubyMapping::LastEditedTimeProperty, NotionRubyMapping::PeopleProperty, NotionRubyMapping::PhoneNumberProperty, NotionRubyMapping::RichTextProperty, NotionRubyMapping::StatusProperty, NotionRubyMapping::UrlProperty] irb(main):016> db.save => NotionRubyMapping::Database-cad035c49e5346e78331cb5d05cbc754
実行した結果、以下のようになりました。
おわりに
今回は、オプションのないプロパティの追加を解説しました。明日以降はオプションが存在するプロパティの追加を解説します。
add_property のマニュアルはこちらです。