Class: Ecoportal::API::GraphQL::Input::SearchConf

Inherits:
Object
  • Object
show all
Defined in:
lib/ecoportal/api/graphql/input/search_conf.rb,
lib/ecoportal/api/graphql/input/search_conf/ai_generator.rb

Overview

Builder for the searchConf hash argument used by pages, contractorEntities, personMembers, and other paginated queries.

Usage: conf = SearchConf.new .filter(SearchConf::Exact.new(:external_id, 'ABC')) .sort(:created_at, :desc)

# OR filter (find by one of several externalIds) conf = SearchConf.new.filter( SearchConf::Or.new( SearchConf::Exact.new(:external_id, '1111'), SearchConf::Exact.new(:external_id, '2222') ) )

# Parametrize — reuse the same structure with a different value base = SearchConf.new.filter(SearchConf::Exact.new(:external_id, 'X')) copy = base.with(external_id: 'Y')

# Pass to any query: org.pages(searchConf: conf.to_h, first: 10)

Filter operations (snake_case — CamelCase silently returns no results): SearchConf::Exact — exact_filter SearchConf::DateRange — date_filter SearchConf::Register — register_filter (org search only) SearchConf::And — and_filter (explicit AND group) SearchConf::Or — or_filter

Defined Under Namespace

Modules: AIGenerator, Composable, MembranePath Classes: And, DateRange, Exact, FieldRef, HasAny, Or, RawFilter, Register

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSearchConf


SearchConf builder



381
382
383
384
385
# File 'lib/ecoportal/api/graphql/input/search_conf.rb', line 381

def initialize
  @filters      = []
  @sorters      = []
  @search_query = nil
end

Class Method Details

.[](key) ⇒ Object



285
286
287
# File 'lib/ecoportal/api/graphql/input/search_conf.rb', line 285

def [](key)
  FieldRef.new(key.to_s)
end

.from_description(text, **opts) ⇒ Object

Build a SearchConf from a natural-language description using Claude. See SearchConf::AIGenerator for full documentation and options.

Usage: require 'anthropic' conf = SearchConf.from_description( "active pages in Safety register updated this month", register_id: 'REG_123' )



48
49
50
51
# File 'lib/ecoportal/api/graphql/input/search_conf.rb', line 48

def from_description(text, **opts)
  require_relative 'search_conf/ai_generator'
  AIGenerator.from_description(text, **opts)
end

.in_register(*ids) ⇒ Object



289
290
291
# File 'lib/ecoportal/api/graphql/input/search_conf.rb', line 289

def in_register(*ids)
  Register.new(*ids.flatten)
end

.raw(operation, **params) ⇒ Object



293
294
295
# File 'lib/ecoportal/api/graphql/input/search_conf.rb', line 293

def raw(operation, **params)
  RawFilter.new(operation, **params)
end

Instance Method Details

#as_json(_ = nil) ⇒ Object

Allow JSON serialisation directly (via json gem as_json protocol).



429
430
431
# File 'lib/ecoportal/api/graphql/input/search_conf.rb', line 429

def as_json(_ = nil)
  to_h
end

#filter(*filter_objs) ⇒ Object

Add one or more filter objects. Multiple calls accumulate (implicit AND at top level).



388
389
390
391
# File 'lib/ecoportal/api/graphql/input/search_conf.rb', line 388

def filter(*filter_objs)
  @filters.concat(filter_objs.flatten)
  self
end

#query(text) ⇒ Object

Set a full-text search string.



401
402
403
404
# File 'lib/ecoportal/api/graphql/input/search_conf.rb', line 401

def query(text)
  @search_query = text
  self
end

#sort(key, direction = :desc) ⇒ Object

Add a sorter. direction: :asc or :desc. Multiple sorters are supported (applied in order).



395
396
397
398
# File 'lib/ecoportal/api/graphql/input/search_conf.rb', line 395

def sort(key, direction = :desc)
  @sorters << { key: key.to_s, direction: direction.to_s }
  self
end

#to_hObject

Serialises to the Hash structure expected by searchConf arguments. Sorters: single object when one sorter (API convention), array when multiple.



420
421
422
423
424
425
426
# File 'lib/ecoportal/api/graphql/input/search_conf.rb', line 420

def to_h
  h = {}
  h[:filters] = @filters.map(&:to_h) unless @filters.empty?
  h[:sorters] = @sorters.length == 1 ? @sorters.first : @sorters unless @sorters.empty?
  h[:query]   = @search_query if @search_query
  h
end

#with(**substitutions) ⇒ Object

Returns a new SearchConf with filter values substituted. Substitution keys match the filter's field key (e.g. external_id:, gte:, lte:). Filters that don't recognise a key are returned unchanged.



409
410
411
412
413
414
415
416
# File 'lib/ecoportal/api/graphql/input/search_conf.rb', line 409

def with(**substitutions)
  clone.tap do |copy|
    copy.instance_variable_set(
      :@filters,
      @filters.map { |f| f.respond_to?(:substitute) ? f.substitute(substitutions) : f }
    )
  end
end