tag.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. # The regular expression for matching the individual namespace and predicate portions of a machine tag
  25. #
  26. # @return [Regexp] the regular expression for matching the individual namespace and predicate
  27. # portions of a machine tag
  28. #
  29. PREFIX = /[a-z][a-z0-9_]*/i
  30. # The regular expression for matching the namespace and predicate portion of a machine tag
  31. #
  32. # @return [Regexp] the regular expression for matching the namespace and predicate portion of a machine tag
  33. #
  34. NAMESPACE_AND_PREDICATE = /(?<namespace>#{PREFIX}):(?<predicate>#{PREFIX})/
  35. # The regular expression for matching a machine tag
  36. #
  37. # @return [Regexp] the regular expression for matching a machine tag
  38. #
  39. MACHINE_TAG = /^(?<namespace_and_predicate>#{NAMESPACE_AND_PREDICATE})=(?<value>.*)$/
  40. # A tag which can be a machine tag.
  41. #
  42. class Tag < String
  43. # The namespace portion of the machine tag, +nil+ if the tag is not a machine tag
  44. #
  45. # @return [String, nil] the namespace portion of the machine tag, +nil+ if the tag is not a machine tag
  46. #
  47. attr_reader :namespace
  48. # The predicate portion of the machine tag, +nil+ if the tag is not a machine tag
  49. #
  50. # @return [String, nil] the predicate portion of the machine tag, +nil+ if the tag is not a machine tag
  51. #
  52. attr_reader :predicate
  53. # The namespace and predicate portion of the machine tag, +nil+ if the tag is not a machine tag
  54. #
  55. # @return [String, nil] the namespace and predicate portion of the machine tag, +nil+ if the tag
  56. # is not a machine tag
  57. #
  58. attr_reader :namespace_and_predicate
  59. # The value portion of the machine tag, +nil+ if the tag is not a machine tag
  60. #
  61. # @return [String, nil] the value portion of the machine tag is not a machine tag
  62. #
  63. attr_reader :value
  64. # Creates a tag object which can be a machine tag.
  65. #
  66. # @param str [String] the tag string
  67. #
  68. def initialize(str)
  69. super
  70. if match = self.match(MACHINE_TAG)
  71. @namespace_and_predicate = match[:namespace_and_predicate]
  72. @namespace = match[:namespace]
  73. @predicate = match[:predicate]
  74. @value = match[:value]
  75. @value = $1 if @value =~ /^"(.*)"$/
  76. end
  77. end
  78. # Creates a machine tag from a given namespace, predicate, and value.
  79. #
  80. # @param namespace [String] the namespace
  81. # @param predicate [String] the predicate
  82. # @param value [String, #to_s] the value
  83. #
  84. # @return [Tag] the machine tag
  85. #
  86. # @raise [ArgumentError] if the +namespace+ or +predicate+ are not in the correct format
  87. #
  88. def self.machine_tag(namespace, predicate, value)
  89. raise ArgumentError, "Invalid machine tag namespace: #{namespace.inspect}" unless namespace =~ /^#{PREFIX}$/
  90. raise ArgumentError, "Invalid machine tag predicate: #{predicate.inspect}" unless predicate =~ /^#{PREFIX}$/
  91. new("#{namespace}:#{predicate}=#{value}")
  92. end
  93. # Returns whether this tag is a machine tag or not.
  94. #
  95. # @return [Boolean] +true+ if this tag is a machine tag, otherwise +false+
  96. #
  97. def machine_tag?
  98. !!namespace
  99. end
  100. end
  101. end