Class: MachineTag::Set

Inherits:
Set
  • Object
show all
Defined in:
lib/machine_tag/set.rb

Overview

Set of tags which can be machine tags.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enum = nil, &block) ⇒ Set

Creates a set of tags which can be machine tags. If String objects are passed in they will be converted to Tag.

Parameters:

  • enum (Enumerable<Tag, String>, nil) (defaults to: nil)

    the enumerable object of tags

  • block (Proc)

    the optional block to preprocess elements before inserting them



48
49
50
51
52
53
54
# File 'lib/machine_tag/set.rb', line 48

def initialize(enum = nil, &block)
  @plain_tags = ::Set[]
  @machine_tags = ::Set[]
  @tags_by_namespace = {}
  @tags_by_namespace_and_predicate = {}
  super
end

Instance Attribute Details

#machine_tags::Set<Tag> (readonly)

The tags in the set which are machine tags

Returns:

  • (::Set<Tag>)

    the tags in the set which are machine tags



40
41
42
# File 'lib/machine_tag/set.rb', line 40

def machine_tags
  @machine_tags
end

#plain_tags::Set<Tag> (readonly)

The tags in the set which are not machine tags

Returns:

  • (::Set<Tag>)

    the tags in the set which are not machine tags



34
35
36
# File 'lib/machine_tag/set.rb', line 34

def plain_tags
  @plain_tags
end

Instance Method Details

#[](namespace_or_namespace_and_predicate, predicate = nil) ⇒ ::Set<Tag>

Retrieves machine tags in the Set with a matching namespace or namespace and predicate.

Examples:

tags = MachineTag::Set['a:b=x', 'a:b=y', 'a:c=z']
tags['a']           # => #<Set: {"a:b=x", "a:b=y", "a:c=z"}>
tags['a', 'b']      # => #<Set: {"a:b=x", "a:b=y"}>
tags['a:c']         # => #<Set: {"a:c=z"}>
tags['a', /^[bc]$/] # => #<Set: {"a:b=x", "a:b=y", "a:c=z"}>
tags[/^a:[bc]$/]    # => #<Set: {"a:b=x", "a:b=y", "a:c=z"}>

Parameters:

  • namespace_or_namespace_and_predicate (String, Regexp)

    the namespace to retrieve or the namespace and predicate to retreive combined in a string separated by ‘:’

  • predicate (String, Regexp, nil) (defaults to: nil)

    the predicate to retreive

Returns:

  • (::Set<Tag>)

    the machines tags that have the given namespace or namespace and predicate



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/machine_tag/set.rb', line 96

def [](namespace_or_namespace_and_predicate, predicate = nil)
  case namespace_or_namespace_and_predicate
  when Regexp
    tags = @machine_tags.select do |machine_tag|
      machine_tag.namespace =~ namespace_or_namespace_and_predicate ||
        machine_tag.namespace_and_predicate =~ namespace_or_namespace_and_predicate
    end

    case predicate
    when nil
    when Regexp
      tags.select! { |machine_tag| machine_tag.predicate =~ predicate }
    else
      raise ArgumentError, "Invalid machine tag predicate: #{predicate.inspect}" unless predicate =~ /^#{PREFIX}$/
      tags.select! { |machine_tag| machine_tag.predicate == predicate }
    end

    ::Set.new tags
  else
    if namespace_or_namespace_and_predicate =~ /^#{PREFIX}$/
      namespace = namespace_or_namespace_and_predicate

      unless predicate
        @tags_by_namespace[namespace] || Set[]
      else
        case predicate
        when Regexp
          ::Set.new @tags_by_namespace[namespace].select { |machine_tag| machine_tag.predicate =~ predicate }
        else
          raise ArgumentError, "Invalid machine tag predicate: #{predicate.inspect}" unless predicate =~ /^#{PREFIX}$/
          @tags_by_namespace_and_predicate["#{namespace}:#{predicate}"] || Set[]
        end
      end
    elsif namespace_or_namespace_and_predicate =~ /^#{NAMESPACE_AND_PREDICATE}$/
      namespace_and_predicate = namespace_or_namespace_and_predicate
      raise ArgumentError, "Separate predicate passed with namespace and predicate: #{namespace_and_predicate.inspect}, #{predicate.inspect}" if predicate
      @tags_by_namespace_and_predicate[namespace_and_predicate] || Set[]
    else
      raise ArgumentError, "Invalid machine tag namespace and/or predicate: #{namespace_or_namespace_and_predicate.inspect}, #{predicate.inspect}"
    end
  end
end

#add(tag) ⇒ Set Also known as: <<

Adds a tag to the set of tags. If a String object is passed in it will be converted to Tag.

Parameters:

  • tag (Tag, String)

    the tag or string to add

Returns:

  • (Set)

    the tag set



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/machine_tag/set.rb', line 62

def add(tag)
  tag = Tag.new(tag) unless tag.is_a? Tag
  super(tag)

  if tag.machine_tag?
    @machine_tags << tag
    @tags_by_namespace[tag.namespace] ||= ::Set[]
    @tags_by_namespace[tag.namespace] << tag
    @tags_by_namespace_and_predicate[tag.namespace_and_predicate] ||= ::Set[]
    @tags_by_namespace_and_predicate[tag.namespace_and_predicate] << tag
  else
    @plain_tags << tag
  end

  self
end