Class: Adva::Static::Import::Source
- Inherits:
-
Pathname
- Object
- Pathname
- Adva::Static::Import::Source
- Defined in:
- lib/adva/static/import/source.rb
Constant Summary collapse
- TYPES =
['html', 'jekyll', 'yml']
- EXTENSIONS =
TYPES.map { |type| ".#{type}" }
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #all ⇒ Object
- #basename ⇒ Object
- #directory ⇒ Object
- #dirname ⇒ Object
- #files ⇒ Object
- #find ⇒ Object
- #find_or_self ⇒ Object
- #full_path ⇒ Object
-
#initialize(path, root = nil) ⇒ Source
constructor
A new instance of Source.
- #parents ⇒ Object
- #path ⇒ Object
- #root? ⇒ Boolean
- #self_and_parents ⇒ Object
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
#root ⇒ Object (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 |
#all ⇒ Object
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 |
#basename ⇒ Object
47 48 49 |
# File 'lib/adva/static/import/source.rb', line 47 def basename @basename ||= super.to_s.sub(/\.\w+$/, '') end |
#directory ⇒ Object
43 44 45 |
# File 'lib/adva/static/import/source.rb', line 43 def directory @directory ||= self.class.new(dirname, root) end |
#dirname ⇒ Object
51 52 53 |
# File 'lib/adva/static/import/source.rb', line 51 def dirname @dirname ||= super.to_s.sub(/^.$/, '') end |
#files ⇒ Object
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 |
#find ⇒ Object
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_self ⇒ Object
21 22 23 |
# File 'lib/adva/static/import/source.rb', line 21 def find_or_self find or self end |
#full_path ⇒ Object
59 60 61 |
# File 'lib/adva/static/import/source.rb', line 59 def full_path @full_path ||= root.join(self) end |
#parents ⇒ Object
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 |
#path ⇒ Object
55 56 57 |
# File 'lib/adva/static/import/source.rb', line 55 def path @_path ||= [dirname, basename].select(&:present?).join('/') end |
#root? ⇒ 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_parents ⇒ Object
63 64 65 |
# File 'lib/adva/static/import/source.rb', line 63 def self_and_parents parents << self end |