Module: Athar::SQL

Defined in:
lib/athar/sql.rb

Defined Under Namespace

Classes: Render

Constant Summary collapse

GENERATORS_ROOT =
File.expand_path("../generators/athar", __dir__)
INSTALL_FUNCTIONS_DIR =
File.join(GENERATORS_ROOT, "install", "functions")
MODEL_TRIGGERS_DIR =
File.join(GENERATORS_ROOT, "model", "triggers")
STATIC_FUNCTIONS =
%w[
  athar_filter_keys
  athar_capture_delete
].freeze
TEMPLATE_FUNCTIONS =
%w[
  athar_capture_truncate
].freeze
INSTALLED_FUNCTIONS =
(STATIC_FUNCTIONS + TEMPLATE_FUNCTIONS).freeze

Class Method Summary collapse

Class Method Details

.all_functions(locals = {}) ⇒ Object



35
36
37
# File 'lib/athar/sql.rb', line 35

def all_functions(locals = {})
  INSTALLED_FUNCTIONS.to_h { |name| [name, read_function(name, locals)] }
end

.function_signature(name) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/athar/sql.rb', line 39

def function_signature(name)
  case name
  when "athar_filter_keys" then "jsonb, text[]"
  when "athar_capture_delete", "athar_capture_truncate" then ""
  else
    raise ArgumentError, "unknown SQL function: #{name.inspect}"
  end
end

.read_function(name, locals = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/athar/sql.rb', line 24

def read_function(name, locals = {})
  if STATIC_FUNCTIONS.include?(name)
    File.read(File.join(INSTALL_FUNCTIONS_DIR, "#{name}.sql"))
  elsif TEMPLATE_FUNCTIONS.include?(name)
    path = File.join(INSTALL_FUNCTIONS_DIR, "#{name}.sql.erb")
    render(File.read(path), locals)
  else
    raise ArgumentError, "unknown SQL function: #{name.inspect}"
  end
end

.render(template, locals) ⇒ Object



48
49
50
# File 'lib/athar/sql.rb', line 48

def render(template, locals)
  Render.new(locals).result(template)
end