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,
  :version,
  :client,
  :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.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/better_auth/plugin.rb', line 31

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

  @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] || {}
  @version = raw[:version]
  @client = stringify_hash(input[:client] || input["client"])
  @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



25
26
27
28
29
# File 'lib/better_auth/plugin.rb', line 25

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

  new(value || {})
end

Instance Method Details

#[](key) ⇒ Object



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

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

#dig(*keys) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/better_auth/plugin.rb', line 64

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



57
58
59
60
61
62
# File 'lib/better_auth/plugin.rb', line 57

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



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

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

#to_hObject



76
77
78
79
80
# File 'lib/better_auth/plugin.rb', line 76

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