Sha256: 59114b32ff9ae8451b3dfd49ca1b215574a5c82fc627c3991dcf5b849d758b77

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 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 cast(document.text || options[:default], options[:cast])
        elsif document[attr]
          return cast(document[attr], options[:cast])
        end
      end
      return options[:default]
    end
    
    def cast(value, cast_to = nil)
      value && case cast_to
        when :integer then value.to_i
        when :decimal then value.to_f
        when :string then value.to_s
        else value
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
microformat-0.0.5 lib/microformat/attribute.rb
microformat-0.0.4 lib/microformat/attribute.rb