Class: NotionRubyMapping::PropertyCache

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

Instance Method Summary collapse

Constructor Details

#initialize(json = {}) ⇒ PropertyCache

Returns a new instance of PropertyCache.



3
4
5
6
# File 'lib/notion_ruby_mapping/property_cache.rb', line 3

def initialize(json = {})
  @properties = {}
  @json = json
end

Instance Method Details

#[](key) ⇒ Property

Returns Property for key.

Parameters:

  • key (String)

Returns:



10
11
12
13
14
15
16
17
18
# File 'lib/notion_ruby_mapping/property_cache.rb', line 10

def [](key)
  ans = @properties[key]
  unless ans
    if @json && @json[key]
      @properties[key] = Property.create_from_json key, @json[key]
    end
  end
  @properties[key]
end

#add_property(property, will_update: false) ⇒ Object

Parameters:

  • property (Property)

    added Property

  • will_update (FalseClass) (defaults to: false)

    true if the property value will update to Notion



22
23
24
25
26
# File 'lib/notion_ruby_mapping/property_cache.rb', line 22

def add_property(property, will_update: false)
  @properties[property.name] = property
  property.will_update = true if will_update
  self
end

#create_jsonHash

Returns created json.

Returns:

  • (Hash)

    created json



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

def create_json
  @properties.each_with_object({}) do |(key, property), ans|
    if property.will_update
      ans["properties"] ||= {}
      ans["properties"][key] = property.create_json
    end
  end
end