Class: Locomotive::Mounter::Writer::FileSystem::Base

Inherits:
Object
  • Object
show all
Includes:
Utils::Output
Defined in:
lib/locomotive/mounter/writer/file_system/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mounting_point, runner) ⇒ Base

Returns a new instance of Base.



12
13
14
15
# File 'lib/locomotive/mounter/writer/file_system/base.rb', line 12

def initialize(mounting_point, runner)
  self.mounting_point = mounting_point
  self.runner         = runner
end

Instance Attribute Details

#mounting_pointObject

Returns the value of attribute mounting_point.



10
11
12
# File 'lib/locomotive/mounter/writer/file_system/base.rb', line 10

def mounting_point
  @mounting_point
end

#runnerObject

Returns the value of attribute runner.



10
11
12
# File 'lib/locomotive/mounter/writer/file_system/base.rb', line 10

def runner
  @runner
end

Instance Method Details

#create_folder(path) ⇒ Object

Helper method to create a folder from a relative path

Parameters:

  • path (String)

    The relative path



33
34
35
36
37
38
# File 'lib/locomotive/mounter/writer/file_system/base.rb', line 33

def create_folder(path)
  fullpath = File.join(self.target_path, path)
  unless File.exists?(fullpath)
    FileUtils.mkdir_p(fullpath)
  end
end

#open_file(path, mode = 'w', &block) ⇒ Object

Open a file described by the relative path. The file will be closed after the execution of the block.

Parameters:

  • path (String)

    The relative path

  • mode (String) (defaults to: 'w')

    The file mode (‘w’ by default)

  • &block (Lambda)

    The block passed to the File.open method



46
47
48
49
50
51
52
53
# File 'lib/locomotive/mounter/writer/file_system/base.rb', line 46

def open_file(path, mode = 'w', &block)
  # make sure the target folder exists
  self.create_folder(File.dirname(path))

  fullpath = File.join(self.target_path, path)

  File.open(fullpath, mode, &block)
end

#prepareObject

It should always be called before executing the write method. Writers inheriting from this class can overide it



20
21
22
# File 'lib/locomotive/mounter/writer/file_system/base.rb', line 20

def prepare
  self.output_title(:writing)
end

#target_pathObject



55
56
57
# File 'lib/locomotive/mounter/writer/file_system/base.rb', line 55

def target_path
  self.runner.target_path
end

#writeObject

Writers inheriting from this class must overide it



25
26
27
# File 'lib/locomotive/mounter/writer/file_system/base.rb', line 25

def write
  raise 'The write method has to be overridden'
end