Sha256: 2cb5e2109629a2157217fc8968555d9becdcb85c59351369ec933ab92f6e39ba

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

require 'open-uri'

module Subber::File
  class Base
    class << self
      attr_reader :parser_klass
      attr_reader :formatter_klass

      # @param value [Class] class to be used as parser
      #
      def parser(value = nil)
        @parser_klass = value unless value.nil?
      end

      # @param value [Class] class to be used as formatter
      #
      def formatter(value = nil)
        @formatter_klass = value unless value.nil?
      end

      # @param content [String]
      # @return [Subber::File::Base]
      #
      def from_content(content)
        subtitles = parser_klass.parse(content)
        new(subtitles: subtitles)
      end

      # @param path [String]
      # @return [Subber::File::Base]
      #
      def from_path(path)
        content = open(path).read
        from_content(content)
      end
    end

    attr_reader :subtitles

    # @param [Hash]
    # @param subtitles [Array<Subber::Subtitle>]
    #
    def initialize(subtitles: nil)
      @subtitles = subtitles
    end

    # @return [String]
    #
    def content
      @content ||= formatter.format(subtitles)
    end

    # @param path [String] Remote or local file path
    #
    def export(path)
      File.write(path, content)
    end

    private

    def formatter
      self.class.formatter_klass
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
subber-0.1.4 lib/subber/file/base.rb
subber-0.1.3 lib/subber/file/base.rb
subber-0.1.2 lib/subber/file/base.rb
subber-0.1.1 lib/subber/file/base.rb