Sha256: 0d5dafbdb7be8ab06296d49b7ac5619c9cb38d82fac847f7d6931bd085cfb069

Contents?: true

Size: 959 Bytes

Versions: 1

Compression:

Stored size: 959 Bytes

Contents

module Ro
  class Asset < ::String
    include Klass

    DEFAULT_IMAGE_PATTERNS = [
      /[.](webp|jpg|jpeg|png|gif|tif|tiff|svg)$/i
    ]

    def Asset.image_patterns
      @image_patterns ||= DEFAULT_IMAGE_PATTERNS.dup
    end

    attr_reader :path, :node, :relative_path, :name, :url

    def initialize(arg, *args)
      options = args.last.is_a?(Hash) ? args.pop : {}

      @path = Path.for(arg, *args)

      @node = options.fetch(:node) { Node.for(@path.split('/assets/').first) }

      @relative_path = @path.relative_to(@node.path)

      @name = @relative_path

      @url = @node.url_for(@relative_path)

      super(@path)
    end

    def image?
      @path.file? && Asset.image_patterns.any? { |pattern| pattern === @path.basename }
    end

    def src
      key = relative_path.parts
      subdir = key.size > 2 ? key[1] : nil
      is_src = subdir == 'src'

      return unless is_src

      Ro.render_src(path, node)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ro-4.2.0 lib/ro/asset.rb