Sha256: a6bcd0811bc168f6d64a6ac2cea3a822c5135a7b9b166c3da31506f6f38bcbcc

Contents?: true

Size: 1.6 KB

Versions: 7

Compression:

Stored size: 1.6 KB

Contents

module Microformats
  # stub to get around the tests for now
  class PropertySet
    def initialize(hash)
      @hash = hash
    end

    def to_h
      @hash
    end

    def to_hash
      @hash.to_hash
    end

    def to_json
      @hash.to_hash.to_json
    end

    def [](key)
      @hash[key]
    end

    def to_s
      @hash.to_s
    end

    def respond_to?(sym, include_private = false)
      key?(sym) || super(sym, include_private)
    end

    def method_missing(mname, *args, &block)
      if respond_to?(mname)
        result_hash = val?(mname)

        if result_hash.is_a?(Array)
          if args[0].nil?
            result_hash = result_hash[0] # will return nil for an empty array
          elsif args[0] == :all
            return result_hash.map do |x|
              ParserResult.new(x)
            end
          elsif args[0].to_i < result_hash.count
            result_hash = result_hash[args[0].to_i]
          else
            result_hash = result_hash[0] # will return nil for an empty array
          end
        end

        if result_hash.is_a?(Hash)
          ParserResult.new(result_hash)
        else
          result_hash
        end
      else
        super(mname, *args, &block)
      end
    end

    private

    def key?(name)
      name = name.to_s
      name_dash = name.tr('_', '-') if name.include?('_')

      !@hash[name].nil? || !@hash[name_dash].nil?
    end

    def val?(name)
      name = name.to_s
      name_dash = name.tr('_', '-') if name.include?('_')

      if !@hash[name].nil?
        @hash[name]
      elsif !@hash[name_dash].nil?
        @hash[name_dash]
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
microformats-4.5.0 lib/microformats/results/property_set.rb
microformats-4.4.1 lib/microformats/results/property_set.rb
microformats-4.3.1 lib/microformats/results/property_set.rb
microformats-4.2.1 lib/microformats/results/property_set.rb
microformats-4.2.0 lib/microformats/results/property_set.rb
microformats-4.1.0 lib/microformats/results/property_set.rb
microformats-4.0.9 lib/microformats/results/property_set.rb