Sha256: 31acd163dc191886bec6f0f46e06bfc8930b0f65bce564b73ddb621303aae7a9

Contents?: true

Size: 824 Bytes

Versions: 2

Compression:

Stored size: 824 Bytes

Contents

require_relative 'node_base'

require 'typesafe_enum'

module XML
  module MappingExtensions

    # 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
    class TypesafeEnumNode < NodeBase
      def initialize(*args)
        super
        @enum_class = @options[:class]
      end

      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

      def to_xml_text(enum_instance)
        enum_instance.value.to_s if enum_instance
      end
    end
    ::XML::Mapping.add_node_class TypesafeEnumNode
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
xml-mapping_extensions-0.3.1 lib/xml/mapping_extensions/typesafe_enum_node.rb
xml-mapping_extensions-0.3.0 lib/xml/mapping_extensions/typesafe_enum_node.rb