Class: NotionRubyMapping::FilesProperty

Inherits:
Property
  • Object
show all
Includes:
IsEmptyIsNotEmpty
Defined in:
lib/notion_ruby_mapping/properties/files_property.rb

Overview

Files property

Constant Summary collapse

TYPE =
"files"

Instance Attribute Summary collapse

Attributes inherited from Property

#name, #property_cache, #property_id, #will_update

Instance Method Summary collapse

Methods included from IsEmptyIsNotEmpty

#filter_is_empty, #filter_is_not_empty

Methods inherited from Property

#assert_data_source_property, #assert_database_or_data_source_property, #assert_page_property, #clear_will_update, create_from_json, #data_source?, #database?, #database_or_data_source?, #make_filter_query, #new_name=, #page?, #property_schema_json, #remove, #retrieve_page_property, #type, #update_property_schema_json

Constructor Details

#initialize(name, will_update: false, base_type: "page", json: nil, files: [], property_id: nil, property_cache: nil) ⇒ FilesProperty

Returns a new instance of FilesProperty.

Parameters:

  • name (String, Symbol)

    Property name

  • files (String) (defaults to: [])

    files value (optional)



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/notion_ruby_mapping/properties/files_property.rb', line 47

def initialize(name, will_update: false, base_type: "page", json: nil, files: [], property_id: nil,
               property_cache: nil)
  super name, will_update: will_update, base_type: base_type, property_id: property_id,
              property_cache: property_cache
  if database_or_data_source?
    @files = json || {}
  elsif json
    @files = json.map { |sub_json| FileObject.new json: sub_json }
    @file_names = json.map { |sub_json| sub_json["name"] }
  elsif !files.empty?
    @files = Array(files).map { |url| FileObject.file_object url }
    @file_names = Array(files)
  else
    @files = []
  end
end

Instance Attribute Details

#filesObject

Returns the value of attribute files.



9
10
11
# File 'lib/notion_ruby_mapping/properties/files_property.rb', line 9

def files
  @files
end

Instance Method Details

#clearObject

Removes all files from the property.

This method replaces the files array with an empty array.



18
19
20
21
# File 'lib/notion_ruby_mapping/properties/files_property.rb', line 18

def clear
  assert_page_property __method__
  self.files = []
end

#file_names=(file_names = []) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/notion_ruby_mapping/properties/files_property.rb', line 30

def file_names=(file_names = [])
  array_file_names = Array(file_names)
  unless @files.length == array_file_names.length
    raise ArgumentError,
          "files and file_names must be the same sizes."
  end

  @will_update = true
  @file_names = array_file_names
end

#property_values_jsonHash

Returns:

  • (Hash)


65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/notion_ruby_mapping/properties/files_property.rb', line 65

def property_values_json
  assert_page_property __method__
  if @files.map(&:type).include? "file"
    {}
  else
    files = @files.map(&:property_values_json)
    @file_names&.each_with_index do |name, i|
      files[i]["name"] = name.is_a?(FileUploadObject) ? name.fname : name
    end
    {@name => {"files" => files, "type" => "files"}}
  end
end

#update_from_json(json) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/notion_ruby_mapping/properties/files_property.rb', line 78

def update_from_json(json)
  return if database_or_data_source?

  @files = json["files"].map { |sub_json| FileObject.new json: sub_json }
  @file_names = json["files"].map { |sub_json| sub_json["name"] }
  @will_update = false
  self
end