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)

    Property name

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

    files value (optional)



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

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

Raises:

  • (StandardError)


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

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

  @will_update = true
  @file_names = array_file_names
end

#property_values_jsonHash

Returns:

  • (Hash)


54
55
56
57
58
59
60
61
62
63
# File 'lib/notion_ruby_mapping/properties/files_property.rb', line 54

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 { |name, i| files[i]["name"] = name }
    {@name => {"files" => files, "type" => "files"}}
  end
end

#update_from_json(json) ⇒ Object



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

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