creek icon indicating copy to clipboard operation
creek copied to clipboard

Ability to add your own cells value encoders (converters)

Open DmitriyFirsov opened this issue 2 years ago • 1 comments

I propose to make the converter this way:

class Converter
      BUILD_IN_CONVERTERS = [
        # build-in converters from this https://github.com/pythonicrubyist/creek/blob/master/lib/creek/styles/converter.rb#L41
      ]

      def initialize(custom_converters: [])
        @_converters = custom_converters + BUILD_IN_CONVERTERS
      end

      def call(value, type, style, options = {})
        @_converters.each do |converter|
          if converter.is_support?(value, type, style, options)
            return converter.convert(value, type, style, options)
          end
        end

        raise StandardError, "Not found converter for cell #{options[:cell_name].to_s} with type #{type.to_s} and style #{style.to_s}"
      end
    end

Base value converter class

      class Base

        def is_support?(value, type, style, options)
          false
        end

        def convert(value, type, style, options)
          raise NotImplementedError
        end

      end

and pass to options cell name, sheet name, and raw sharing strings (in my case need to convert them to HTML with styles)

DmitriyFirsov avatar Jul 03 '23 06:07 DmitriyFirsov

@pythonicrubyist What do you think of my proposal?

DmitriyFirsov avatar Jul 20 '23 06:07 DmitriyFirsov