notion_ruby_mapping 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/Guardfile +1 -1
  3. data/README.md +550 -197
  4. data/env.yml.sample +7 -0
  5. data/lib/notion_ruby_mapping/base.rb +45 -25
  6. data/lib/notion_ruby_mapping/checkbox_property.rb +26 -0
  7. data/lib/notion_ruby_mapping/created_by_property.rb +21 -0
  8. data/lib/notion_ruby_mapping/created_time_property.rb +20 -0
  9. data/lib/notion_ruby_mapping/database.rb +8 -8
  10. data/lib/notion_ruby_mapping/date_base_property.rb +40 -38
  11. data/lib/notion_ruby_mapping/date_property.rb +61 -0
  12. data/lib/notion_ruby_mapping/email_property.rb +30 -1
  13. data/lib/notion_ruby_mapping/files_property.rb +40 -0
  14. data/lib/notion_ruby_mapping/formula_property.rb +18 -0
  15. data/lib/notion_ruby_mapping/last_edited_by_property.rb +21 -0
  16. data/lib/notion_ruby_mapping/last_edited_time_property.rb +20 -0
  17. data/lib/notion_ruby_mapping/list.rb +33 -2
  18. data/lib/notion_ruby_mapping/mention_object.rb +49 -0
  19. data/lib/notion_ruby_mapping/multi_select_property.rb +25 -6
  20. data/lib/notion_ruby_mapping/notion_cache.rb +126 -53
  21. data/lib/notion_ruby_mapping/number_property.rb +13 -8
  22. data/lib/notion_ruby_mapping/page.rb +5 -2
  23. data/lib/notion_ruby_mapping/payload.rb +10 -2
  24. data/lib/notion_ruby_mapping/people_property.rb +38 -0
  25. data/lib/notion_ruby_mapping/phone_number_property.rb +30 -1
  26. data/lib/notion_ruby_mapping/property.rb +79 -41
  27. data/lib/notion_ruby_mapping/property_cache.rb +31 -12
  28. data/lib/notion_ruby_mapping/query.rb +5 -2
  29. data/lib/notion_ruby_mapping/relation_property.rb +37 -0
  30. data/lib/notion_ruby_mapping/rich_text_object.rb +74 -0
  31. data/lib/notion_ruby_mapping/rich_text_property.rb +18 -0
  32. data/lib/notion_ruby_mapping/rollup_property.rb +29 -0
  33. data/lib/notion_ruby_mapping/select_property.rb +12 -6
  34. data/lib/notion_ruby_mapping/text_object.rb +89 -0
  35. data/lib/notion_ruby_mapping/text_property.rb +61 -0
  36. data/lib/notion_ruby_mapping/title_property.rb +18 -0
  37. data/lib/notion_ruby_mapping/url_property.rb +30 -1
  38. data/lib/notion_ruby_mapping/user_object.rb +38 -0
  39. data/lib/notion_ruby_mapping/version.rb +2 -1
  40. data/lib/notion_ruby_mapping.rb +3 -3
  41. data/notion_ruby_mapping.gemspec +3 -1
  42. metadata +40 -7
@@ -4,21 +4,25 @@ module NotionRubyMapping
4
4
  # abstract class for property
5
5
  class Property
6
6
  # @param [String] name Property name
7
- # @param [Hash] json
8
7
  # @return [Property] generated Property object
9
- def initialize(name, json: nil)
8
+ def initialize(name, will_update: false)
10
9
  @name = name
11
- @will_update = false
12
- @json = json
10
+ @will_update = will_update
13
11
  end
14
12
  attr_reader :name
15
- attr_accessor :will_update
13
+
14
+ # @return [TrueClass, FalseClass]
15
+ attr_reader :will_update
16
16
 
17
17
  # @param [String] key query parameter
18
18
  # @param [Object] value query value
19
19
  # @return [NotionRubyMapping::Query] generated Query object
20
- def make_filter_query(key, value)
21
- Query.new(filter: {"property" => @name, type => {key => value}})
20
+ def make_filter_query(key, value, rollup = nil, rollup_type = nil)
21
+ if rollup
22
+ Query.new filter: {"property" => @name, rollup => {rollup_type => {key => value}}}
23
+ else
24
+ Query.new filter: {"property" => @name, type => {key => value}}
25
+ end
22
26
  end
23
27
 
24
28
  # @return [Symbol] property type
@@ -26,19 +30,53 @@ module NotionRubyMapping
26
30
  self.class::TYPE
27
31
  end
28
32
 
29
- # @param [String] key
33
+ # @param [String] name
30
34
  # @param [Hash] input_json
31
35
  # @return [NotionRubyMapping::Property, nil] generated Property object
32
- def self.create_from_json(key, input_json)
33
- keys = input_json.keys
34
- if keys.include? "number"
35
- NumberProperty.new key, json: input_json["number"]
36
- elsif keys.include? "select"
37
- SelectProperty.new key, json: input_json["select"]
38
- elsif keys.include? "multi_select"
39
- MultiSelectProperty.new key, json: input_json["multi_select"]
36
+ def self.create_from_json(name, input_json)
37
+ raise StandardError, "Property not found: #{name}:#{input_json}" if input_json.nil?
38
+
39
+ case input_json["type"]
40
+ when "number"
41
+ NumberProperty.new name, number: input_json["number"]
42
+ when "select"
43
+ SelectProperty.new name, json: input_json["select"]
44
+ when "multi_select"
45
+ MultiSelectProperty.new name, json: input_json["multi_select"]
46
+ when "date"
47
+ DateProperty.new name, json: input_json["date"]
48
+ when "title"
49
+ TitleProperty.new name, json: input_json["title"]
50
+ when "rich_text"
51
+ RichTextProperty.new name, json: input_json["rich_text"]
52
+ when "checkbox"
53
+ CheckboxProperty.new name, checkbox: input_json["checkbox"]
54
+ when "people"
55
+ PeopleProperty.new name, json: input_json["people"]
56
+ when "email"
57
+ EmailProperty.new name, email: input_json["email"]
58
+ when "url"
59
+ UrlProperty.new name, url: input_json["url"]
60
+ when "phone_number"
61
+ PhoneNumberProperty.new name, phone_number: input_json["phone_number"]
62
+ when "files"
63
+ FilesProperty.new name, json: input_json["files"]
64
+ when "created_time"
65
+ CreatedTimeProperty.new name, created_time: input_json["created_time"]
66
+ when "last_edited_time"
67
+ LastEditedTimeProperty.new name, last_edited_time: input_json["last_edited_time"]
68
+ when "created_by"
69
+ CreatedByProperty.new name, json: input_json["created_by"]
70
+ when "last_edited_by"
71
+ LastEditedByProperty.new name, json: input_json["last_edited_by"]
72
+ when "formula"
73
+ FormulaProperty.new name, json: input_json["formula"]
74
+ when "rollup"
75
+ RollupProperty.new name, json: input_json["rollup"]
76
+ when "relation"
77
+ RelationProperty.new name, json: input_json["relation"]
40
78
  else
41
- nil
79
+ raise StandardError, "Irregular property type: #{input_json["type"]}"
42
80
  end
43
81
  end
44
82
  end
@@ -47,14 +85,14 @@ module NotionRubyMapping
47
85
  module EqualsDoesNotEqual
48
86
  # @param [String, Number] value Query value
49
87
  # @return [NotionRubyMapping::Query] generated Query object
50
- def filter_equals(value)
51
- make_filter_query "equals", value
88
+ def filter_equals(value, rollup = nil, rollup_type = nil)
89
+ make_filter_query "equals", value, rollup, rollup_type
52
90
  end
53
91
 
54
92
  # @param [String, Number] value Query value
55
93
  # @return [NotionRubyMapping::Query] generated Query object
56
- def filter_does_not_equal(value)
57
- make_filter_query "does_not_equal", value
94
+ def filter_does_not_equal(value, rollup = nil, rollup_type = nil)
95
+ make_filter_query "does_not_equal", value, rollup, rollup_type
58
96
  end
59
97
  end
60
98
 
@@ -62,14 +100,14 @@ module NotionRubyMapping
62
100
  module ContainsDoesNotContain
63
101
  # @param [String] value Query value
64
102
  # @return [NotionRubyMapping::Query] generated Query object
65
- def filter_contains(value)
66
- make_filter_query "contains", value
103
+ def filter_contains(value, rollup = nil, rollup_type = nil)
104
+ make_filter_query "contains", value, rollup, rollup_type
67
105
  end
68
106
 
69
107
  # @param [String] value Query value
70
108
  # @return [NotionRubyMapping::Query] generated Query object
71
- def filter_does_not_contain(value)
72
- make_filter_query "does_not_contain", value
109
+ def filter_does_not_contain(value, rollup = nil, rollup_type = nil)
110
+ make_filter_query "does_not_contain", value, rollup, rollup_type
73
111
  end
74
112
  end
75
113
 
@@ -77,27 +115,27 @@ module NotionRubyMapping
77
115
  module StartsWithEndsWith
78
116
  # @param [String] value Query value
79
117
  # @return [NotionRubyMapping::Query] generated Query object
80
- def filter_starts_with(value)
81
- make_filter_query "starts_with", value
118
+ def filter_starts_with(value, rollup = nil, rollup_type = nil)
119
+ make_filter_query "starts_with", value, rollup, rollup_type
82
120
  end
83
121
 
84
122
  # @param [String] value Query value
85
123
  # @return [NotionRubyMapping::Query] generated Query object
86
- def filter_ends_with(value)
87
- make_filter_query "ends_with", value
124
+ def filter_ends_with(value, rollup = nil, rollup_type = nil)
125
+ make_filter_query "ends_with", value, rollup, rollup_type
88
126
  end
89
127
  end
90
128
 
91
129
  # module for make query of is_empty and is_not_empty
92
130
  module IsEmptyIsNotEmpty
93
131
  # @return [NotionRubyMapping::Query] generated Query object
94
- def filter_is_empty
95
- make_filter_query "is_empty", true
132
+ def filter_is_empty(rollup = nil, rollup_type = nil)
133
+ make_filter_query "is_empty", true, rollup, rollup_type
96
134
  end
97
135
 
98
136
  # @return [NotionRubyMapping::Query] generated Query object
99
- def filter_is_not_empty
100
- make_filter_query "is_not_empty", true
137
+ def filter_is_not_empty(rollup = nil, rollup_type = nil)
138
+ make_filter_query "is_not_empty", true, rollup, rollup_type
101
139
  end
102
140
  end
103
141
 
@@ -105,26 +143,26 @@ module NotionRubyMapping
105
143
  module GreaterThanLessThan
106
144
  # @param [Number] value Query value
107
145
  # @return [NotionRubyMapping::Query] generated Query object
108
- def filter_greater_than(value)
109
- make_filter_query "greater_than", value
146
+ def filter_greater_than(value, rollup = nil, rollup_type = nil)
147
+ make_filter_query "greater_than", value, rollup, rollup_type
110
148
  end
111
149
 
112
150
  # @param [Number] value Query value
113
151
  # @return [NotionRubyMapping::Query] generated Query object
114
- def filter_less_than(value)
115
- make_filter_query "less_than", value
152
+ def filter_less_than(value, rollup = nil, rollup_type = nil)
153
+ make_filter_query "less_than", value, rollup, rollup_type
116
154
  end
117
155
 
118
156
  # @param [Number] value Query value
119
157
  # @return [NotionRubyMapping::Query] generated Query object
120
- def filter_greater_than_or_equal_to(value)
121
- make_filter_query "greater_than_or_equal_to", value
158
+ def filter_greater_than_or_equal_to(value, rollup = nil, rollup_type = nil)
159
+ make_filter_query "greater_than_or_equal_to", value, rollup, rollup_type
122
160
  end
123
161
 
124
162
  # @param [Number] value Query value
125
163
  # @return [NotionRubyMapping::Query] generated Query object
126
- def filter_less_than_or_equal_to(value)
127
- make_filter_query "less_than_or_equal_to", value
164
+ def filter_less_than_or_equal_to(value, rollup = nil, rollup_type = nil)
165
+ make_filter_query "less_than_or_equal_to", value, rollup, rollup_type
128
166
  end
129
167
  end
130
168
  end
@@ -1,36 +1,55 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NotionRubyMapping
4
+ # PropertyCache class
2
5
  class PropertyCache
6
+ include Enumerable
3
7
  def initialize(json = {})
4
8
  @properties = {}
5
9
  @json = json
6
10
  end
11
+ attr_writer :json
7
12
 
8
13
  # @param [String] key
9
14
  # @return [Property] Property for key
10
15
  def [](key)
11
- ans = @properties[key]
12
- unless ans
13
- if @json && @json[key]
14
- @properties[key] = Property.create_from_json key, @json[key]
15
- end
16
+ @properties[key] ||= Property.create_from_json key, @json[key]
17
+ end
18
+
19
+ # @param [Array] key
20
+ # @return [Array]
21
+ def values_at(*key)
22
+ generate_all_properties
23
+ @properties.values_at(*key)
24
+ end
25
+
26
+ def generate_all_properties
27
+ if @json.empty?
28
+ @properties.values
29
+ else
30
+ @json.keys.map { |key| self[key] }
16
31
  end
17
- @properties[key]
32
+ end
33
+
34
+ # @return [Hash, Enumerator]
35
+ def each(&block)
36
+ return enum_for(:each) unless block_given?
37
+
38
+ generate_all_properties.each(&block)
18
39
  end
19
40
 
20
41
  # @param [Property] property added Property
21
- # @param [FalseClass] will_update true if the property value will update to Notion
22
- def add_property(property, will_update: false)
42
+ def add_property(property)
23
43
  @properties[property.name] = property
24
- property.will_update = true if will_update
25
44
  self
26
45
  end
27
46
 
28
47
  # @return [Hash] created json
29
- def create_json
30
- @properties.each_with_object({}) do |(key, property), ans|
48
+ def property_values_json
49
+ @properties.each_with_object({}) do |(_, property), ans|
31
50
  if property.will_update
32
51
  ans["properties"] ||= {}
33
- ans["properties"][key] = property.create_json
52
+ ans["properties"].merge! property.property_values_json
34
53
  end
35
54
  end
36
55
  end
@@ -3,11 +3,14 @@
3
3
  module NotionRubyMapping
4
4
  # Query object
5
5
  class Query
6
- def initialize(filter: {}, sort: [])
6
+ def initialize(filter: {}, sort: [], page_size: 100, start_cursor: nil)
7
7
  @filter = filter
8
8
  @sort = sort
9
+ @page_size = page_size
10
+ @start_cursor = start_cursor
9
11
  end
10
- attr_reader :filter, :sort
12
+ attr_reader :filter, :sort, :page_size
13
+ attr_accessor :start_cursor
11
14
 
12
15
  # @param [Query] other_query other query
13
16
  # @return [NotionRubyMapping::Query] updated self (Query object)
@@ -4,5 +4,42 @@ module NotionRubyMapping
4
4
  # MultiSelect property
5
5
  class RelationProperty < MultiProperty
6
6
  TYPE = "relation"
7
+
8
+ # @param [String] name
9
+ # @param [Hash] json
10
+ # @param [String, Array<String>] relation
11
+ def initialize(name, will_update: false, json: nil, relation: nil)
12
+ super name, will_update: will_update
13
+ @relation = if relation
14
+ Array(relation)
15
+ elsif json
16
+ json.map { |r| r["id"] }
17
+ else
18
+ []
19
+ end
20
+ end
21
+
22
+ # @param [String, Array<String>] relation a id or Array of id
23
+ # @return [Array<String>] settled relation Array
24
+ def relation=(relation)
25
+ @will_update = true
26
+ @relation = Array(relation)
27
+ end
28
+
29
+ # @param [Hash] json
30
+ def update_from_json(json)
31
+ @will_update = false
32
+ @relation = json["relation"].map { |r| r["id"] }
33
+ end
34
+
35
+ # @return [Hash] created json
36
+ def property_values_json
37
+ {
38
+ @name => {
39
+ "type" => "relation",
40
+ "relation" => @relation.map { |rid| {"id" => rid} },
41
+ },
42
+ }
43
+ end
7
44
  end
8
45
  end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NotionRubyMapping
4
+ # RichTextObject
5
+ class RichTextObject
6
+ # @param [String] type
7
+ # @return [TextObject]
8
+ def initialize(type, options = {})
9
+ if instance_of?(RichTextObject)
10
+ raise StandardError,
11
+ "RichTextObject is abstract class. Please use TextObject."
12
+ end
13
+
14
+ @type = type
15
+ @options = options
16
+ end
17
+
18
+ def self.create_from_json(json)
19
+ type = json["type"]
20
+ options = (json["annotations"] || {}).merge(json.slice("plain_text", "href"))
21
+ case type
22
+ when "text"
23
+ TextObject.new json["plain_text"], options
24
+ when "mention"
25
+ mention = json["mention"]
26
+ case mention["type"]
27
+ when "user"
28
+ MentionObject.new options.merge({"user_id" => mention["user"]["id"]})
29
+ when "page"
30
+ MentionObject.new options.merge({"page_id" => mention["page"]["id"]})
31
+ when "database"
32
+ MentionObject.new options.merge({"database_id" => mention["database"]["id"]})
33
+ when "date"
34
+ MentionObject.new options.merge(mention["date"].slice("start", "end", "time_zone"))
35
+ else
36
+ raise StandardError, json
37
+ end
38
+ else
39
+ raise StandardError, json
40
+ end
41
+ end
42
+
43
+ # @param [RichTextObject, String] to
44
+ # @return [NotionRubyMapping::RichTextObject] RichTextObject
45
+ def self.text_object(to)
46
+ if to.is_a? RichTextObject
47
+ to
48
+ else
49
+ TextObject.new to
50
+ end
51
+ end
52
+
53
+ # @return [Hash{String (frozen)->Object}]
54
+ def property_values_json
55
+ {
56
+ "type" => @type,
57
+ @type => partial_property_values_json,
58
+ "plain_text" => @options["plain_text"],
59
+ "href" => @options["href"],
60
+ }.merge annotations_json
61
+ end
62
+
63
+ protected
64
+
65
+ # @return [Hash] options
66
+ attr_reader :options
67
+
68
+ # @return [Hash, Hash{String (frozen)->Hash}]
69
+ def annotations_json
70
+ annotations = @options.slice(*%w[bold italic strikethrough underline code color])
71
+ annotations.empty? ? {} : {"annotations" => annotations}
72
+ end
73
+ end
74
+ end
@@ -1,7 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionRubyMapping
4
+ # RichTextProperty
4
5
  class RichTextProperty < TextProperty
5
6
  TYPE = "rich_text"
7
+
8
+ # @param [Hash] json
9
+ def update_from_json(json)
10
+ @will_update = false
11
+ @text_objects = json["rich_text"].map { |to| RichTextObject.create_from_json to }
12
+ end
13
+
14
+ # @return [Hash] created json
15
+ def property_values_json
16
+ text_json = @text_objects.map(&:property_values_json)
17
+ {
18
+ @name => {
19
+ "type" => "rich_text",
20
+ "rich_text" => text_json.empty? ? (@json || []) : text_json,
21
+ },
22
+ }
23
+ end
6
24
  end
7
25
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NotionRubyMapping
4
+ # Rollup property
5
+ class RollupProperty < DateBaseProperty
6
+ include ContainsDoesNotContain
7
+ include StartsWithEndsWith
8
+ include GreaterThanLessThan
9
+ TYPE = "rollup"
10
+
11
+ # @param [String] name
12
+ # @param [Hash] json
13
+ def initialize(name, json: nil)
14
+ super name, will_update: false
15
+ @json = json
16
+ end
17
+
18
+ # @return [Hash] {} created_time cannot be updated
19
+ def property_values_json
20
+ {}
21
+ end
22
+
23
+ # @param [Hash] json
24
+ def update_from_json(json)
25
+ @will_update = false
26
+ @json = json["rollup"]
27
+ end
28
+ end
29
+ end
@@ -10,14 +10,20 @@ module NotionRubyMapping
10
10
  # @param [String] name Property name
11
11
  # @param [Hash] json
12
12
  # @param [String] select String value (optional)
13
- def initialize(name, json: nil, select: nil)
14
- super(name, json: json)
15
- @select = select
13
+ def initialize(name, will_update: false, json: nil, select: nil)
14
+ super name, will_update: will_update
15
+ @select = select || json && json["name"]
16
+ end
17
+
18
+ # @param [Hash] json
19
+ def update_from_json(json)
20
+ @will_update = false
21
+ @select = json["select"]["name"]
16
22
  end
17
23
 
18
24
  # @return [Hash]
19
- def create_json
20
- {"select" => @select ? {"name" => @select} : @json} || {}
25
+ def property_values_json
26
+ {@name => {"type" => "select", "select" => (@select ? {"name" => @select} : @json)}}
21
27
  end
22
28
 
23
29
  # @param [String] select
@@ -27,4 +33,4 @@ module NotionRubyMapping
27
33
  @select = select
28
34
  end
29
35
  end
30
- end
36
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NotionRubyMapping
4
+ # TextObject
5
+ class TextObject < RichTextObject
6
+ # @param [String] text
7
+ # @return [TextObject]
8
+ def initialize(text, options = {})
9
+ super "text", {"plain_text" => text}.merge(options)
10
+ @text = text
11
+ @type = "text"
12
+ @will_update = false
13
+ end
14
+ attr_reader :text, :will_update
15
+
16
+ # @param [String, RichTextObject] value
17
+ # @return [RichTextObject] self
18
+ def text=(value)
19
+ @will_update = true
20
+ if value.is_a? RichTextObject
21
+ @options = value.options
22
+ @text = value.text
23
+ else
24
+ p value
25
+ @text = value
26
+ @options["plain_text"] = value
27
+ end
28
+ self
29
+ end
30
+
31
+ # @param [String, RichTextObject] value
32
+ # @return [String] input text
33
+ def plain_text=(value)
34
+ text(value)
35
+ end
36
+
37
+ # @param [Boolean] flag
38
+ # @return [Boolean] input flag
39
+ def bold=(flag)
40
+ @will_update = true
41
+ @options["bold"] = flag
42
+ end
43
+
44
+ # @param [Boolean] flag
45
+ # @return [Boolean] input flag
46
+ def italic=(flag)
47
+ @will_update = true
48
+ @options["italic"] = flag
49
+ end
50
+
51
+ # @param [Boolean] flag
52
+ # @return [Boolean] input flag
53
+ def strikethrough=(flag)
54
+ @will_update = true
55
+ @options["strikethrough"] = flag
56
+ end
57
+
58
+ # @param [Boolean] flag
59
+ # @return [Boolean] input flag
60
+ def underline=(flag)
61
+ @will_update = true
62
+ @options["underline"] = flag
63
+ end
64
+
65
+ # @param [Boolean] flag
66
+ # @return [Boolean] input flag
67
+ def code=(flag)
68
+ @will_update = true
69
+ @options["code"] = flag
70
+ end
71
+
72
+ # @param [String] color
73
+ # @return [String] input color
74
+ def color=(color)
75
+ @will_update = true
76
+ @options["color"] = color
77
+ end
78
+
79
+ protected
80
+
81
+ def partial_property_values_json
82
+ url = @options["href"]
83
+ {
84
+ "content" => @text,
85
+ "link" => url ? {"url" => url} : nil,
86
+ }
87
+ end
88
+ end
89
+ end
@@ -1,11 +1,72 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "forwardable"
4
+
3
5
  module NotionRubyMapping
4
6
  # Text property
5
7
  class TextProperty < Property
8
+ extend Forwardable
9
+ include Enumerable
6
10
  include EqualsDoesNotEqual
7
11
  include ContainsDoesNotContain
8
12
  include StartsWithEndsWith
9
13
  include IsEmptyIsNotEmpty
14
+
15
+ # @param [String] name
16
+ # @param [Hash] json
17
+ # @param [Array<RichTextObject>] text_objects
18
+ def initialize(name, will_update: false, json: nil, text_objects: nil)
19
+ raise StandardError, "TextObject is abstract class. Please use RichTextProperty." if instance_of? TextProperty
20
+
21
+ super name, will_update: will_update
22
+ @text_objects = if text_objects
23
+ text_objects.map { |to_s| RichTextObject.text_object to_s }
24
+ elsif json
25
+ json.map { |to| RichTextObject.create_from_json to }
26
+ else
27
+ []
28
+ end
29
+ end
30
+ attr_reader :text_objects
31
+
32
+ # @return [TrueClass, FalseClass] will update?
33
+ def will_update
34
+ @will_update || @text_objects.map(&:will_update).any?
35
+ end
36
+
37
+ # @param [Proc] block
38
+ # @return [Enumerator]
39
+ def each(&block)
40
+ return enum_for(:each) unless block
41
+
42
+ @text_objects.each(&block)
43
+ end
44
+
45
+ # @param [String, RichTextObject] to
46
+ # @return [NotionRubyMapping::RichTextObject] added RichTextObject
47
+ def <<(to)
48
+ @will_update = true
49
+ rto = RichTextObject.text_object(to)
50
+ @text_objects << rto
51
+ rto
52
+ end
53
+
54
+ # @param [Numeric] index index number
55
+ # @return [RichTextObject] selected RichTextObject
56
+ def [](index)
57
+ @text_objects[index]
58
+ end
59
+
60
+ # @param [Numeric] index
61
+ # @return [NotionRubyMapping::RichTextObject] removed RichTextObject
62
+ def delete_at(index)
63
+ @will_update = true
64
+ @text_objects[index]
65
+ end
66
+
67
+ # @return [String] full_text
68
+ def full_text
69
+ map(&:text).join ""
70
+ end
10
71
  end
11
72
  end
@@ -1,7 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionRubyMapping
4
+ # TitleProperty
4
5
  class TitleProperty < TextProperty
5
6
  TYPE = "title"
7
+
8
+ # @param [Hash] json
9
+ def update_from_json(json)
10
+ @will_update = false
11
+ @text_objects = json["title"].map { |to| RichTextObject.create_from_json to }
12
+ end
13
+
14
+ # @return [Hash] created json
15
+ def property_values_json
16
+ text_json = map(&:property_values_json)
17
+ {
18
+ @name => {
19
+ "type" => "title",
20
+ "title" => text_json.empty? ? @json : text_json,
21
+ },
22
+ }
23
+ end
6
24
  end
7
25
  end