Class: FFFS::File

Inherits:
Object
  • Object
show all
Includes:
Node
Defined in:
lib/fffs/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Node

#path

Constructor Details

#initialize(name, content = '', parent = nil, filesystem = nil) ⇒ File

Returns a new instance of File.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fffs/file.rb', line 32

def initialize (name, content='', parent=nil, filesystem=nil)
  @filesystem = filesystem
  @parent     = parent

  @name = name
  @mode = 0644
  @mime = 'text/plain'

  @content = content.clone
  @content.force_encoding('ASCII-8BIT') rescue nil
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



29
30
31
# File 'lib/fffs/file.rb', line 29

def content
  @content
end

#filesystemObject

Returns the value of attribute filesystem.



27
28
29
# File 'lib/fffs/file.rb', line 27

def filesystem
  @filesystem
end

#mimeObject

Returns the value of attribute mime.



29
30
31
# File 'lib/fffs/file.rb', line 29

def mime
  @mime
end

#modeObject (readonly)

Returns the value of attribute mode.



30
31
32
# File 'lib/fffs/file.rb', line 30

def mode
  @mode
end

#nameObject

Returns the value of attribute name.



30
31
32
# File 'lib/fffs/file.rb', line 30

def name
  @name
end

#parentObject

Returns the value of attribute parent.



27
28
29
# File 'lib/fffs/file.rb', line 27

def parent
  @parent
end

Instance Method Details

#chmod(mode) ⇒ Object



50
51
52
# File 'lib/fffs/file.rb', line 50

def chmod (mode)
  @mode = mode
end

#execute(*args) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fffs/file.rb', line 79

def execute (*args)
  require 'tempfile'

  tmp = Tempfile.new('fffs')
  tmp.chmod 0700

  save(tmp)

  tmp.close

  Kernel.system(tmp.path, *args)
ensure
  tmp.unlink
end

#inspectObject



98
99
100
# File 'lib/fffs/file.rb', line 98

def inspect
  self.path
end

#save(file, mode = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fffs/file.rb', line 54

def save (file, mode=nil)
  result = true

  if file.is_a?(String)
    ::File.open(file, 'wb') {|f|
      result = f.write content

      begin
        f.chmod(mode || @mode)
      rescue Exception => e
      end
    }
  else
    result = file.write content
    file.flush

    begin
      file.chmod(mode || @mode)
    rescue Exception => e
    end
  end

  result
end

#to_sObject



94
95
96
# File 'lib/fffs/file.rb', line 94

def to_s
  @content
end