Sha256: e177d2971fa9fe60b4cc62d0a7e270acadc607ee408aca0f5b3fe455b01f5030

Contents?: true

Size: 714 Bytes

Versions: 1

Compression:

Stored size: 714 Bytes

Contents

require_relative 'node_base'

require 'ruby-enum'

module XML
  module MappingExtensions

    # Base class for single-attribute nodes with `Ruby::Enum` values
    #
    # Usage:
    #   - extend this class
    #   - add an `ENUM_CLASS` constant whose value is the `Ruby::Enum` class
    #     to be mapped
    #   - call `::XML::Mapping.add_node_class` to add your new node class
    class EnumNodeBase < NodeBase

      def to_value(xml_text)
        enum_class = self.class::ENUM_CLASS
        enum_class.map do |_, enum|
          return enum if enum.value == xml_text
        end
      end

      def to_xml_text(value)
        value.respond_to?(:value) ? value.value : value.to_s
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xml-mapping_extensions-0.2.1 lib/xml/mapping_extensions/enum_node_base.rb