Class: Tobacco::Smoker

Inherits:
Object
  • Object
show all
Defined in:
lib/tobacco/smoker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(consumer, content = nil) ⇒ Smoker

Returns a new instance of Smoker.



10
11
12
13
14
15
16
17
# File 'lib/tobacco/smoker.rb', line 10

def initialize(consumer, content = nil)
  self.consumer = consumer
  self.content  = content

  Tobacco::Callback.instance(consumer)

  generate_file_paths
end

Instance Attribute Details

#consumerObject

Returns the value of attribute consumer.



4
5
6
# File 'lib/tobacco/smoker.rb', line 4

def consumer
  @consumer
end

#contentObject

Returns the value of attribute content.



4
5
6
# File 'lib/tobacco/smoker.rb', line 4

def content
  @content
end

#file_path_generatorObject

Returns the value of attribute file_path_generator.



4
5
6
# File 'lib/tobacco/smoker.rb', line 4

def file_path_generator
  @file_path_generator
end

#persisterObject

Returns the value of attribute persister.



4
5
6
# File 'lib/tobacco/smoker.rb', line 4

def persister
  @persister
end

#readerObject

Returns the value of attribute reader.



4
5
6
# File 'lib/tobacco/smoker.rb', line 4

def reader
  @reader
end

Instance Method Details

#continue_writeObject

Public: Called by ContentValidator if content is valid



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tobacco/smoker.rb', line 42

def continue_write
  begin
    content_writer = Tobacco::Exhaler.new(content, filepath)
    content_writer.write!

    Tobacco::Callback.instance.notify(:on_success, content)

  rescue => e
    Tobacco.log("ErrorWriting: #{filepath}\nError: #{e}")

    error = error_object('Error Writing', e)
    Tobacco::Callback.instance.notify(:on_write_error, error)

    # raise
  end
end

#filepathObject



59
60
61
# File 'lib/tobacco/smoker.rb', line 59

def filepath
  @filepath ||= file_path_generator.output_filepath
end

#generate_file_pathsObject



19
20
21
# File 'lib/tobacco/smoker.rb', line 19

def generate_file_paths
  self.file_path_generator = Tobacco::Roller.new(consumer)
end

#readObject



23
24
25
# File 'lib/tobacco/smoker.rb', line 23

def read
  self.content = Tobacco::ContentReader.new(self).read
end

#write!Object

Public: Writes content to file allowing for manipulation of the content beforehand through the :before_write callback This is due to the fact that content can be set directly without going through the read method.

Validate is only in the write method because the content can be set directly and the read method never called.



36
37
38
# File 'lib/tobacco/smoker.rb', line 36

def write!
  Tobacco::ContentValidator.new(self).validate!
end