Class: NotionRubyMapping::FilesProperty

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

Overview

Select 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_database_property, #assert_page_property, #clear_will_update, #contents?, create_from_json, #database?, #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)



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/notion_ruby_mapping/properties/files_property.rb', line 39

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?
    @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

#file_names=(file_names = []) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/notion_ruby_mapping/properties/files_property.rb', line 22

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

  @will_update = true
  @file_names = array_file_names
end

#property_values_jsonHash

Returns:

  • (Hash)


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/notion_ruby_mapping/properties/files_property.rb', line 57

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



70
71
72
73
74
75
76
77
# File 'lib/notion_ruby_mapping/properties/files_property.rb', line 70

def update_from_json(json)
  return if database?

  @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