Class: NotionRubyMapping::RichTextArray

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/notion_ruby_mapping/rich_text_array.rb

Overview

RichTextObject

Instance Method Summary collapse

Constructor Details

#initialize(key, json: nil, text_objects: nil, will_update: false) ⇒ RichTextArray

Returns a new instance of RichTextArray.

Parameters:

  • json (Array) (defaults to: nil)


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/notion_ruby_mapping/rich_text_array.rb', line 9

def initialize(key, json: nil, text_objects: nil, will_update: false)
  @key = key
  @rich_text_objects = if json
                         create_from_json(json)
                       elsif text_objects
                         Array(text_objects).map do |to|
                           RichTextObject.text_object to
                         end
                       else
                         []
                       end
  @will_update = will_update
end

Instance Method Details

#<<(to) ⇒ NotionRubyMapping::RichTextObject

Returns added RichTextObject.

Parameters:

Returns:



68
69
70
71
72
73
# File 'lib/notion_ruby_mapping/rich_text_array.rb', line 68

def <<(to)
  @will_update = true
  rto = RichTextObject.text_object(to)
  @rich_text_objects << rto
  rto
end

#[](index) ⇒ RichTextObject

Returns selected RichTextObject.

Parameters:

  • index (Numeric)

    index number

Returns:



77
78
79
# File 'lib/notion_ruby_mapping/rich_text_array.rb', line 77

def [](index)
  @rich_text_objects[index]
end

#create_from_json(json = []) ⇒ Array

Returns RichTextArray.

Parameters:

  • json (Array) (defaults to: [])

Returns:

  • (Array)

    RichTextArray



25
26
27
# File 'lib/notion_ruby_mapping/rich_text_array.rb', line 25

def create_from_json(json = [])
  json.map { |rt_json| RichTextObject.create_from_json rt_json }
end

#delete_at(index) ⇒ NotionRubyMapping::RichTextObject

Returns removed RichTextObject.

Parameters:

  • index (Numeric)

Returns:



31
32
33
34
# File 'lib/notion_ruby_mapping/rich_text_array.rb', line 31

def delete_at(index)
  @will_update = true
  @rich_text_objects.delete_at index
end

#each(&block) ⇒ Enumerator

Parameters:

  • block (Proc)

Returns:

  • (Enumerator)


38
39
40
41
42
# File 'lib/notion_ruby_mapping/rich_text_array.rb', line 38

def each(&block)
  return enum_for(:each) unless block

  @rich_text_objects.each(&block)
end

#full_textString

Returns:

  • (String)


45
46
47
# File 'lib/notion_ruby_mapping/rich_text_array.rb', line 45

def full_text
  map(&:text).join ""
end

#property_schema_jsonObject



53
54
55
# File 'lib/notion_ruby_mapping/rich_text_array.rb', line 53

def property_schema_json
  will_update ? {@key => @rich_text_objects.map(&:property_values_json)} : {}
end

#property_values_jsonObject



49
50
51
# File 'lib/notion_ruby_mapping/rich_text_array.rb', line 49

def property_values_json
  will_update ? @rich_text_objects.map(&:property_values_json) : []
end

#update_property_schema_jsonObject



57
58
59
# File 'lib/notion_ruby_mapping/rich_text_array.rb', line 57

def update_property_schema_json
  will_update ? {@key => @rich_text_objects.map(&:property_values_json)} : {}
end

#will_updateTrueClass, FalseClass

Returns true if it will update.

Returns:

  • (TrueClass, FalseClass)

    true if it will update



62
63
64
# File 'lib/notion_ruby_mapping/rich_text_array.rb', line 62

def will_update
  @will_update || @rich_text_objects.map(&:will_update).any?
end