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.

string_functions.rb 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. require_relative '../function'
  2. module Dentaku
  3. module AST
  4. module StringFunctions
  5. class Left < Function
  6. def initialize(string, length)
  7. @string = string
  8. @length = length
  9. end
  10. def value(context={})
  11. string = @string.value(context).to_s
  12. length = @length.value(context)
  13. string[0, length]
  14. end
  15. end
  16. class Right < Function
  17. def initialize(string, length)
  18. @string = string
  19. @length = length
  20. end
  21. def value(context={})
  22. string = @string.value(context).to_s
  23. length = @length.value(context)
  24. string[length * -1, length] || string
  25. end
  26. end
  27. class Mid < Function
  28. def initialize(string, offset, length)
  29. @string = string
  30. @offset = offset
  31. @length = length
  32. end
  33. def value(context={})
  34. string = @string.value(context).to_s
  35. offset = @offset.value(context)
  36. length = @length.value(context)
  37. string[offset - 1, length].to_s
  38. end
  39. end
  40. class Len < Function
  41. def initialize(string)
  42. @string = string
  43. end
  44. def value(context={})
  45. string = @string.value(context).to_s
  46. string.length
  47. end
  48. end
  49. class Find < Function
  50. def initialize(needle, haystack)
  51. @needle = needle
  52. @haystack = haystack
  53. end
  54. def value(context={})
  55. needle = @needle.value(context)
  56. needle = needle.to_s unless needle.is_a?(Regexp)
  57. haystack = @haystack.value(context).to_s
  58. pos = haystack.index(needle)
  59. pos && pos + 1
  60. end
  61. end
  62. class Substitute < Function
  63. def initialize(original, search, replacement)
  64. @original = original
  65. @search = search
  66. @replacement = replacement
  67. end
  68. def value(context={})
  69. original = @original.value(context).to_s
  70. search = @search.value(context)
  71. search = search.to_s unless search.is_a?(Regexp)
  72. replacement = @replacement.value(context).to_s
  73. original.sub(search, replacement)
  74. end
  75. end
  76. class Concat < Function
  77. def initialize(left, right)
  78. @left = left
  79. @right = right
  80. end
  81. def value(context={})
  82. left = @left.value(context).to_s
  83. right = @right.value(context).to_s
  84. left + right
  85. end
  86. end
  87. end
  88. end
  89. end
  90. Dentaku::AST::Function.register_class(:left, Dentaku::AST::StringFunctions::Left)
  91. Dentaku::AST::Function.register_class(:right, Dentaku::AST::StringFunctions::Right)
  92. Dentaku::AST::Function.register_class(:mid, Dentaku::AST::StringFunctions::Mid)
  93. Dentaku::AST::Function.register_class(:len, Dentaku::AST::StringFunctions::Len)
  94. Dentaku::AST::Function.register_class(:find, Dentaku::AST::StringFunctions::Find)
  95. Dentaku::AST::Function.register_class(:substitute, Dentaku::AST::StringFunctions::Substitute)
  96. Dentaku::AST::Function.register_class(:concat, Dentaku::AST::StringFunctions::Concat)