Class: Dire::Node

Inherits:
Object
  • Object
show all
Includes:
Module::Check, Module::Path, Module::Validity
Defined in:
lib/dire/node.rb

Direct Known Subclasses

Dir, File, Link, Other

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, root, validate: true) ⇒ Node



9
10
11
12
13
14
# File 'lib/dire/node.rb', line 9

def initialize node, root, validate: true
  @path = str2abs node
  @root = plant root

  validate! if validate
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/dire/node.rb', line 7

def path
  @path
end

#rootObject (readonly)

Returns the value of attribute root.



7
8
9
# File 'lib/dire/node.rb', line 7

def root
  @root
end

Instance Method Details

#get(path, validate: true) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dire/node.rb', line 16

def get path, validate: true
  path = expand path
  type = path.ftype rescue nil

  const = case type
    when 'directory' then Dir
    when 'file'      then File
    when 'link'      then Link

    else Other
  end

  const.new path, root, validate: validate
end

#parentObject



31
32
33
34
35
# File 'lib/dire/node.rb', line 31

def parent
  Dire::Dir.new absolute_path.join('..'), root
rescue Dire::Error::InvalidPath
  nil
end

#to_sObject



37
38
39
# File 'lib/dire/node.rb', line 37

def to_s
  relative_path.to_s
end

#typeObject



41
42
43
# File 'lib/dire/node.rb', line 41

def type
  path.ftype rescue 'other'
end