Sha256: 5c85c8312f3f9b14f45a96fc3f95edf3d41a986887e1abd6735469b07e0a6630

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

module ViewAssets
  class PathInfo < String
    def ==(pi)
      pi = PathInfo.new(pi) unless pi.kind_of?(PathInfo)

      self.rel.to_s == pi.rel.to_s
    end

    def initialize(path)
      replace path
    end

    def abs?
      !match(/^#{root}/).nil?
    end

    # alter path string
    #   'lib.js'
    # =>
    #   '/path/to/app/public/:type/file.js'
    def abs
      return self if abs?

      # PathInfo.new("#{root}/#{with_ext? ? self : "#{self}.#{ext}" }")
      PathInfo.new("#{root}/#{self}")
    end

    def abs!
      replace abs
    end

    def with_ext?
      !!match(/\.(#{JS_EXT}|#{CSS_EXT})$/)
    end

    # alter path string
    #   '/path/to/app/:dir/assets/:asset_type/file.js'
    #  =>
    #   'file.js'
    def rel
      return self unless abs?

      PathInfo.new(gsub(/^#{root}\//, ''))
    end

    def rel!
      replace rel
      self
    end

    def root
      "#{Rails.public_path}"
    end

    def lib?
      rel.match(/^lib\/(#{JS_PATH}|#{CSS_PATH})/)
    end

    def vendor?
      rel.match(/^vendor\/(#{JS_PATH}|#{CSS_PATH})/)
    end

    def app?
      rel.match(/^app\/(#{JS_PATH}|#{CSS_PATH})/)
    end

    # PathInfo.new('lib/javascripts/file.js').depth # => 0
    # PathInfo.new('app/javascripts/controller/action/file.js').depth # => 2
    # PathInfo.new('/path/to/my/app/public/lib/javascripts/file.js').depth # => 0
    def depth
      rel.count("/") - 2
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
view_assets-1.0.4 lib/view_assets/path_info.rb
view_assets-1.0.3 lib/view_assets/path_info.rb
view_assets-1.0.0 lib/view_assets/path_info.rb