Class: MachineTag::Tag

Inherits:
String
  • Object
show all
Defined in:
lib/machine_tag/tag.rb

Overview

A tag which can be a machine tag.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Tag

Creates a tag object which can be a machine tag.

Parameters:

  • str (String)

    the tag string


76
77
78
79
80
81
82
83
84
85
# File 'lib/machine_tag/tag.rb', line 76

def initialize(str)
  super
  if match = self.match(MACHINE_TAG)
    @namespace_and_predicate = match[:namespace_and_predicate]
    @namespace = match[:namespace]
    @predicate = match[:predicate]
    @value = match[:value]
    @value = $1 if @value =~ /^"(.*)"$/
  end
end

Instance Attribute Details

#namespaceString? (readonly)

The namespace portion of the machine tag, nil if the tag is not a machine tag

Returns:

  • (String, nil)

    the namespace portion of the machine tag, nil if the tag is not a machine tag


51
52
53
# File 'lib/machine_tag/tag.rb', line 51

def namespace
  @namespace
end

#namespace_and_predicateString? (readonly)

The namespace and predicate portion of the machine tag, nil if the tag is not a machine tag

Returns:

  • (String, nil)

    the namespace and predicate portion of the machine tag, nil if the tag is not a machine tag


64
65
66
# File 'lib/machine_tag/tag.rb', line 64

def namespace_and_predicate
  @namespace_and_predicate
end

#predicateString? (readonly)

The predicate portion of the machine tag, nil if the tag is not a machine tag

Returns:

  • (String, nil)

    the predicate portion of the machine tag, nil if the tag is not a machine tag


57
58
59
# File 'lib/machine_tag/tag.rb', line 57

def predicate
  @predicate
end

#valueString? (readonly)

The value portion of the machine tag, nil if the tag is not a machine tag

Returns:

  • (String, nil)

    the value portion of the machine tag is not a machine tag


70
71
72
# File 'lib/machine_tag/tag.rb', line 70

def value
  @value
end

Class Method Details

.machine_tag(namespace, predicate, value) ⇒ Tag

Creates a machine tag from a given namespace, predicate, and value.

Parameters:

  • namespace (String)

    the namespace

  • predicate (String)

    the predicate

  • value (String, #to_s)

    the value

Returns:

  • (Tag)

    the machine tag

Raises:

  • (ArgumentError)

    if the namespace or predicate are not in the correct format


97
98
99
100
101
102
# File 'lib/machine_tag/tag.rb', line 97

def self.machine_tag(namespace, predicate, value)
  raise ArgumentError, "Invalid machine tag namespace: #{namespace.inspect}" unless namespace =~ /^#{PREFIX}$/
  raise ArgumentError, "Invalid machine tag predicate: #{predicate.inspect}" unless predicate =~ /^#{PREFIX}$/

  new("#{namespace}:#{predicate}=#{value}")
end

Instance Method Details

#machine_tag?Boolean

Returns whether this tag is a machine tag or not.

Returns:

  • (Boolean)

    true if this tag is a machine tag, otherwise false


108
109
110
# File 'lib/machine_tag/tag.rb', line 108

def machine_tag?
  !!namespace
end