Sha256: 3639aa4e4173d3cb9b8acc4df08198991948d0d994e71eb013b59eb2dfbdfc26

Contents?: true

Size: 983 Bytes

Versions: 3

Compression:

Stored size: 983 Bytes

Contents

require 'mongoid'

module Mongoid
  class PathExtension < String
    def initialize(str)
      super str.to_s
      @str = str.to_s
    end

    def components
      return [] unless @str.present?
      @str.split('/')
    end

    def root
      components.first
    end

    def permalink
      components.last
    end

    def absolute
      ['/', @str].join
    end

    def has_parent?
      components.length > 1
    end

    def parent_path
      return unless has_parent?
      components[0..-2].join('/')
    end

    def parent_permalink
      return unless has_parent?
      components[-2]
    end

    class << self
      def demongoize(value)
        PathExtension.new(value)
      end

      def mongoize(value)
        case value
        when PathExtension then value.mongoize
        else value
        end
      end

      def evolve(value)
        case value
        when PathExtension then value.mongoize
        else value
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongoid-path_extension-0.1.2 lib/mongoid/path_extension.rb
mongoid-path_extension-0.1.1 lib/mongoid/path_extension.rb
mongoid-path_extension-0.1.0 lib/mongoid/path_extension.rb