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.new
  @machine_tags = ::Set.new
  @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"}>

Parameters:

  • namespace_or_namespace_and_predicate (String)

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

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

    the predicate to retreive

Returns:

  • (::Set<Tag>)

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/machine_tag/set.rb', line 94

def [](namespace_or_namespace_and_predicate, predicate = nil)
  if namespace_or_namespace_and_predicate =~ /^#{PREFIX}$/
    namespace = namespace_or_namespace_and_predicate

    unless predicate
      @tags_by_namespace[namespace]
    else
      raise ArgumentError, "Invalid machine tag predicate: #{predicate.inspect}" unless predicate =~ /^#{PREFIX}$/
      @tags_by_namespace_and_predicate["#{namespace}:#{predicate}"]
    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]
  else
    raise ArgumentError, "Invalid machine tag namespace and/or predicate: #{namespace_or_namespace_and_predicate.inspect}, #{predicate.inspect}"
  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.new
    @tags_by_namespace[tag.namespace] << tag
    @tags_by_namespace_and_predicate[tag.namespace_and_predicate] ||= ::Set.new
    @tags_by_namespace_and_predicate[tag.namespace_and_predicate] << tag
  else
    @plain_tags << tag
  end

  self
end