Class: SmallVictories::SiteFile

Inherits:
Object
  • Object
show all
Defined in:
lib/smallvictories/site_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ SiteFile

Returns a new instance of SiteFile.



24
25
26
# File 'lib/smallvictories/site_file.rb', line 24

def initialize path
  @path = path
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/smallvictories/site_file.rb', line 3

def path
  @path
end

Class Method Details

.files_hash(folder_path = SmallVictories::Server::WEB_ROOT) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Method Details

#asset?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/smallvictories/site_file.rb', line 44

def asset?
  audio? or image? or video? or css? or js?
end

#audio?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/smallvictories/site_file.rb', line 52

def audio?
  file_type =~ /audio/ ? true : false
end

#css?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/smallvictories/site_file.rb', line 56

def css?
  extension == 'css'
end

#downloadable?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/smallvictories/site_file.rb', line 60

def downloadable?
  asset? || pdf?
end

#extensionObject



28
29
30
# File 'lib/smallvictories/site_file.rb', line 28

def extension
  File.extname(path).gsub(/^./,'')
end

#fileObject



32
33
34
# File 'lib/smallvictories/site_file.rb', line 32

def file
  File.open(path)
end

#file_typeObject



40
41
42
# File 'lib/smallvictories/site_file.rb', line 40

def file_type
  MIME::Types.type_for('css')
end

#html?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/smallvictories/site_file.rb', line 84

def html?
  extension == 'html'
end

#image?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/smallvictories/site_file.rb', line 64

def image?
  file_type =~ /image/ ? true : false
end

#js?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/smallvictories/site_file.rb', line 68

def js?
  extension == 'js'
end

#markdown?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/smallvictories/site_file.rb', line 88

def markdown?
  extension == 'md'
end

#media_asset?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/smallvictories/site_file.rb', line 48

def media_asset?
  audio? or image? or video?
end

#pdf?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/smallvictories/site_file.rb', line 96

def pdf?
  extension == 'pdf'
end

#readObject



36
37
38
# File 'lib/smallvictories/site_file.rb', line 36

def read
  file.read
end

#renderObject



76
77
78
# File 'lib/smallvictories/site_file.rb', line 76

def render
  read
end

#text?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/smallvictories/site_file.rb', line 80

def text?
  extension == 'txt'
end

#text_based_file?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/smallvictories/site_file.rb', line 100

def text_based_file?
  markdown? || text? || html? || webloc? || pdf?
end

#video?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/smallvictories/site_file.rb', line 72

def video?
  file_type =~ /video/ ? true : false
end

#webloc?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/smallvictories/site_file.rb', line 92

def webloc?
  extension == 'webloc' || extension.downcase == 'url'
end