Class: NotionRubyMapping::RichTextArray

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

Overview

RichTextObject

Instance Attribute Summary collapse

Class Method Summary collapse

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/controllers/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 Attribute Details

#will_updateTrueClass, FalseClass

Returns true if it will update.

Returns:

  • (TrueClass, FalseClass)

    true if it will update



88
89
90
# File 'lib/notion_ruby_mapping/controllers/rich_text_array.rb', line 88

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

Class Method Details

.rich_text_array(key, text_objects = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/notion_ruby_mapping/controllers/rich_text_array.rb', line 24

def self.rich_text_array(key, text_objects = nil)
  if text_objects.nil?
    RichTextArray.new key
  elsif text_objects.is_a? RichTextArray
    text_objects
  else
    RichTextArray.new key, text_objects: text_objects
  end
end

Instance Method Details

#<<(to) ⇒ NotionRubyMapping::RichTextObject

Returns added RichTextObject.

Parameters:

Returns:



36
37
38
39
40
41
# File 'lib/notion_ruby_mapping/controllers/rich_text_array.rb', line 36

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:



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

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

#create_from_json(json = []) ⇒ Array

Returns RichTextArray.

Parameters:

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

Returns:

  • (Array)

    RichTextArray



51
52
53
# File 'lib/notion_ruby_mapping/controllers/rich_text_array.rb', line 51

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:



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

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

#each(&block) ⇒ Enumerator

Parameters:

  • block (Proc)

Returns:

  • (Enumerator)


64
65
66
67
68
# File 'lib/notion_ruby_mapping/controllers/rich_text_array.rb', line 64

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

  @rich_text_objects.each(&block)
end

#full_textString

Returns:

  • (String)


71
72
73
# File 'lib/notion_ruby_mapping/controllers/rich_text_array.rb', line 71

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

#property_schema_jsonObject



79
80
81
# File 'lib/notion_ruby_mapping/controllers/rich_text_array.rb', line 79

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

#property_values_jsonObject



75
76
77
# File 'lib/notion_ruby_mapping/controllers/rich_text_array.rb', line 75

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

#update_property_schema_jsonObject



83
84
85
# File 'lib/notion_ruby_mapping/controllers/rich_text_array.rb', line 83

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