Sha256: 2a927ca125f974d2746818746e7c759cce1ce1123e6f840ac71b12f54cbc5318

Contents?: true

Size: 1.26 KB

Versions: 6

Compression:

Stored size: 1.26 KB

Contents

module SmallCage
  class DocumentPath
    attr_reader :root, :uri, :path

    def initialize(root, path)
      @root = Pathname.new(root).realpath

      @path = Pathname.new(path)
      if @path.exist?
        @path = @path.realpath
      else
        @path = @path.cleanpath
      end

      fail "Illegal path: #{ path.to_s }" if @path.to_s[0...@root.to_s.length] != @root.to_s

      if @path == @root
        @uri = '/'
      else
        @uri = @path.to_s[@root.to_s.length .. -1]
      end
    end

    def smc?
      @path.extname == '.smc'
    end

    def outfile
      return nil unless smc?
      self.class.new(@root, @path.to_s[0 .. -5])
    end

    def outuri
      return nil unless smc?
      uri[0 .. -5]
    end

    def self.to_uri(root, path)
      new(root, path).uri
    end

    def self.create_with_uri(root, uri, base = nil)
      base ||= root
      if uri[0, 1] == '/'
        path = root + uri[1..-1] # absolute URI
      else
        path = base + uri # relative URI
      end
      new(root, path)
    end

    def self.add_smc_method(obj, value)
      obj.instance_eval do
        @__smallcage ||= {}
        @__smallcage[:smc] = value
      end

      def obj.smc
        @__smallcage.nil? ? nil : @__smallcage[:smc]
      end

      obj
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
smallcage-0.3.2 lib/smallcage/document_path.rb
smallcage-0.3.1 lib/smallcage/document_path.rb
smallcage-0.3.0 lib/smallcage/document_path.rb
smallcage-0.2.9 lib/smallcage/document_path.rb
smallcage-0.2.8 lib/smallcage/document_path.rb
smallcage-0.2.7 lib/smallcage/document_path.rb