Class: Deja::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/deja/configuration.rb

Overview

Holds everything host-specific so the gem itself stays ignorant of your app. You register at least one provider; cache_root has a sensible default, and the judge settings only matter if you use the ‘meet_requirements` matcher.

Constant Summary collapse

DEFAULT_CACHE_SUBPATH =

Default recorded-cache location, relative to project_root.

"spec/support/deja_cache"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



24
25
26
27
28
29
30
# File 'lib/deja/configuration.rb', line 24

def initialize
  @cache_root = nil
  @project_root = Pathname.new(Dir.pwd)
  @judge_attrs = {}
  @judge_client = nil
  @adapters = {}
end

Instance Attribute Details

#adaptersObject (readonly)

Directory display in error messages is computed relative to this.



11
12
13
# File 'lib/deja/configuration.rb', line 11

def adapters
  @adapters
end

#judge_attrsObject



32
33
34
# File 'lib/deja/configuration.rb', line 32

def judge_attrs
  @judge_attrs || {}
end

#project_rootObject

Directory display in error messages is computed relative to this.



11
12
13
# File 'lib/deja/configuration.rb', line 11

def project_root
  @project_root
end

Instance Method Details

#cache_rootObject

Where recorded cache files live. Defaults to project_root/spec/support/deja_cache.



37
38
39
# File 'lib/deja/configuration.rb', line 37

def cache_root
  @cache_root || project_root.join(DEFAULT_CACHE_SUBPATH)
end

#cache_root=(value) ⇒ Object

Accepts a String or Pathname (e.g. Rails.root.join(…)).



42
43
44
# File 'lib/deja/configuration.rb', line 42

def cache_root=(value)
  @cache_root = value && Pathname.new(value.to_s)
end

#judge_client(&block) ⇒ Object

How to build the client used by the ‘meet_requirements` judge. Required if you use that matcher — there is no default, so the judge’s auth/model is an explicit choice. The block returns a client.

c.judge_client { Anthropic::Client.new }

Called with no block, returns the configured proc (raises if unset).



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/deja/configuration.rb', line 70

def judge_client(&block)
  if block
    @judge_client = block
  else
    @judge_client || raise(Deja::Error, <<~MSG)
      Deja.configuration.judge_client is not set. The `meet_requirements`
      matcher needs a client to judge values against requirements. Set one in
      your Deja.configure block:

        Deja.configure do |c|
          c.judge_client { Anthropic::Client.new }
        end
    MSG
  end
end

#register(provider, install:, real_client: nil, as: provider) ⇒ Object

Register a provider adapter. ‘provider` is a built-in adapter name (today: `:anthropic`). `install` swaps your app’s client for Deja’s stub and runs in the example’s context (RSpec’s ‘allow` is available). `real_client` is an optional block building a live client; it defaults per provider. `as` names the registration when you want two of the same provider.

c.register :anthropic,
  install: ->(client) { allow(AnthropicClient).to receive(:client).and_return(client) },
  real_client: -> { Anthropic::Client.new(api_key: my_key) }


59
60
61
# File 'lib/deja/configuration.rb', line 59

def register(provider, install:, real_client: nil, as: provider)
  @adapters[as] = Deja::Adapters.build(provider, key: as, install:, real_client:)
end