Class: FactDb::Temporal::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/fact_db/temporal/query_builder.rb

Overview

A scoped query builder for temporal queries. Allows chaining: facts.at(“2024-01-15”).query(“Paula’s role”)

Examples:

Basic usage

facts.at("2024-01-15").query("Paula's role", format: :cypher)
facts.at("2024-01-15").facts_for(entity_id)
facts.at("2024-01-15").compare_to("2024-06-15")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(facts, date) ⇒ QueryBuilder

Initialize with a Facts instance and date

Parameters:

  • facts (FactDb::Facts)

    The Facts instance

  • date (Date)

    The point in time



20
21
22
23
# File 'lib/fact_db/temporal/query_builder.rb', line 20

def initialize(facts, date)
  @facts = facts
  @date = date
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



14
15
16
# File 'lib/fact_db/temporal/query_builder.rb', line 14

def date
  @date
end

Instance Method Details

#compare_to(other_date, topic: nil) ⇒ Hash

Compare this date to another

Parameters:

  • other_date (Date, String)

    The date to compare to

  • topic (String, nil) (defaults to: nil)

    Optional topic to compare

Returns:

  • (Hash)

    Differences with :added, :removed, :unchanged keys



56
57
58
# File 'lib/fact_db/temporal/query_builder.rb', line 56

def compare_to(other_date, topic: nil)
  @facts.diff(topic, from: @date, to: other_date)
end

#facts(format: :json, **options) ⇒ Array, ...

Get all facts valid at this date

Parameters:

  • format (Symbol) (defaults to: :json)

    Output format

Returns:

  • (Array, String, Hash)

    Results



38
39
40
# File 'lib/fact_db/temporal/query_builder.rb', line 38

def facts(format: :json, **options)
  @facts.facts_at(@date, format: format, **options)
end

#facts_for(entity_id, format: :json, **options) ⇒ Array, ...

Get facts for a specific entity at this date

Parameters:

  • entity_id (Integer)

    Entity ID

  • format (Symbol) (defaults to: :json)

    Output format

Returns:

  • (Array, String, Hash)

    Results



47
48
49
# File 'lib/fact_db/temporal/query_builder.rb', line 47

def facts_for(entity_id, format: :json, **options)
  @facts.facts_at(@date, entity: entity_id, format: format, **options)
end

#query(topic, format: :json, **options) ⇒ Array, ...

Execute a query at this point in time

Parameters:

  • topic (String)

    The query topic

  • format (Symbol) (defaults to: :json)

    Output format (:json, :triples, :cypher, :text, :prolog)

Returns:

  • (Array, String, Hash)

    Results at this point in time



30
31
32
# File 'lib/fact_db/temporal/query_builder.rb', line 30

def query(topic, format: :json, **options)
  @facts.query_facts(topic: topic, at: @date, format: format, **options)
end

#state_for(entity_id, format: :json) ⇒ Array

Get the timeline state at this date

Parameters:

  • entity_id (Integer)

    Entity ID

Returns:

  • (Array)

    Facts valid at this date for the entity



64
65
66
# File 'lib/fact_db/temporal/query_builder.rb', line 64

def state_for(entity_id, format: :json)
  @facts.facts_at(@date, entity: entity_id, format: format)
end