Class: BetterAuth::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/better_auth/plugin.rb

Constant Summary collapse

FIELDS =
[
  :id,
  :init,
  :endpoints,
  :middlewares,
  :hooks,
  :schema,
  :migrations,
  :options,
  :rate_limit,
  :error_codes,
  :on_request,
  :on_response,
  :adapter
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}, **keywords) ⇒ Plugin

Returns a new instance of Plugin.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/better_auth/plugin.rb', line 29

def initialize(data = {}, **keywords)
  data = data.to_h if data.respond_to?(:to_h) && !data.is_a?(Hash)
  raw = normalize_hash((data || {}).merge(keywords))

  @id = raw[:id].to_s
  @init = raw[:init]
  @endpoints = normalize_endpoint_keys(raw[:endpoints] || {})
  @middlewares = normalize_middlewares(raw[:middlewares] || [])
  @hooks = normalize_hooks(raw[:hooks] || {})
  @schema = raw[:schema] || {}
  @migrations = raw[:migrations] || {}
  @options = raw[:options] || {}
  @rate_limit = Array(raw[:rate_limit])
  @error_codes = normalize_error_codes(raw)
  @on_request = raw[:on_request]
  @on_response = raw[:on_response]
  @adapter = raw[:adapter]
end

Class Method Details

.coerce(value) ⇒ Object



23
24
25
26
27
# File 'lib/better_auth/plugin.rb', line 23

def self.coerce(value)
  return value if value.is_a?(self)

  new(value || {})
end

Instance Method Details

#[](key) ⇒ Object



48
49
50
# File 'lib/better_auth/plugin.rb', line 48

def [](key)
  to_h[normalize_key(key)]
end

#dig(*keys) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/better_auth/plugin.rb', line 59

def dig(*keys)
  keys.reduce(to_h) do |value, key|
    return nil unless value.respond_to?(:[])

    value[normalize_key(key)] || value[key]
  end
end

#fetch(key, *default, &block) ⇒ Object



52
53
54
55
56
57
# File 'lib/better_auth/plugin.rb', line 52

def fetch(key, *default, &block)
  normalized = normalize_key(key)
  return to_h.fetch(normalized, *default, &block) if default.any? || block

  to_h.fetch(normalized)
end

#merge_options!(defaults) ⇒ Object



67
68
69
# File 'lib/better_auth/plugin.rb', line 67

def merge_options!(defaults)
  @options = deep_merge(@options, normalize_hash(defaults || {}))
end

#to_hObject



71
72
73
74
75
# File 'lib/better_auth/plugin.rb', line 71

def to_h
  FIELDS.each_with_object({}) do |field, result|
    result[field] = public_send(field)
  end
end