Sha256: fbee076df5acf1da0163c01be9df9d0a163b1a51941498ad5894f45e5def6535

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

module Brief
  class Document::ContentExtractor
    def initialize(model_type, document)
      @model_type = model_type
      @document = document
    end

    attr_reader :document

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

    def content_schema_attributes
      model_class.definition.content_schema.attributes
    end

    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

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

    def method_missing(meth, *_args, &_block)
      if settings = content_schema_attributes.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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
brief-1.5.2 lib/brief/document/content_extractor.rb
brief-1.5.1 lib/brief/document/content_extractor.rb
brief-1.5.0 lib/brief/document/content_extractor.rb