Sha256: b681e22e90377db4a6960b051077f24bb8a367c3912064f1f9807612ef0dc678

Contents?: true

Size: 895 Bytes

Versions: 6

Compression:

Stored size: 895 Bytes

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 attribute_set
      model_class.definition.content_schema.attributes
    end

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

    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
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
brief-1.4.4 lib/brief/document/content_extractor.rb
brief-1.4.2 lib/brief/document/content_extractor.rb
brief-1.4.1 lib/brief/document/content_extractor.rb
brief-1.3.2 lib/brief/document/content_extractor.rb
brief-1.3.1 lib/brief/document/content_extractor.rb
brief-1.3.0 lib/brief/document/content_extractor.rb