Sha256: 2b78e439885eca19e5f32998ffaba819c432c34ade194ed7140967ffa7eac692
Contents?: true
Size: 1.68 KB
Versions: 2
Compression:
Stored size: 1.68 KB
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 # Creates a new {TypesafeEnumNode} # @param :class the enum class def initialize(*args) super @enum_class = @options[:class] fail ArgumentError, "No :class found for TypesafeEnumNode #{@attrname} of #{@owner}" unless @enum_class end # Converts an enum value or value string to an enum instance # @param xml_text [String, nil] the enum value or value string # @return [TypesafeEnum::Base, nil] an instance of the enum class declared in the initializer, # or nil if `xml_text` is nil def to_value(xml_text) return nil unless xml_text enum_instance = @enum_class.find_by_value(xml_text) enum_instance = @enum_class.find_by_value_str(xml_text) unless enum_instance fail ArgumentError, "No instance of enum class #{@enum_class.name} found for value '#{xml_text}'" unless enum_instance enum_instance end # Converts an enum value or value string to an enum instance # @param enum_instance [TypesafeEnum::Base, nil] an instance of the enum class declared in the initializer # @return [String, nil] the enum value as a string, or nil if `enum_instance` is nil 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.4.7 | lib/xml/mapping_extensions/typesafe_enum_node.rb |
xml-mapping_extensions-0.4.6 | lib/xml/mapping_extensions/typesafe_enum_node.rb |