set.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. # tags['a', /^[bc]$/] # => #<Set: {"a:b=x", "a:b=y", "a:c=z"}>
  80. # tags[/^a:[bc]$/] # => #<Set: {"a:b=x", "a:b=y", "a:c=z"}>
  81. #
  82. # @param namespace_or_namespace_and_predicate [String, Regexp] the namespace to retrieve or the namespace
  83. # and predicate to retreive combined in a string separated by +':'+
  84. # @param predicate [String, Regexp, nil] the predicate to retreive
  85. #
  86. # @return [::Set<Tag>] the machines tags that have the given namespace or namespace and predicate
  87. #
  88. def [](namespace_or_namespace_and_predicate, predicate = nil)
  89. case namespace_or_namespace_and_predicate
  90. when Regexp
  91. tags = @machine_tags.select do |machine_tag|
  92. machine_tag.namespace =~ namespace_or_namespace_and_predicate ||
  93. machine_tag.namespace_and_predicate =~ namespace_or_namespace_and_predicate
  94. end
  95. case predicate
  96. when nil
  97. when Regexp
  98. tags.select! { |machine_tag| machine_tag.predicate =~ predicate }
  99. else
  100. raise ArgumentError, "Invalid machine tag predicate: #{predicate.inspect}" unless predicate =~ /^#{PREFIX}$/
  101. tags.select! { |machine_tag| machine_tag.predicate == predicate }
  102. end
  103. ::Set.new tags
  104. else
  105. if namespace_or_namespace_and_predicate =~ /^#{PREFIX}$/
  106. namespace = namespace_or_namespace_and_predicate
  107. unless predicate
  108. @tags_by_namespace[namespace]
  109. else
  110. case predicate
  111. when Regexp
  112. ::Set.new @tags_by_namespace[namespace].select { |machine_tag| machine_tag.predicate =~ predicate }
  113. else
  114. raise ArgumentError, "Invalid machine tag predicate: #{predicate.inspect}" unless predicate =~ /^#{PREFIX}$/
  115. @tags_by_namespace_and_predicate["#{namespace}:#{predicate}"]
  116. end
  117. end
  118. elsif namespace_or_namespace_and_predicate =~ /^#{NAMESPACE_AND_PREDICATE}$/
  119. namespace_and_predicate = namespace_or_namespace_and_predicate
  120. raise ArgumentError, "Separate predicate passed with namespace and predicate: #{namespace_and_predicate.inspect}, #{predicate.inspect}" if predicate
  121. @tags_by_namespace_and_predicate[namespace_and_predicate]
  122. else
  123. raise ArgumentError, "Invalid machine tag namespace and/or predicate: #{namespace_or_namespace_and_predicate.inspect}, #{predicate.inspect}"
  124. end
  125. end
  126. end
  127. end
  128. end