Class: NotionRubyMapping::PropertyCache

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

Overview

PropertyCache class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json = {}) ⇒ PropertyCache

Returns a new instance of PropertyCache.



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

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

Instance Attribute Details

#json=(value) ⇒ Object (writeonly)

Sets the attribute json

Parameters:

  • value

    the value to set the attribute json to.



11
12
13
# File 'lib/notion_ruby_mapping/property_cache.rb', line 11

def json=(value)
  @json = value
end

Instance Method Details

#[](key) ⇒ Property

Returns Property for key.

Parameters:

  • key (String)

Returns:



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

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

#add_property(property) ⇒ Object

Parameters:

  • property (Property)

    added Property



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

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

#each(&block) ⇒ Hash, Enumerator

Returns:

  • (Hash, Enumerator)


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

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

  generate_all_properties.each(&block)
end

#generate_all_propertiesObject



26
27
28
29
30
31
32
# File 'lib/notion_ruby_mapping/property_cache.rb', line 26

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

#property_values_jsonHash

Returns created json.

Returns:

  • (Hash)

    created json



48
49
50
51
52
53
54
55
# File 'lib/notion_ruby_mapping/property_cache.rb', line 48

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

#values_at(*key) ⇒ Array

Parameters:

  • key (Array)

Returns:

  • (Array)


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

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