Class: Notion::Query::Builder
- Inherits:
-
Object
- Object
- Notion::Query::Builder
- Defined in:
- lib/notion/query/builder.rb
Defined Under Namespace
Classes: Filter
Constant Summary collapse
- DEFAULT_VALUE =
Object.new.freeze
- OPERATORS =
%i[ equals does_not_equal contains does_not_contain starts_with ends_with greater_than less_than greater_than_or_equal_to less_than_or_equal_to before after on_or_before on_or_after is_empty is_not_empty past_week past_month past_year next_week next_month next_year ].freeze
- EMPTY_OBJECT_OPERATORS =
%i[past_week past_month past_year next_week next_month next_year].freeze
Instance Method Summary collapse
- #add(property, operator, value, type = nil) ⇒ Object
- #all_of(*filters) ⇒ Object
- #any_of(*filters) ⇒ Object
-
#initialize(schema: nil, strict: false) ⇒ Builder
constructor
A new instance of Builder.
- #order(property = nil, timestamp: nil, direction: :ascending) ⇒ Object
- #order_by_timestamp(timestamp, direction = :ascending) ⇒ Object
- #raw(filter) ⇒ Object
- #to_h ⇒ Object
- #where(property, type: nil) ⇒ Object
Constructor Details
#initialize(schema: nil, strict: false) ⇒ Builder
Returns a new instance of Builder.
16 17 18 19 20 21 |
# File 'lib/notion/query/builder.rb', line 16 def initialize(schema: nil, strict: false) @filters = [] @sorts = [] @schema = schema || {} @strict = strict end |
Instance Method Details
#add(property, operator, value, type = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/notion/query/builder.rb', line 27 def add(property, operator, value, type = nil) raise ArgumentError, "property is required" if property.nil? || property.to_s.empty? definition = @schema[property.to_s] || @schema[property.to_sym] raise UnknownPropertyError, "unknown property: #{property}" if @strict && !definition property_type = type || schema_type(definition) || :rich_text condition = { property: property.to_s, property_type.to_sym => { operator => value } } @filters << condition self end |
#all_of(*filters) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/notion/query/builder.rb', line 39 def all_of(*filters) raise ArgumentError, "all_of requires a filter" if filters.empty? @filters << { and: filters.map { |filter| filter_value(filter) } } self end |
#any_of(*filters) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/notion/query/builder.rb', line 46 def any_of(*filters) raise ArgumentError, "any_of requires a filter" if filters.empty? @filters << { or: filters.map { |filter| filter_value(filter) } } self end |
#order(property = nil, timestamp: nil, direction: :ascending) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/notion/query/builder.rb', line 65 def order(property = nil, timestamp: nil, direction: :ascending) raise ArgumentError, "provide either property or timestamp" if property.nil? == .nil? unless %i[ascending descending].include?(direction) raise ArgumentError, "direction must be ascending or descending" end @sorts << { direction: direction }.merge(property ? { property: property.to_s } : { timestamp: }) self end |
#order_by_timestamp(timestamp, direction = :ascending) ⇒ Object
75 76 77 |
# File 'lib/notion/query/builder.rb', line 75 def (, direction = :ascending) order(timestamp: , direction: direction) end |
#raw(filter) ⇒ Object
79 80 81 82 |
# File 'lib/notion/query/builder.rb', line 79 def raw(filter) @filters << filter self end |
#to_h ⇒ Object
84 85 86 87 88 89 |
# File 'lib/notion/query/builder.rb', line 84 def to_h result = {} result[:filter] = @filters.one? ? @filters.first : { and: @filters } unless @filters.empty? result[:sorts] = @sorts unless @sorts.empty? result end |