set.rb 4.4 KB

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