Class: Tobacco::ContentReader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(smoker) ⇒ ContentReader

Returns a new instance of ContentReader.



5
6
7
8
9
# File 'lib/tobacco/content_reader.rb', line 5

def initialize(smoker)
  @smoker   = smoker
  @consumer = smoker.consumer
  @filepath = smoker.file_path_generator
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



3
4
5
# File 'lib/tobacco/content_reader.rb', line 3

def content
  @content
end

#modified_contentObject

Returns the value of attribute modified_content.



3
4
5
# File 'lib/tobacco/content_reader.rb', line 3

def modified_content
  @modified_content
end

#readerObject

Returns the value of attribute reader.



3
4
5
# File 'lib/tobacco/content_reader.rb', line 3

def reader
  @reader
end

Instance Method Details

#choose_readerObject

The reader will either be the calling class (consumer) if it provides the content method or a new Inhaler object that will be used to read the content from a url



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tobacco/content_reader.rb', line 30

def choose_reader
  self.reader = \
    if @consumer.respond_to? Tobacco.content_method
      @consumer
    else
      Inhaler.new(@filepath.content_url).tap do |inhaler|

        # Add an alias for the user configured content_method
        # so that when it is called it calls :read
        # on the Inhaler instance
        #
        inhaler.instance_eval %{
          alias :"#{Tobacco.content_method}" :read
        }
      end
  end
end

#modify_contentObject



17
18
19
20
# File 'lib/tobacco/content_reader.rb', line 17

def modify_content
  self.modified_content = \
    Tobacco::Callback.instance.notify(:before_write, content)
end

#readObject



11
12
13
14
15
# File 'lib/tobacco/content_reader.rb', line 11

def read
  choose_reader
  read_content
  modify_content
end

#read_contentObject



22
23
24
# File 'lib/tobacco/content_reader.rb', line 22

def read_content
  self.content = reader.send(Tobacco.content_method)
end