Class: Bespoke::Export::Filter::FunctionCall

Inherits:
Object
  • Object
show all
Defined in:
lib/bespoke/export/filter/function_call.rb

Constant Summary collapse

EXTRACTION_REGEXP =
/\A(?<function>[^\(]+)\((?<args>[^\)]*)\)\z/
DEFAULT_CUSTOM_FUNCTIONS =
{
  'random_str' => ->(args) { "LOWER(HEX(RANDOMBLOB(#{ args[0].to_i })))" },
}.with_indifferent_access

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, custom_functions = {}) ⇒ FunctionCall

Returns a new instance of FunctionCall.



23
24
25
26
# File 'lib/bespoke/export/filter/function_call.rb', line 23

def initialize(string, custom_functions={})
  @string = string.strip
  @custom_functions = DEFAULT_CUSTOM_FUNCTIONS.merge(custom_functions)
end

Instance Attribute Details

#custom_functionsObject (readonly)

Returns the value of attribute custom_functions.



13
14
15
# File 'lib/bespoke/export/filter/function_call.rb', line 13

def custom_functions
  @custom_functions
end

Class Method Details

.extract_from(string, custom_functions = {}) ⇒ Object



19
20
21
# File 'lib/bespoke/export/filter/function_call.rb', line 19

def self.extract_from(string, custom_functions={})
  new(string, custom_functions).extract
end

.match?(string) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/bespoke/export/filter/function_call.rb', line 15

def self.match?(string)
  !!EXTRACTION_REGEXP.match(string.strip)
end

Instance Method Details

#argumentsObject

TODO: Make this type cast integers, booleans and possibly identifiers



41
42
43
# File 'lib/bespoke/export/filter/function_call.rb', line 41

def arguments
  @arguments ||= call_parts[:args].split(',').map(&:strip)
end

#extractObject



28
29
30
31
32
33
34
# File 'lib/bespoke/export/filter/function_call.rb', line 28

def extract
  if custom_function?
    Sequel.lit(compile_custom_function)
  else
    Sequel.function(function.upcase, *arguments)
  end
end

#functionObject



36
37
38
# File 'lib/bespoke/export/filter/function_call.rb', line 36

def function
  call_parts[:function]
end