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

Inherits:
Pathname
  • Object
show all
Defined in:
lib/adva/static/import/source.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) ⇒ Source

Returns a new instance of Source.



12
13
14
15
16
17
18
19
# File 'lib/adva/static/import/source.rb', line 12

def initialize(path, root = nil)
  root ||= path.root if path.respond_to?(:root)
  @root = Pathname.new(root.to_s)

  path = path.to_s.gsub(root, '') if root
  path = path.to_s[1..-1] if path.to_s[0, 1] == '/'
  super(path)
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

Instance Method Details

#<=>(other) ⇒ Object



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

def <=>(other)
  path == 'index' ? -1 : other.path == 'index' ? 1 : path <=> other.path
end

#allObject



30
31
32
# File 'lib/adva/static/import/source.rb', line 30

def all
  @all ||= Dir[root.join(path).join("**/*.{#{TYPES.join(',')}}")].map { |path| Source.new(path, root) }
end

#basenameObject



47
48
49
# File 'lib/adva/static/import/source.rb', line 47

def basename
  @basename ||= super.to_s.sub(/\.\w+$/, '')
end

#directoryObject



43
44
45
# File 'lib/adva/static/import/source.rb', line 43

def directory
  @directory ||= self.class.new(dirname, root)
end

#dirnameObject



51
52
53
# File 'lib/adva/static/import/source.rb', line 51

def dirname
  @dirname ||= super.to_s.sub(/^.$/, '')
end

#filesObject



34
35
36
37
# File 'lib/adva/static/import/source.rb', line 34

def files
  files = path == 'index' ? directory.all : all
  files.reject { |path| path.basename == 'site' }.sort
end

#findObject



25
26
27
28
# File 'lib/adva/static/import/source.rb', line 25

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

#find_or_selfObject



21
22
23
# File 'lib/adva/static/import/source.rb', line 21

def find_or_self
  find or self
end

#full_pathObject



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

def full_path
  @full_path ||= root.join(self)
end

#parentsObject



67
68
69
70
71
72
73
74
# File 'lib/adva/static/import/source.rb', line 67

def parents
  @parents ||= begin
    parts = self.to_s.split('/')[0..-2]
    parts.inject([]) do |parents, part|
      parents << Source.new(parts[0..parents.size].join('/'), root)
    end
  end
end

#pathObject



55
56
57
# File 'lib/adva/static/import/source.rb', line 55

def path
  @_path ||= [dirname, basename].select(&:present?).join('/')
end

#root?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/adva/static/import/source.rb', line 39

def root?
  @_root ||= path == 'index' || full_path.to_s == root.to_s
end

#self_and_parentsObject



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

def self_and_parents
  parents << self
end