Class: FileBrowser::Path

Inherits:
Object
  • Object
show all
Defined in:
app/models/file_browser/path.rb

Defined Under Namespace

Classes: NotFoundError

Constant Summary collapse

BASE =
'/'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Path

Returns a new instance of Path.



21
22
23
24
25
# File 'app/models/file_browser/path.rb', line 21

def initialize(name)
  validate(name)
  @name    = name
  @entries = get_entries
end

Class Attribute Details

.baseObject



9
10
11
# File 'app/models/file_browser/path.rb', line 9

def base
  @base ||= BASE
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



14
15
16
# File 'app/models/file_browser/path.rb', line 14

def entries
  @entries
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'app/models/file_browser/path.rb', line 14

def name
  @name
end

Class Method Details

.name_from_params(params) ⇒ Object



16
17
18
19
# File 'app/models/file_browser/path.rb', line 16

def self.name_from_params(params)
  name = params[:id]
  name == 'root' ? base : name
end

Instance Method Details

#baseObject



43
44
45
# File 'app/models/file_browser/path.rb', line 43

def base
  self.class.base
end

#file_listObject



33
34
35
36
37
# File 'app/models/file_browser/path.rb', line 33

def file_list
  list = Dir.glob("#{name}/*")
  list.unshift '..' unless root?
  list
end

#get_entriesObject



27
28
29
30
31
# File 'app/models/file_browser/path.rb', line 27

def get_entries
  file_list.map do |entry_name|
    Entry.new(entry_name)
  end
end

#root?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/models/file_browser/path.rb', line 39

def root?
  name == base
end

#validate(name) ⇒ Object

Raises:



47
48
49
# File 'app/models/file_browser/path.rb', line 47

def validate(name)
  raise NotFoundError unless File.exists?(name)
end