tag.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. module MachineTag
  2. # A tag which can be a machine tag.
  3. #
  4. class Tag < String
  5. # The regular expression for matching a machine tag
  6. #
  7. # @return [Regexp] the regular expression for matching a machine tag
  8. MACHINE_TAG = /^(?<namespace>[a-zA-Z][a-zA-Z0-9_]*):(?<predicate>[a-zA-Z][a-zA-Z0-9_]*)=(?<value>.*)$/
  9. # The namespace portion of the machine tag, +nil+ if the tag is not a machine tag
  10. #
  11. # @return [String, nil] the namespace portion of the machine tag, +nil+ if the tag is not a machine tag
  12. #
  13. attr_reader :namespace
  14. # The predicate portion of the machine tag, +nil+ if the tag is not a machine tag
  15. #
  16. # @return [String, nil] the predicate portion of the machine tag, +nil+ if the tag is not a machine tag
  17. #
  18. attr_reader :predicate
  19. # The value portion of the machine tag, +nil+ if the tag is not a machine tag
  20. #
  21. # @return [String, nil] the value portion of the machine tag is not a machine tag
  22. #
  23. attr_reader :value
  24. # Creates a tag object which can be a machine tag.
  25. #
  26. # @param str [String] the tag string
  27. #
  28. def initialize(str)
  29. super
  30. if match = self.match(MACHINE_TAG)
  31. @namespace = match[:namespace]
  32. @predicate = match[:predicate]
  33. @value = match[:value]
  34. @value = $1 if @value =~ /^"(.*)"$/
  35. end
  36. end
  37. # Returns whether this tag is a machine tag or not.
  38. #
  39. # @return [Boolean] +true+ if this tag is a machine tag, otherwise +false+
  40. #
  41. def machine_tag?
  42. !!namespace
  43. end
  44. end
  45. end