class SmallVictories::SiteFile

Attributes

path[RW]

Public Class Methods

files_hash(folder_path=SmallVictories::Server::WEB_ROOT) click to toggle source
# File lib/smallvictories/site_file.rb, line 5
def self.files_hash folder_path=SmallVictories::Server::WEB_ROOT
  hash = {}

  Dir.glob(File.join(folder_path, '*')) do |path|
    next if path == '.' or path == '..'
    file = SiteFile.new path
    key = path.gsub(folder_path, '').gsub(/^\//, '').gsub('/', '.')
    key = key.gsub(/\.#{file.extension}$/, '')

    if Dir.exists?(path)
      hash[key] = SiteFile.files_hash(path)
    else
      hash[key] = file.downloadable? ? path : file.read
    end
  end

  hash
end
new(path) click to toggle source
# File lib/smallvictories/site_file.rb, line 24
def initialize path
  @path = path
end

Public Instance Methods

asset?() click to toggle source
# File lib/smallvictories/site_file.rb, line 48
def asset?
  audio? or image? or video? or css? or js?
end
audio?() click to toggle source
# File lib/smallvictories/site_file.rb, line 56
def audio?
  file_type =~ /audio/ ? true : false
end
css?() click to toggle source
# File lib/smallvictories/site_file.rb, line 60
def css?
  extension == 'css'
end
downloadable?() click to toggle source
# File lib/smallvictories/site_file.rb, line 64
def downloadable?
  asset? || pdf?
end
extension() click to toggle source
# File lib/smallvictories/site_file.rb, line 28
def extension
  File.extname(path).gsub(/^./,'')
end
file() click to toggle source
# File lib/smallvictories/site_file.rb, line 32
def file
  File.open(path)
end
file_type() click to toggle source
# File lib/smallvictories/site_file.rb, line 40
def file_type
  if ext = File.extname(path).split(".").last
    MIME::Types.type_for(ext).first.to_s
  else
    'text/html'
  end
end
html?() click to toggle source
# File lib/smallvictories/site_file.rb, line 88
def html?
  extension == 'html'
end
image?() click to toggle source
# File lib/smallvictories/site_file.rb, line 68
def image?
  file_type =~ /image/ ? true : false
end
js?() click to toggle source
# File lib/smallvictories/site_file.rb, line 72
def js?
  extension == 'js'
end
markdown?() click to toggle source
# File lib/smallvictories/site_file.rb, line 92
def markdown?
  extension == 'md'
end
media_asset?() click to toggle source
# File lib/smallvictories/site_file.rb, line 52
def media_asset?
  audio? or image? or video?
end
pdf?() click to toggle source
# File lib/smallvictories/site_file.rb, line 100
def pdf?
  extension == 'pdf'
end
read() click to toggle source
# File lib/smallvictories/site_file.rb, line 36
def read
  file.read
end
render() click to toggle source
# File lib/smallvictories/site_file.rb, line 80
def render
  read
end
text?() click to toggle source
# File lib/smallvictories/site_file.rb, line 84
def text?
  extension == 'txt'
end
text_based_file?() click to toggle source
# File lib/smallvictories/site_file.rb, line 104
def text_based_file?
  markdown? || text? || html? || webloc? || pdf?
end
video?() click to toggle source
# File lib/smallvictories/site_file.rb, line 76
def video?
  file_type =~ /video/ ? true : false
end
webloc?() click to toggle source
# File lib/smallvictories/site_file.rb, line 96
def webloc?
  extension == 'webloc' || extension.downcase == 'url'
end