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



107
108
109
# File 'lib/notion_ruby_mapping/controllers/rich_text_array.rb', line 107

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

#clear_will_updateFalseClass

Returns:

  • (FalseClass)


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

def clear_will_update
  @will_update = false
  @rich_text_objects.each(&:clear_will_update)
  false
end

#create_from_json(json = []) ⇒ Array

Returns RichTextArray.

Parameters:

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

Returns:

  • (Array)

    RichTextArray



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

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:



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

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

#each(&block) ⇒ Enumerator

Parameters:

  • block (Proc)

Returns:

  • (Enumerator)


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

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

  @rich_text_objects.each(&block)
end

#full_textString

Returns:

  • (String)


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

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

#property_schema_jsonObject



98
99
100
# File 'lib/notion_ruby_mapping/controllers/rich_text_array.rb', line 98

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

#property_values_jsonObject



94
95
96
# File 'lib/notion_ruby_mapping/controllers/rich_text_array.rb', line 94

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

#rich_text_objects=(text_info) ⇒ Object

Parameters:



83
84
85
86
87
88
89
90
91
92
# File 'lib/notion_ruby_mapping/controllers/rich_text_array.rb', line 83

def rich_text_objects=(text_info)
  @will_update = true
  @rich_text_objects = if text_info.is_a? RichTextArray
                         text_info.filter { true }
                       else
                         Array(text_info).map do |to|
                           RichTextObject.text_object to
                         end
                       end
end

#update_property_schema_json(flag = false) ⇒ Object



102
103
104
# File 'lib/notion_ruby_mapping/controllers/rich_text_array.rb', line 102

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