TableView の実装(6) : hkob の雑記録 (461)

はじめに

hkob の雑記録の第461回目(通算34日目)は、TableView の Retrieve a view で Subtasks を設定してみます。

Subtasks のテスト

configuration の中に subtasks が存在します。View で細かく設定するよりは Subtasks というクラスを作成します。ただ、これは View の中でしか利用しないので、無駄にファイルを増やしても大変なので、View クラスの中に設定します。

        context "when subtasks" do
          let(:subtasks) { target.subtasks }

          it { expect(subtasks.class).to eq View::Subtasks }
          it { expect(subtasks.filter_scope).to eq "parents" }
        end

Subtasks の実装

まず、subtasks の取得部分を先に記載します。configuration の中の subtasks の中で

  class View
    # @param [String, nil] id
    # @param [String, nil] json
    def initialize(id: nil, json: nil)
      @nc = NotionCache.instance
      @json = json
      @id = @nc.hex_id(id || @json && @json["id"])
      configuration = @json["configuration"]
      return unless @json && configuration

      @type = configuration["type"]
      @properties = configuration["properties"].map do |property|
        ViewProperty.create_from_json property["property_name"], property
      end
      return unless configuration["subtasks"]

      @subtasks = Subtasks.new configuration["subtasks"]
    end
    attr_reader :id, :json, :type, :properties, :subtasks

そして、View の中に Subtasks を生成します。

  class View::Subtasks
    def initialize(json)
      @json = json
      @filter_scope = json["filter_scope"]
    end
    attr_reader :json, :filter_scope
  end

とりあえず、filter_scope 以外の設定については、Update や Create の時に実装することにします。テストはこのように

NotionRubyMapping::TableView
  find
    when real access
      is expected to be a kind of NotionRubyMapping::TableView
      is expected to eq "32fd8e4e98ab81e88ba9000c227aaf76"
      is expected to eq "table"
      is expected to eq ["Title", "TextTitle", "NumberTitle", "SelectTitle", "MultiSelectTitle", "DateTitle", "UserTitle", "F..."VerificationTitle", "OwnerTitle", "BlockedByTitle", "BlockingTitle", "SubItemTitle", "ParentTitle"]                                                                                
      when subtasks
        is expected to eq NotionRubyMapping::View::Subtasks
        is expected to eq "parents"
    when dry run
      behaves like dry run
        is expected to eq "#!/bin/sh\ncurl  'https://api.notion.com/v1/views/32fd8e4e98ab81e88ba9000c227aaf76' \\\n  -H 'Notion-Version: 2026-03-11' \\\n  -H 'Authorization: Bearer '\"$NOTION_API_KEY\"''"                                                                                                        

Finished in 0.00647 seconds (files took 0.26006 seconds to load)
7 examples, 0 failures

おわりに

とりあえず subtasks configuration を実装しました。この後、group-by などをさらに実装していきます。

https://hkob.notion.site/hkob-16dd8e4e98ab807cbe3cf3cc94cdfe0f?pvs=4hkob.notion.site