Class: Dropsite::SiteFile
- Defined in:
- lib/dropsite/site_file.rb
Constant Summary collapse
- EXTENSIONS =
{ :image => %w[jpg jpeg gif png], :text => %w[txt], :pdf => %w[pdf] }
Instance Attribute Summary collapse
-
#abs_path ⇒ Object
Returns the value of attribute abs_path.
-
#size_in_bytes ⇒ Object
Returns the value of attribute size_in_bytes.
Attributes inherited from SiteItem
Attributes included from RenderHelper
Instance Method Summary collapse
- #file_type ⇒ Object
-
#initialize(rel_path, abs_path = nil, site = nil) ⇒ SiteFile
constructor
A new instance of SiteFile.
-
#size ⇒ Object
Human readable file size.
- #to_s ⇒ Object
Methods inherited from SiteItem
#<=>, #error, #name, #notice, #top_level?, #warning
Methods included from RenderHelper
#back_link, #each_parent_directory_link_tag, #get_binding, #image_tag, #javascript_include_tag, #link, #page_asset_image_tag, #page_assets_link_base, #parent_dir_name, #parent_directory_link_tag, #plugin_assets_link_base, #stylesheet_link_tag, #url_for
Constructor Details
#initialize(rel_path, abs_path = nil, site = nil) ⇒ SiteFile
Returns a new instance of SiteFile.
11 12 13 14 15 16 |
# File 'lib/dropsite/site_file.rb', line 11 def initialize(rel_path, abs_path=nil, site=nil) @path = rel_path @abs_path = abs_path @size_in_bytes = File.size(abs_path) if abs_path @site = site end |
Instance Attribute Details
#abs_path ⇒ Object
Returns the value of attribute abs_path.
9 10 11 |
# File 'lib/dropsite/site_file.rb', line 9 def abs_path @abs_path end |
#size_in_bytes ⇒ Object
Returns the value of attribute size_in_bytes.
9 10 11 |
# File 'lib/dropsite/site_file.rb', line 9 def size_in_bytes @size_in_bytes end |
Instance Method Details
#file_type ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/dropsite/site_file.rb', line 18 def file_type if File.extname(name).empty? :unknown else ext = File.extname(name).sub(/^\./, '') EXTENSIONS.each {|k, v| return k if v.include? ext} return :unknown end end |
#size ⇒ Object
Human readable file size
29 30 31 32 33 34 35 36 |
# File 'lib/dropsite/site_file.rb', line 29 def size return '' if !@size_in_bytes return '0 B' if @size_in_bytes == 0 units = %w{B KB MB GB TB} e = (Math.log(@size_in_bytes)/Math.log(1024)).floor s = "%.1f" % (@size_in_bytes.to_f / 1024 ** e) s.sub(/\.?0*$/, " #{units[e]}") end |
#to_s ⇒ Object
38 39 40 |
# File 'lib/dropsite/site_file.rb', line 38 def to_s "SiteFile(#{@path})" end |