Sha256: 34931466f0ef19d51871e138de5fe7db410fca90aa4e9f53f8dd589517a6adb1

Contents?: true

Size: 908 Bytes

Versions: 1

Compression:

Stored size: 908 Bytes

Contents

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

    def document
      @document
    end

    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

1 entries across 1 versions & 1 rubygems

Version Path
brief-1.2.0 lib/brief/document/content_extractor.rb