Module: Sourcerer::Yaml::TagUtils

Defined in:
lib/sourcerer/yaml.rb

Overview

Utility methods for working with YAML-sourced data adorned with tags

Class Method Summary collapse

Class Method Details

.detag(value) ⇒ Object

Extracts the original value from a tagged data structure.

Parameters:

  • value (Object)

    The tagged value (a Hash) or any other value.

Returns:

  • (Object)

    The original value, or the value itself if not tagged.



124
125
126
# File 'lib/sourcerer/yaml.rb', line 124

def self.detag value
  value.is_a?(Hash) && value.key?('value') ? value['value'] : value
end

.tag?(value, tag) ⇒ Boolean

Checks if a value has a specific tag.

Parameters:

  • value (Object)

    The tagged value to check.

  • tag (String, Symbol)

    The tag to check for.

Returns:

  • (Boolean)

    ‘true` if the value has the specified tag, `false` otherwise.



141
142
143
# File 'lib/sourcerer/yaml.rb', line 141

def self.tag? value, tag
  tag_of(value)&.to_s == tag.to_s
end

.tag_of(value) ⇒ String?

Retrieves the tag from a tagged data structure.

Parameters:

  • value (Object)

    The tagged value (a Hash) or any other value.

Returns:

  • (String, nil)

    The tag string, or ‘nil` if not tagged.



132
133
134
# File 'lib/sourcerer/yaml.rb', line 132

def self.tag_of value
  value.is_a?(Hash) ? value['__tag__'] : nil
end