set.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. require 'set'
  24. module MachineTag
  25. # Set of tags which can be machine tags.
  26. #
  27. class Set < ::Set
  28. # The tags in the set which are machine tags
  29. #
  30. # @return [::Set<Tag>] the tags in the set whihc are machine tags
  31. #
  32. attr_reader :machine_tags
  33. # Creates a set of tags which can be machine tags. If String objects are passed in they will be
  34. # converted to {Tag}.
  35. #
  36. # @param enum [Enumerable<Tag, String>, nil] the enumerable object of tags
  37. # @param block [Proc] the optional block to preprocess elements before inserting them
  38. #
  39. def initialize(enum = nil, &block)
  40. @machine_tags = ::Set.new
  41. @tags_by_namespace = {}
  42. @tags_by_namespace_and_predicate = {}
  43. super
  44. end
  45. # Adds a tag to the set of tags. If a String object is passed in it will be converted to {Tag}.
  46. #
  47. # @param tag [Tag, String] the tag or string to add
  48. #
  49. # @return [Set] the tag set
  50. #
  51. def add(tag)
  52. tag = Tag.new(tag) unless tag.is_a? Tag
  53. super(tag)
  54. if tag.machine_tag?
  55. @machine_tags << tag
  56. @tags_by_namespace[tag.namespace] ||= ::Set.new
  57. @tags_by_namespace[tag.namespace] << tag
  58. @tags_by_namespace_and_predicate[tag.namespace_and_predicate] ||= ::Set.new
  59. @tags_by_namespace_and_predicate[tag.namespace_and_predicate] << tag
  60. end
  61. self
  62. end
  63. # Retrieves machine tags in the Set with a matching namespace or namespace and predicate.
  64. #
  65. # @example
  66. # tags = MachineTag::Set['a:b=x', 'a:b=y', 'a:c=z']
  67. # tags['a'] # => #<Set: {"a:b=x", "a:b=y", "a:c=z"}>
  68. # tags['a', 'b'] # => #<Set: {"a:b=x", "a:b=y"}>
  69. # tags['a:c'] # => #<Set: {"a:c=z"}>
  70. #
  71. # @param namespace_or_namespace_and_predicate [String] the namespace to retrieve or the namespace
  72. # and predicate to retreive combined in a string separated by +':'+
  73. # @param predicate [String, nil] the predicate to retreive
  74. #
  75. # @return [::Set<Tag>] the machines tags that have the given namespace or namespace and predicate
  76. #
  77. def [](namespace_or_namespace_and_predicate, predicate = nil)
  78. if namespace_or_namespace_and_predicate =~ /^#{PREFIX}$/
  79. namespace = namespace_or_namespace_and_predicate
  80. unless predicate
  81. @tags_by_namespace[namespace]
  82. else
  83. raise ArgumentError, "Invalid machine tag predicate: #{predicate.inspect}" unless predicate =~ /^#{PREFIX}$/
  84. @tags_by_namespace_and_predicate["#{namespace}:#{predicate}"]
  85. end
  86. elsif namespace_or_namespace_and_predicate =~ /^#{NAMESPACE_AND_PREDICATE}$/
  87. namespace_and_predicate = namespace_or_namespace_and_predicate
  88. raise ArgumentError, "Separate predicate passed with namespace and predicate: #{namespace_and_predicate.inspect}, #{predicate.inspect}" if predicate
  89. @tags_by_namespace_and_predicate[namespace_and_predicate]
  90. else
  91. raise ArgumentError, "Invalid machine tag namespace and/or predicate: #{namespace_or_namespace_and_predicate.inspect}, #{predicate.inspect}"
  92. end
  93. end
  94. end
  95. end