Sha256: 52f937197282cc34c49a9b23ecdd43314af4f61a1e139316e4049c5ee82e9eff

Contents?: true

Size: 1.39 KB

Versions: 19

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module Bridgetown
  class LiquidRenderer
    class File
      def initialize(renderer, filename)
        @renderer = renderer
        @filename = filename
      end

      def parse(content)
        measure_time do
          @renderer.cache[@filename] ||= Liquid::Template.parse(
            content, line_numbers: true
          )
        end
        @template = @renderer.cache[@filename]

        self
      end

      def render(*args)
        measure_time do
          measure_bytes do
            measure_counts do
              @template.render(*args)
            end
          end
        end
      end

      # This method simply 'rethrows any error' before attempting to render the template.
      def render!(*args)
        measure_time do
          measure_bytes do
            measure_counts do
              @template.render!(*args)
            end
          end
        end
      end

      def warnings
        @template.warnings
      end

      private

      def measure_counts
        @renderer.increment_count(@filename)
        yield
      end

      def measure_bytes
        yield.tap do |str|
          @renderer.increment_bytes(@filename, str.bytesize)
        end
      end

      def measure_time
        before = Time.now
        yield
      ensure
        after = Time.now
        @renderer.increment_time(@filename, after - before)
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
bridgetown-core-0.19.3 lib/bridgetown-core/liquid_renderer/file.rb
bridgetown-core-0.19.2 lib/bridgetown-core/liquid_renderer/file.rb
bridgetown-core-0.19.1 lib/bridgetown-core/liquid_renderer/file.rb
bridgetown-core-0.19.0 lib/bridgetown-core/liquid_renderer/file.rb
bridgetown-core-0.18.6 lib/bridgetown-core/liquid_renderer/file.rb
bridgetown-core-0.18.5 lib/bridgetown-core/liquid_renderer/file.rb
bridgetown-core-0.18.4 lib/bridgetown-core/liquid_renderer/file.rb
bridgetown-core-0.18.3 lib/bridgetown-core/liquid_renderer/file.rb
bridgetown-core-0.18.2 lib/bridgetown-core/liquid_renderer/file.rb
bridgetown-core-0.18.1 lib/bridgetown-core/liquid_renderer/file.rb
bridgetown-core-0.18.0 lib/bridgetown-core/liquid_renderer/file.rb
bridgetown-core-0.17.1 lib/bridgetown-core/liquid_renderer/file.rb
bridgetown-core-0.17.0 lib/bridgetown-core/liquid_renderer/file.rb
bridgetown-core-0.16.0 lib/bridgetown-core/liquid_renderer/file.rb
bridgetown-core-0.16.0.beta2 lib/bridgetown-core/liquid_renderer/file.rb
bridgetown-core-0.16.0.beta1 lib/bridgetown-core/liquid_renderer/file.rb
bridgetown-core-0.15.0 lib/bridgetown-core/liquid_renderer/file.rb
bridgetown-core-0.15.0.beta4 lib/bridgetown-core/liquid_renderer/file.rb
bridgetown-core-0.15.0.beta3 lib/bridgetown-core/liquid_renderer/file.rb