Class: NotionRubyMapping::PropertyCache
- Inherits:
-
Object
- Object
- NotionRubyMapping::PropertyCache
- Includes:
- Enumerable
- Defined in:
- lib/notion_ruby_mapping/property_cache.rb
Overview
PropertyCache class
Instance Attribute Summary collapse
-
#json ⇒ Object
writeonly
Sets the attribute json.
Instance Method Summary collapse
-
#[](key) ⇒ Property
Property for key.
- #add_property(property) ⇒ Object
- #each(&block) ⇒ Hash, Enumerator
- #generate_all_properties ⇒ Object
-
#initialize(json = {}) ⇒ PropertyCache
constructor
A new instance of PropertyCache.
-
#property_values_json ⇒ Hash
Created json.
- #values_at(*key) ⇒ Array
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
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.
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
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
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_properties ⇒ Object
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_json ⇒ Hash
Returns 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
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 |