Class: XML::MappingExtensions::TypesafeEnumNode

Inherits:
NodeBase
  • Object
show all
Defined in:
lib/xml/mapping_extensions/typesafe_enum_node.rb

Overview

Base class for single-attribute nodes with values that extend TypesafeEnum::Base

Usage: # for node class MyEnum typesafe_enum_node :my_enum, '@my_enum', default_value: nil, class: MyEnum

Instance Method Summary collapse

Methods inherited from NodeBase

#extract_attr_value, #set_attr_value

Constructor Details

#initialize(*args) ⇒ TypesafeEnumNode

Parameters:

  • :class

    the enum class



17
18
19
20
21
# File 'lib/xml/mapping_extensions/typesafe_enum_node.rb', line 17

def initialize(*args)
  super
  @enum_class = @options[:class]
  fail ArgumentError, "No :class found for TypesafeEnumNode #{@attrname} of #{@owner}" unless @enum_class
end

Instance Method Details

#to_value(xml_text) ⇒ TypesafeEnum::Base

Converts an enum value or value string to an enum instance

Parameters:

  • xml_text

    the enum value or value string

Returns:

  • (TypesafeEnum::Base)

    an instance of the enum class declared in the initializer



26
27
28
29
30
# File 'lib/xml/mapping_extensions/typesafe_enum_node.rb', line 26

def to_value(xml_text)
  enum_instance = @enum_class.find_by_value(xml_text)
  enum_instance = @enum_class.find_by_value_str(xml_text) unless enum_instance
  enum_instance
end

#to_xml_text(enum_instance) ⇒ String

Converts an enum value or value string to an enum instance

Parameters:

  • enum_instance (TypesafeEnum::Base)

    an instance of the enum class declared in the initializer

Returns:

  • (String)

    the enum value as a string



35
36
37
# File 'lib/xml/mapping_extensions/typesafe_enum_node.rb', line 35

def to_xml_text(enum_instance)
  enum_instance.value.to_s if enum_instance
end