Class: Mongoid::PathExtension

Inherits:
String
  • Object
show all
Defined in:
lib/mongoid/path_extension.rb,
lib/mongoid/path_extension/version.rb

Constant Summary collapse

VERSION =
'0.1.4'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ PathExtension

Returns a new instance of PathExtension.



5
6
7
8
# File 'lib/mongoid/path_extension.rb', line 5

def initialize(str)
  super str.to_s
  @str = str.to_s
end

Class Method Details

.demongoize(value) ⇒ Object



51
52
53
# File 'lib/mongoid/path_extension.rb', line 51

def demongoize(value)
  PathExtension.new(value)
end

.evolve(value) ⇒ Object



62
63
64
65
66
67
# File 'lib/mongoid/path_extension.rb', line 62

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

.mongoize(value) ⇒ Object



55
56
57
58
59
60
# File 'lib/mongoid/path_extension.rb', line 55

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

Instance Method Details

#absoluteObject



23
24
25
# File 'lib/mongoid/path_extension.rb', line 23

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

#ancestor_pathsObject



31
32
33
34
35
36
37
38
# File 'lib/mongoid/path_extension.rb', line 31

def ancestor_paths
  return unless has_parent?
  res = []
  components[0..-2].each_with_index do |component, index|
    res << components[0..index].join('/')
  end
  res
end

#componentsObject



10
11
12
13
# File 'lib/mongoid/path_extension.rb', line 10

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

#has_parent?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/mongoid/path_extension.rb', line 27

def has_parent?
  components.length > 1
end

#parent_pathObject



40
41
42
43
# File 'lib/mongoid/path_extension.rb', line 40

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


45
46
47
48
# File 'lib/mongoid/path_extension.rb', line 45

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


19
20
21
# File 'lib/mongoid/path_extension.rb', line 19

def permalink
  components.last
end

#rootObject



15
16
17
# File 'lib/mongoid/path_extension.rb', line 15

def root
  components.first
end