pythonicrubyist / creek

Ruby library for parsing large Excel files.
http://rubygems.org/gems/creek
MIT License
386 stars 109 forks source link

Ability to add your own cells value encoders (converters) #118

Open DmitriyFirsov opened 1 year ago

DmitriyFirsov commented 1 year ago

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 commented 1 year ago

@pythonicrubyist What do you think of my proposal?