Sha256: 2d6ec1f2282b49cfb39854497d27d36418bdce6cda1810bab17362d850232c83
Contents?: true
Size: 1.41 KB
Versions: 2
Compression:
Stored size: 1.41 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_s.strip.to_i when :decimal then value.to_s.strip.to_f when :string then value.to_s.strip else value end end end end
Version data entries
2 entries across 2 versions & 2 rubygems
Version | Path |
---|---|
elcamino-microformat-0.0.8 | lib/microformat/attribute.rb |
microformat-0.0.7 | lib/microformat/attribute.rb |