Class: Brief::Document::ContentExtractor

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

Defined Under Namespace

Classes: ExtractionRule

Instance Attribute Summary collapse

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



43
44
45
46
47
# File 'lib/brief/document/content_extractor.rb', line 43

def method_missing(meth, *_args, &_block)
  return super unless supports_extraction?(meth)
  rule = ExtractionRule.new(extraction_rule_for(meth))
  rule.apply_to(document)
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



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

def document
  @document
end

Instance Method Details

#content_schema_attributesObject



14
15
16
# File 'lib/brief/document/content_extractor.rb', line 14

def content_schema_attributes
  model_class.definition.content_schema.attributes.symbolize_keys!
end

#extracted_content_dataObject



18
19
20
21
22
23
24
25
# File 'lib/brief/document/content_extractor.rb', line 18

def extracted_content_data
  me = self
  content_schema_attributes.keys.reduce({}.to_mash) do |memo, attr|
    val = me.send(attr) rescue nil
    memo[attr] = val if val
    memo
  end
end

#extraction_rule_for(attribute) ⇒ Object



31
32
33
# File 'lib/brief/document/content_extractor.rb', line 31

def extraction_rule_for(attribute)
  content_schema_attributes.fetch(attribute.to_sym, nil)
end

#model_classObject



10
11
12
# File 'lib/brief/document/content_extractor.rb', line 10

def model_class
  document.model_class
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/brief/document/content_extractor.rb', line 27

def respond_to?(meth)
  supports_extraction?(meth) || super
end

#selector_for(attribute) ⇒ Object



35
36
37
# File 'lib/brief/document/content_extractor.rb', line 35

def selector_for(attribute)
  extraction_rule_for(attribute).first
end

#supports_extraction?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/brief/document/content_extractor.rb', line 39

def supports_extraction?(attribute)
  content_schema_attributes.key?(attribute.to_sym)
end