Class: Brief::Document::ContentExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/brief/document/content_extractor.rb

Instance Method Summary collapse

Constructor Details

#initialize(model_type, document) ⇒ ContentExtractor

Returns a new instance of ContentExtractor.



3
4
5
6
# File 'lib/brief/document/content_extractor.rb', line 3

def initialize(model_type, document)
  @model_type = model_type
  @document = document
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/brief/document/content_extractor.rb', line 24

def method_missing(meth, *args, &block)
  if settings = attribute_set.fetch(meth, nil)
    if settings.args.length == 1 && settings.args.first.is_a?(String)
      selector = settings.args.first
      matches = document.css(selector)

      if matches.length > 1
        selector.match(/first-of-type/) ? matches.first.text : matches.map(&:text)
      else
        matches.first.try(:text)
      end
    end
  end
end

Instance Method Details

#attribute_setObject



16
17
18
# File 'lib/brief/document/content_extractor.rb', line 16

def attribute_set
  model_class.definition.content_schema.attributes
end

#documentObject



8
9
10
# File 'lib/brief/document/content_extractor.rb', line 8

def document
  @document
end

#model_classObject



12
13
14
# File 'lib/brief/document/content_extractor.rb', line 12

def model_class
  Brief::Model.for_type(@model_type)
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/brief/document/content_extractor.rb', line 20

def respond_to?(meth)
  attribute_set.key?(meth) || super
end