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
  athar_mask_email
  athar_mask_partial
  athar_mask_hash
  athar_apply_masks
].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



39
40
41
# File 'lib/athar/sql.rb', line 39

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

.function_signature(name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/athar/sql.rb', line 43

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

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



28
29
30
31
32
33
34
35
36
37
# File 'lib/athar/sql.rb', line 28

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



54
55
56
# File 'lib/athar/sql.rb', line 54

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