Sha256: 43a725be8754fc15226aa36a728b6b5165dd5db3e8f7d4114473d4ad266657b0

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 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 && document.text) || options[:default], options[:cast])
        elsif document[attr]
          return cast(document && 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

1 entries across 1 versions & 1 rubygems

Version Path
microformat-0.0.6 lib/microformat/attribute.rb