Sha256: a5b12d5ff2fb92ce08f5eb6f5e6a4701b1ef09a07692d7632c90aa873ce7c89f

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

require "microformat/attribute_map"

module Microformat
  class Attribute
    attr_reader :name, :options, :attributes

    def initialize(name, options = {}, &block)
      @name = name
      @options = options
      @attributes ||= {}
      if options[:format]
        @attributes.merge!(options[:format].attribute_definition.attributes)
      end
      block.yield(self) if block_given?
    end
    
    def attribute(name, options = {}, &block)
      Attribute.new(name, options, &block).tap do |attribute|
        @attributes.merge!(name.to_sym => attribute)
      end
    end
    
    def read_from(document)
      selector = options[:selector] || ".#{@name}"
      sub_document = document.css(selector).first
      value = read_value_from(sub_document)
      AttributeMap.new(self, sub_document, { value: value })
    end
    
    def read_value_from(document)
      Array(options[:attribute] || "text").each do |attr|
        if attr == "text"
          return document.text
        elsif document[attr]
          return document[attr]
        end
      end
      return nil
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
microformat-0.0.3 lib/microformat/attribute.rb
microformat-0.0.2 lib/microformat/attribute.rb