tag.rb 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright (c) 2013 Douglas Thrift
  2. #
  3. # MIT License
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining
  6. # a copy of this software and associated documentation files (the
  7. # "Software"), to deal in the Software without restriction, including
  8. # without limitation the rights to use, copy, modify, merge, publish,
  9. # distribute, sublicense, and/or sell copies of the Software, and to
  10. # permit persons to whom the Software is furnished to do so, subject to
  11. # the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be
  14. # included in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. module MachineTag
  24. # A tag which can be a machine tag.
  25. #
  26. class Tag < String
  27. # The regular expression for matching the namespace and predicate portions of a machine tag
  28. #
  29. # @return [Regexp] the regular expression for matching the namespace and predicate portions of a machine tag
  30. #
  31. PREFIX = /[a-z][a-z0-9_]*/i
  32. # The regular expression for matching a machine tag
  33. #
  34. # @return [Regexp] the regular expression for matching a machine tag
  35. #
  36. MACHINE_TAG = /^(?<namespace>#{PREFIX}):(?<predicate>#{PREFIX})=(?<value>.*)$/
  37. # The namespace portion of the machine tag, +nil+ if the tag is not a machine tag
  38. #
  39. # @return [String, nil] the namespace portion of the machine tag, +nil+ if the tag is not a machine tag
  40. #
  41. attr_reader :namespace
  42. # The predicate portion of the machine tag, +nil+ if the tag is not a machine tag
  43. #
  44. # @return [String, nil] the predicate portion of the machine tag, +nil+ if the tag is not a machine tag
  45. #
  46. attr_reader :predicate
  47. # The value portion of the machine tag, +nil+ if the tag is not a machine tag
  48. #
  49. # @return [String, nil] the value portion of the machine tag is not a machine tag
  50. #
  51. attr_reader :value
  52. # Creates a tag object which can be a machine tag.
  53. #
  54. # @param str [String] the tag string
  55. #
  56. def initialize(str)
  57. super
  58. if match = self.match(MACHINE_TAG)
  59. @namespace = match[:namespace]
  60. @predicate = match[:predicate]
  61. @value = match[:value]
  62. @value = $1 if @value =~ /^"(.*)"$/
  63. end
  64. end
  65. # Creates a machine tag from a given namespace, predicate, and value.
  66. #
  67. # @param namespace [String] the namespace
  68. # @param predicate [String] the predicate
  69. # @param value [String, #to_s] the value
  70. #
  71. # @return [Tag] the machine tag
  72. #
  73. # @raise [ArgumentError] if the +namespace+ or +predicate+ are not in the correct format
  74. #
  75. def self.machine_tag(namespace, predicate, value)
  76. raise ArgumentError, "Invalid machine tag namespace: #{namespace.inspect}" unless namespace =~ /^#{PREFIX}$/
  77. raise ArgumentError, "Invalid machine tag predicate: #{predicate.inspect}" unless predicate =~ /^#{PREFIX}$/
  78. new("#{namespace}:#{predicate}=#{value}")
  79. end
  80. # Returns whether this tag is a machine tag or not.
  81. #
  82. # @return [Boolean] +true+ if this tag is a machine tag, otherwise +false+
  83. #
  84. def machine_tag?
  85. !!namespace
  86. end
  87. end
  88. end