Class: Servel::EntryFactory
- Inherits:
-
Object
- Object
- Servel::EntryFactory
- Extended by:
- Instrumentation
- Defined in:
- lib/servel/entry_factory.rb
Constant Summary collapse
- IMAGE_EXTS =
%w(.jpg .jpeg .png .gif)- VIDEO_EXTS =
%w(.webm .mp4 .mkv)- AUDIO_EXTS =
%w(.mp3 .m4a .wav)- TEXT_EXTS =
%w(.txt)
Class Method Summary collapse
Instance Method Summary collapse
- #entry ⇒ Object
- #icon ⇒ Object
-
#initialize(path) ⇒ EntryFactory
constructor
A new instance of EntryFactory.
- #listing_classes ⇒ Object
- #media_type ⇒ Object
- #size ⇒ Object
- #type ⇒ Object
Methods included from Instrumentation
Constructor Details
#initialize(path) ⇒ EntryFactory
Returns a new instance of EntryFactory.
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/servel/entry_factory.rb', line 48 def initialize(path) @path_basename = path.basename.to_s @path_extname = path.extname.to_s stat = path.stat @path_mtime = stat.mtime @path_size = stat.size @path_ftype = stat.ftype.intern @path_directory = @path_ftype == :directory @path_file = @path_ftype == :file end |
Class Method Details
.for(path) ⇒ Object
42 43 44 45 46 |
# File 'lib/servel/entry_factory.rb', line 42 def self.for(path) new(path).entry rescue Errno::EACCES nil end |
.home(href) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/servel/entry_factory.rb', line 9 def self.home(href) Servel::Entry.new( ftype: :directory, type: "Dir", listing_classes: "home directory", icon: "🏠", href: href, name: "Listings Home" ) end |
.parent(href) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/servel/entry_factory.rb', line 31 def self.parent(href) Servel::Entry.new( ftype: :directory, type: "Dir", listing_classes: "parent directory", icon: "⬆️", href: href, name: "Parent Directory" ) end |
.top(href) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/servel/entry_factory.rb', line 20 def self.top(href) Servel::Entry.new( ftype: :directory, type: "Dir", listing_classes: "top directory", icon: "🔝", href: href, name: "Top Directory" ) end |
Instance Method Details
#entry ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/servel/entry_factory.rb', line 61 def entry Servel::Entry.new( ftype: @path_ftype, type: type, media_type: media_type, listing_classes: listing_classes, icon: icon, href: @path_basename, name: @path_basename, size: size, mtime: @path_mtime ) end |
#icon ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/servel/entry_factory.rb', line 111 def icon return "📁" if @path_directory case media_type when :video "🎞️" when :image "🖼️" when :audio "🔊" else "📝" end end |
#listing_classes ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/servel/entry_factory.rb', line 102 def listing_classes klasses = [] klasses << "file" if @path_file klasses << "directory" if @path_directory klasses << "media" if media_type klasses << media_type if media_type klasses.join(" ") end |
#media_type ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/servel/entry_factory.rb', line 85 def media_type return nil unless @path_file && @path_extname case @path_extname.downcase when *IMAGE_EXTS :image when *VIDEO_EXTS :video when *AUDIO_EXTS :audio when *TEXT_EXTS :text else nil end end |
#size ⇒ Object
125 126 127 |
# File 'lib/servel/entry_factory.rb', line 125 def size @path_directory ? nil : @path_size end |
#type ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/servel/entry_factory.rb', line 75 def type if @path_directory "Dir" elsif @path_file @path_extname.sub(/^\./, "") else "" end end |