Sha256: 1b00425490c3e1489ccc8d36f144be97918219326e0c29ce6e732154f93751d7

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

module Kanade
  module Converter
    class Symbol < Base
      Engine.register_converter!(self)

      def serialize(term, field_info)
        return nil if term.nil?
        map = field_info.options[:mapping]
        map_entry = map[term] unless map.nil?
        return map_entry unless map_entry.nil?
        term.to_s.upcase
      end

      def deserialize(term, field_info)
        # TODO WARNING: This is not thread safe!
        # Consider moving this into field definition instead
        build_field_info(field_info) if field_info.options[:mapping_inverted].nil?

        return nil if term.nil?
        inv = field_info.options[:mapping_inverted][term]
        return inv unless inv.nil?

        # TODO use contract!
        term.to_s.downcase.to_sym
      end

      configurable :symbol_contract, :camel_case

      private
        def build_field_info fi
          if fi.options[:mapping].nil?
            inverted = {}
          else
            inverted = fi.options[:mapping].invert
          end

          fi.options[:mapping_inverted] = inverted
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kanade-0.1.1 lib/kanade/converter/symbol.rb
kanade-0.1.0 lib/kanade/converter/symbol.rb