Class: Adva::Static::Import::Source::Path

Inherits:
Pathname
  • Object
show all
Defined in:
lib/adva/static/import/source/path.rb

Constant Summary collapse

TYPES =
['html', 'jekyll', 'yml']
EXTENSIONS =
TYPES.map { |type| ".#{type}" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, root = nil) ⇒ Path

Returns a new instance of Path.



11
12
13
14
# File 'lib/adva/static/import/source/path.rb', line 11

def initialize(path, root = nil)
  super(path)
  @root = self.class.new(root.to_s) if root && to_s != root.to_s
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



9
10
11
# File 'lib/adva/static/import/source/path.rb', line 9

def root
  @root
end

Instance Method Details

#<=>(other) ⇒ Object



73
74
75
# File 'lib/adva/static/import/source/path.rb', line 73

def <=>(other)
  root? ? -1 : other.root? ? 1 : to_s <=> other.to_s
end

#==(other) ⇒ Object



77
78
79
# File 'lib/adva/static/import/source/path.rb', line 77

def ==(other)
  to_s == other.to_s
end

#descendantsObject



41
42
43
# File 'lib/adva/static/import/source/path.rb', line 41

def descendants
  Dir[join("**/*.{#{TYPES.join(',')}}")].map { |child| join(child.gsub(/#{to_s}\/?/, '')) }
end

#filenameObject



49
50
51
# File 'lib/adva/static/import/source/path.rb', line 49

def filename
  basename.gsub(extname, '')
end

#find(name) ⇒ Object



68
69
70
71
# File 'lib/adva/static/import/source/path.rb', line 68

def find(name)
  path = Dir["#{join(name)}.{#{TYPES.join(',')}}"].first
  self.class.new(path, root) if path
end

#globObject



62
63
64
65
66
# File 'lib/adva/static/import/source/path.rb', line 62

def glob
  paths = Dir[join('**/*')].map { |child| join(child.gsub("#{to_s}/", '')) }
  paths = paths.map { |path| path.filename == 'index' ? path.parent : path }
  paths.sort
end

#gsub(pattern, replacement) ⇒ Object



58
59
60
# File 'lib/adva/static/import/source/path.rb', line 58

def gsub(pattern, replacement)
  self.class.new(to_s.gsub(pattern, replacement), root)
end

#join(other) ⇒ Object



53
54
55
56
# File 'lib/adva/static/import/source/path.rb', line 53

def join(other)
  self.class.new(super, root)
  # self.class.new(super(other.gsub(/^\//, '')), root)
end

#localObject



45
46
47
# File 'lib/adva/static/import/source/path.rb', line 45

def local
  gsub(root ? "#{root}/" : '', '')
end

#parentObject



24
25
26
# File 'lib/adva/static/import/source/path.rb', line 24

def parent
  self.class.new(super, @root || self)
end

#parentsObject



32
33
34
35
# File 'lib/adva/static/import/source/path.rb', line 32

def parents
  parts = to_s.split('/')[0..-2]
  parts.inject([]) { |parents, part| parents << self.class.new(parts[0..parents.size].join('/'), root) }
end

#root?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/adva/static/import/source/path.rb', line 16

def root?
  !@root || filename == 'index' && parent.root? || @root.to_s == to_s # i.e. this is the root path if no root is set
end

#self_and_descendantsObject



37
38
39
# File 'lib/adva/static/import/source/path.rb', line 37

def self_and_descendants
  [self] + descendants
end

#self_and_parentsObject



28
29
30
# File 'lib/adva/static/import/source/path.rb', line 28

def self_and_parents
  parents << self
end