Fuel plugin to deploy Sensu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

function.rb 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. require_relative 'node'
  2. module Dentaku
  3. module AST
  4. class Function < Node
  5. def initialize(*args)
  6. @args = args
  7. end
  8. def dependencies(context={})
  9. @args.flat_map { |a| a.dependencies(context) }
  10. end
  11. def self.get(name)
  12. registry.fetch(function_name(name)) {
  13. fail ParseError, "Undefined function #{ name }"
  14. }
  15. end
  16. def self.register(name, type, implementation)
  17. function = Class.new(self) do
  18. def self.implementation=(impl)
  19. @implementation = impl
  20. end
  21. def self.implementation
  22. @implementation
  23. end
  24. def self.type=(type)
  25. @type = type
  26. end
  27. def self.type
  28. @type
  29. end
  30. def value(context={})
  31. args = @args.map { |a| a.value(context) }
  32. self.class.implementation.call(*args)
  33. end
  34. def type
  35. self.class.type
  36. end
  37. end
  38. function_class = name.to_s.capitalize
  39. Dentaku::AST.send(:remove_const, function_class) if Dentaku::AST.const_defined?(function_class)
  40. Dentaku::AST.const_set(function_class, function)
  41. function.implementation = implementation
  42. function.type = type
  43. registry[function_name(name)] = function
  44. end
  45. def self.register_class(name, function_class)
  46. registry[function_name(name)] = function_class
  47. end
  48. private
  49. def self.function_name(name)
  50. name.to_s.downcase
  51. end
  52. def self.registry
  53. @registry ||= {}
  54. end
  55. end
  56. end
  57. end