Class: Tilt::Fs::FileSystem::File

Inherits:
Object
  • Object
show all
Defined in:
lib/tilt/fs/file_system/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_real_path, new_data_path = nil) ⇒ File

Returns a new instance of File.



10
11
12
13
14
15
16
17
# File 'lib/tilt/fs/file_system/file.rb', line 10

def initialize(new_real_path, new_data_path = nil)
  @real_path = new_real_path
  @logger = FS_LOGGER
  @data_path = new_data_path
  @content = render_content

  logger.debug "file#new(): created, real_path = #{real_path}, data_path = #{data_path}"
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



8
9
10
# File 'lib/tilt/fs/file_system/file.rb', line 8

def content
  @content
end

#data_pathObject (readonly)

Returns the value of attribute data_path.



7
8
9
# File 'lib/tilt/fs/file_system/file.rb', line 7

def data_path
  @data_path
end

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/tilt/fs/file_system/file.rb', line 5

def logger
  @logger
end

#real_pathObject (readonly)

Returns the value of attribute real_path.



6
7
8
# File 'lib/tilt/fs/file_system/file.rb', line 6

def real_path
  @real_path
end

Instance Method Details

#attrObject



72
73
74
# File 'lib/tilt/fs/file_system/file.rb', line 72

def attr
  ::RFuse::Stat.file FS_DEFAULT_FILE_MODE, stat
end

#is_dir?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/tilt/fs/file_system/file.rb', line 54

def is_dir?
  false
end

#is_file?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/tilt/fs/file_system/file.rb', line 58

def is_file?
  true
end

#load_template_dataObject



34
35
36
37
38
39
40
41
42
# File 'lib/tilt/fs/file_system/file.rb', line 34

def load_template_data
  if data_path.nil? || (not ::FileTest.file? data_path)
    logger.debug "file#load_template_data(): no data"
    {}
  else
    logger.debug "file#load_template_data(): data_path = #{data_path}"
    ::YAML.load_file data_path
  end
end

#nameObject

e.g. “foo.txt.erb” => “foo.txt”



45
46
47
48
49
50
51
52
# File 'lib/tilt/fs/file_system/file.rb', line 45

def name
  res = ::Pathname.new(real_path).basename.to_s
  if /\.erb$/ === res
    res.gsub /\.erb$/, ""
  else
    res
  end
end

#render_contentObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tilt/fs/file_system/file.rb', line 19

def render_content
  if ::Tilt.templates_for(real_path).empty?
    ::File.read real_path
  else
    template_data = load_template_data
    template = ::Tilt.new(real_path, nil)
    begin
      template.render self, template_data
    rescue => e
      logger.warn "file#render_content(): #{e}"
      raise "error on file#render_content()"
    end
  end
end

#statObject



62
63
64
65
66
67
68
69
70
# File 'lib/tilt/fs/file_system/file.rb', line 62

def stat
  {
      :uid => ::Process.uid,
      :gid => ::Process.gid,
      :atime => ::Time.now,
      :mtime => ::Time.now,
      :size => content.length,
  }
end