anyway-config-coder について
型付きの anyway_config クラスでアプリケーション設定を管理するコーディングスキルです。環境変数の直接参照や Rails credentials に頼らず、デフォルト値、検証、型変換、環境別 YAML、テストを扱います。
主な機能
- 01型付き設定クラス
- 02環境変数の自動マッピング
- 03必須・条件付き検証
- 04型変換とネスト設定
- 05環境別 YAML 上書き
- 06設定テストのパターン
活用シーン
- 01API クライアント設定の一元管理
- 02デプロイ環境ごとの値の検証
- 03設定のデフォルト値と上書きのテスト
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ majesticlabs-dev/ majestic-marketplace --skill anyway-config-coderインストール後、Claude Code で/anyway-config-coderを入力して読み込まれたか確認します。
同梱ファイルreferences/advanced-patterns.md
詳細
Anyway Config Coder
環境変数を直接参照したり、Rails credentials を使ったりしないでください。 すべての設定を型付きの anyway_config クラスでラップします。
# WRONG
api_key = ENV["GEMINI_API_KEY"]
# RIGHT
class GeminiConfig < Anyway::Config
attr_config :api_key, timeout: 30
required :api_key
end
GeminiConfig.new.api_key
設定クラスのパターン
# config/configs/gemini_config.rb
class GeminiConfig < Anyway::Config
attr_config :api_key, model: "gemini-pro", timeout: 30, max_retries: 3
required :api_key
def configured? = api_key.present?
end
環境変数の自動マッピング:GeminiConfig -> GEMINI_API_KEY、GEMINI_MODEL など。プレフィックスを変更するには config_name :payment を指定します。
ネストされた設定ではハッシュをデフォルト値として使用し、ドット記法でアクセスします:AppConfig.new.database.host。
シングルトンパターン(推奨)
class GeminiConfig < Anyway::Config
attr_config :api_key, :model
class << self
def instance = @instance ||= new
end
end
GeminiConfig.instance.api_key
ディレクトリ構造
config/
├── configs/ # Ruby config classes
│ ├── gemini_config.rb
│ └── stripe_config.rb
└── settings/ # YAML overrides (optional)
└── gemini.yml
YAML ファイルでは default: &default を使用し、環境固有の設定(development:、test:、production:)で上書きします。
バリデーション
class StorageConfig < Anyway::Config
attr_config :bucket, :region, :access_key_id, :secret_access_key
required :bucket, :region
required :access_key_id, :secret_access_key, env: :production # Conditional
def validate!
super
raise_validation_error("Invalid region") unless %w[us-east-1 us-west-2 eu-west-1].include?(region)
end
end
型変換
class ApiConfig < Anyway::Config
# Automatic coercion
attr_config timeout: 30 # Integer
attr_config enabled: true # Boolean
attr_config rate: 1.5 # Float
# Coerce arrays from comma-separated strings
coerce_types allowed_origins: {
type: :string,
array: true
}
# ALLOWED_ORIGINS="example.com,other.com" => ["example.com", "other.com"]
end
設定のテスト
デフォルト値、必須項目のバリデーション、算出メソッドをテストします。テストでは with_env を使って上書きします:
RSpec.describe GeminiConfig do
it "requires api_key" do
expect { described_class.new(api_key: nil) }.to raise_error(Anyway::Config::ValidationError)
end
end
# spec/support/anyway_config.rb - global override
RSpec.configure do |config|
config.around(:each) do |example|
with_env("GEMINI_API_KEY" => "test-key") { example.run }
end
end
よくあるパターン
API クライアントの設定
接続ハッシュを構築する client_options ヘルパーを追加します:
class OpenAIConfig < Anyway::Config
attr_config :api_key, :organization_id, model: "gpt-4", max_tokens: 1000
required :api_key
def client_options = { access_token: api_key, organization_id: }.compact
end
複数プロバイダーの設定
プロバイダー固有のオプションには、述語メソッドと case を使用します:
class StorageConfig < Anyway::Config
attr_config provider: "local", bucket: nil, endpoint: nil
def s3? = provider == "s3"
def local? = provider == "local"
def service_options = case provider when "s3" then s3_options else local_options end
end
アンチパターン
| アンチパターン | 問題点 | 解決策 |
|---|---|---|
直接 ENV["KEY"] を参照 | 型安全性がなく、あちこちに散在する | 設定クラス |
どこでも ENV.fetch を使う | 重複し、バリデーションがない | 設定の一元管理 |
| Rails credentials | 複雑で、テストしにくい | anyway_config クラス |
| ハードコードされたシークレット | セキュリティリスク | 環境変数 |
| マジック文字列 | タイプミスが発生し、IDE のサポートがない | 設定定数 |
詳細なリファレンス
references/advanced-patterns.md- 動的設定、コールバック、継承
コメント
Markdown 対応