Module: Notion::Objects::PropertySchema
- Defined in:
- lib/notion/objects/property_schema.rb
Constant Summary collapse
- READ_ONLY =
%w[created_by created_time formula last_edited_by last_edited_time rollup unique_id].freeze
- DIRECT =
%w[number checkbox url email phone_number].freeze
Class Method Summary collapse
- .convert(properties, schema, strict: false) ⇒ Object
- .encode(type, value) ⇒ Object
- .iso8601(value) ⇒ Object
- .references(value) ⇒ Object
- .rich_text(value) ⇒ Object
- .validate_writable!(type) ⇒ Object
Class Method Details
.convert(properties, schema, strict: false) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/notion/objects/property_schema.rb', line 13 def convert(properties, schema, strict: false) properties.to_h do |name, value| definition = schema[name.to_s] || schema[name.to_sym] raise Query::UnknownPropertyError, "unknown property: #{name}" if strict && !definition [name, definition ? encode(definition.fetch("type", definition[:type]).to_s, value) : value] end end |
.encode(type, value) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/notion/objects/property_schema.rb', line 22 def encode(type, value) return value if value.is_a?(Hash) validate_writable!(type) return { type => value } if DIRECT.include?(type) case type when "title", "rich_text" then { type => rich_text(value) } when "date" then { "date" => { "start" => iso8601(value) } } when "select", "status" then { type => value.nil? ? nil : { "name" => value.to_s } } when "multi_select" then { type => Array(value).map { |item| { "name" => item.to_s } } } when "people", "relation" then { type => references(value) } else { type => value } end end |
.iso8601(value) ⇒ Object
52 53 54 |
# File 'lib/notion/objects/property_schema.rb', line 52 def iso8601(value) value.respond_to?(:iso8601) ? value.iso8601 : value.to_s end |
.references(value) ⇒ Object
42 43 44 |
# File 'lib/notion/objects/property_schema.rb', line 42 def references(value) Array(value).map { |item| { "id" => item.respond_to?(:id) ? item.id : item } } end |
.rich_text(value) ⇒ Object
46 47 48 49 50 |
# File 'lib/notion/objects/property_schema.rb', line 46 def rich_text(value) Array(value).map do |item| item.is_a?(Hash) ? item : { "type" => "text", "text" => { "content" => item.to_s } } end end |
.validate_writable!(type) ⇒ Object
38 39 40 |
# File 'lib/notion/objects/property_schema.rb', line 38 def validate_writable!(type) raise ValidationError, "#{type} is read-only" if READ_ONLY.include?(type) end |