Class: NotionRubyMapping::PropertyCache

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

Overview

PropertyCache class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json = {}, base_type: "page", page_id: nil) ⇒ PropertyCache



7
8
9
10
11
12
# File 'lib/notion_ruby_mapping/controllers/property_cache.rb', line 7

def initialize(json = {}, base_type: "page", page_id: nil)
  @properties = {}
  @json = json
  @base_type = base_type
  @page_id = page_id
end

Instance Attribute Details

#base_typeObject (readonly)

Returns the value of attribute base_type.



14
15
16
# File 'lib/notion_ruby_mapping/controllers/property_cache.rb', line 14

def base_type
  @base_type
end

#json=(value) ⇒ Object (writeonly)

Sets the attribute json



13
14
15
# File 'lib/notion_ruby_mapping/controllers/property_cache.rb', line 13

def json=(value)
  @json = value
end

#page_idObject (readonly)

Returns the value of attribute page_id.



14
15
16
# File 'lib/notion_ruby_mapping/controllers/property_cache.rb', line 14

def page_id
  @page_id
end

Instance Method Details

#[](key) ⇒ Property

Returns Property for key.



19
20
21
# File 'lib/notion_ruby_mapping/controllers/property_cache.rb', line 19

def [](key)
  @properties[key] ||= Property.create_from_json key, @json[key], @base_type, self
end

#add_property(property) ⇒ Object



24
25
26
27
28
# File 'lib/notion_ruby_mapping/controllers/property_cache.rb', line 24

def add_property(property)
  @properties[property.name] = property
  property.property_cache = self
  self
end

#clear_will_updateObject



30
31
32
33
34
# File 'lib/notion_ruby_mapping/controllers/property_cache.rb', line 30

def clear_will_update
  @properties.each do |_, property|
    property.clear_will_update
  end
end

#data_source?TrueClass, FalseClass



42
43
44
# File 'lib/notion_ruby_mapping/controllers/property_cache.rb', line 42

def data_source?
  @base_type == "data_source"
end

#database?TrueClass, FalseClass



37
38
39
# File 'lib/notion_ruby_mapping/controllers/property_cache.rb', line 37

def database?
  @base_type == "database"
end

#database_or_data_source?TrueClass, FalseClass



47
48
49
# File 'lib/notion_ruby_mapping/controllers/property_cache.rb', line 47

def database_or_data_source?
  %w[database data_source].include? @base_type
end

#each(&block) ⇒ Hash, Enumerator



52
53
54
55
56
# File 'lib/notion_ruby_mapping/controllers/property_cache.rb', line 52

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

  generate_all_properties.each(&block)
end

#generate_all_propertiesObject



58
59
60
61
62
63
64
# File 'lib/notion_ruby_mapping/controllers/property_cache.rb', line 58

def generate_all_properties
  if @json.nil? || @json.empty?
    @properties.values
  else
    @json.keys.map { |key| self[key] }
  end
end

#inspectObject



66
67
68
# File 'lib/notion_ruby_mapping/controllers/property_cache.rb', line 66

def inspect
  "PropertyCache"
end

#page?TrueClass, FalseClass



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

def page?
  @base_type == "page"
end

#property_schema_jsonHash



86
87
88
89
90
91
92
93
94
# File 'lib/notion_ruby_mapping/controllers/property_cache.rb', line 86

def property_schema_json
  schema = @properties.each_with_object({}) do |(_, property), ans|
    if property.will_update
      ans["properties"] ||= {}
      ans["properties"].merge! property.property_schema_json
    end
  end
  database? ? {"initial_data_source" => schema} : schema
end

#property_values_jsonHash



76
77
78
79
80
81
82
83
# File 'lib/notion_ruby_mapping/controllers/property_cache.rb', line 76

def property_values_json
  @properties.each_with_object({}) do |(_, property), ans|
    if property.will_update
      ans["properties"] ||= {}
      ans["properties"].merge! property.property_values_json
    end
  end
end

#update_property_schema_jsonHash



97
98
99
100
101
102
103
104
105
# File 'lib/notion_ruby_mapping/controllers/property_cache.rb', line 97

def update_property_schema_json
  schema = @properties.each_with_object({}) do |(_, property), ans|
    if property.will_update
      ans["properties"] ||= {}
      ans["properties"].merge! property.update_property_schema_json
    end
  end
  database? ? {"initial_data_source" => schema} : schema
end

#values_at(*key) ⇒ Array



110
111
112
113
# File 'lib/notion_ruby_mapping/controllers/property_cache.rb', line 110

def values_at(*key)
  generate_all_properties
  @properties.values_at(*key)
end