Class: NotionRubyMapping::List

Inherits:
Base
  • Object
show all
Includes:
Enumerable
Defined in:
lib/notion_ruby_mapping/blocks/list.rb

Overview

Notion List object

Instance Attribute Summary collapse

Attributes inherited from Base

#archived, #has_children, #id, #json

Instance Method Summary collapse

Methods inherited from Base

#append_block_children, #assert_parent_children_pair, #assign_property, #block?, block_id, #children, #comments, #cover, create_from_json, #created_time, #database?, database_id, dry_run_script, #get, #icon, #inspect, #json_properties, #last_edited_time, #new_record?, #page?, page_id, #parent, #parent_id, #properties, #property_values_json, #reload, #restore_from_json, #save, #set_cover, #set_icon, #synced_block_original?, #update_json

Constructor Details

#initialize(json: nil, id: nil, type: nil, value: nil, query: nil) ⇒ List

Returns a new instance of List.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/notion_ruby_mapping/blocks/list.rb', line 8

def initialize(json: nil, id: nil, type: nil, value: nil, query: nil)
  super(json: json, id: id)
  @has_more = @json["has_more"]
  @load_all_contents = !@has_more

  case type
  when "comment_parent"
    @comment_parent = value
  when "database"
    @database = value
  when "parent"
    @parent = value
  when "property"
    @property = value
  when "user_object"
    @user_object = value
  when "search"
    @search = value
  end
  @query = query
  @index = 0
  @has_content = true
end

Instance Attribute Details

#has_moreObject (readonly)

Returns the value of attribute has_more.



31
32
33
# File 'lib/notion_ruby_mapping/blocks/list.rb', line 31

def has_more
  @has_more
end

Instance Method Details

#each(&block) ⇒ NotionRubyMapping::List, Enumerator



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/notion_ruby_mapping/blocks/list.rb', line 35

def each(&block)
  return enum_for(:each) if block.nil?

  if @parent
    each_sub base: @page,
             query: -> { @parent.children @query },
             create_object: ->(json) { Base.create_from_json json },
             &block
  elsif @database
    each_sub base: @database,
             query: -> { @nc.database_query_request @database.id, @query },
             create_object: ->(json) { Base.create_from_json json },
             &block
  elsif @property
    each_sub base: @property,
             query: lambda {
               @nc.page_property_request @property.property_cache.page_id,
                                         @property.property_id,
                                         @query.query_json
             },
             create_object: lambda { |json|
               case json["type"]
               when "people"
                 UserObject.new json: json["people"]
               when "relation"
                 json["relation"]["id"]
               when "rich_text"
                 RichTextObject.create_from_json json["rich_text"]
               when "title"
                 RichTextObject.create_from_json json["title"]
               else
                 json
               end
             },
             &block
  elsif @comment_parent
    each_sub base: @comment_parent,
             query: -> { @nc.retrieve_comments_request @comment_parent.id, @query },
             create_object: ->(json) { CommentObject.new json: json },
             &block
  elsif @user_object
    each_sub base: @user_object,
             query: -> { @nc.users_request @query.query_json },
             create_object: ->(json) { UserObject.new json: json },
             &block
  elsif @search
    each_sub base: @search,
             query: -> { @nc.search @search.payload.merge(@query.query_json) },
             create_object: ->(json) { Base.create_from_json json },
             &block
  end
  self
end