Class: Tzispa::Rig::File

Inherits:
Object
  • Object
show all
Defined in:
lib/tzispa/rig/template.rb

Direct Known Subclasses

Template

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ File

Returns a new instance of File.



20
21
22
23
# File 'lib/tzispa/rig/template.rb', line 20

def initialize(file)
  @file = file
  @loaded = false
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



18
19
20
# File 'lib/tzispa/rig/template.rb', line 18

def content
  @content
end

#fileObject (readonly)

Returns the value of attribute file.



18
19
20
# File 'lib/tzispa/rig/template.rb', line 18

def file
  @file
end

#modifiedObject (readonly)

Returns the value of attribute modified.



18
19
20
# File 'lib/tzispa/rig/template.rb', line 18

def modified
  @modified
end

Instance Method Details

#create(content = nil) ⇒ Object



55
56
57
58
59
# File 'lib/tzispa/rig/template.rb', line 55

def create(content=nil)
  ::File.open(@file, "w") { |f|
    f.puts content
  }
end

#exist?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/tzispa/rig/template.rb', line 33

def exist?
  ::File.exist?(@file)
end

#load!Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tzispa/rig/template.rb', line 37

def load!
  begin
    raise NotFound.new("Template file '#{@file}' not found") unless exist?
    ::File.open(@file, 'r:UTF-8') { |f|
      @content = String.new
      @modified = f.mtime
      while line = f.gets
        @content << line
      end
      f.close
    }
    @loaded = true
  rescue Errno::ENOENT
    raise ReadError.new "Template file '#{@file}' could not be read"
  end
  self
end

#loaded?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/tzispa/rig/template.rb', line 25

def loaded?
  @loaded
end

#modified?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/tzispa/rig/template.rb', line 29

def modified?
  @modified != ::File.mtime(@file)
end