Module: SchemaGraphy::TagUtils

Defined in:
lib/schemagraphy/tag_utils.rb

Overview

A utility module for working with the custom tag data structure. The structure is a hash with 'value' and '__tag__' keys.

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.


11
12
13
# File 'lib/schemagraphy/tag_utils.rb', line 11

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.


28
29
30
# File 'lib/schemagraphy/tag_utils.rb', line 28

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.


19
20
21
# File 'lib/schemagraphy/tag_utils.rb', line 19

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