Class: Microformats::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/microformats/results/collection.rb

Overview

stub to get around the tests for now

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Collection

Returns a new instance of Collection.



6
7
8
# File 'lib/microformats/results/collection.rb', line 6

def initialize(hash)
  @hash = hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/microformats/results/collection.rb', line 56

def method_missing(sym, *args, &block)

  name = sym.to_s
  name_dash = name.gsub('_', '-') if name.include? '_'

  if not @hash['items'].nil?
    result_hash = @hash['items'].select do |x|
        x['type'].include?('h-' + name)
    end
    if result_hash.empty? and not name_dash.nil?
      result_hash = @hash['items'].select do |x|
        x['type'].include?('h-' + name_dash)
      end
    end
  end

  super(sym, *args, &block) if result_hash.empty?

  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.nil? or result_hash.empty?
    result_hash
  elsif result_hash.is_a? Hash
    ParserResult.new(result_hash)
  else
    result_hash
  end
end

Instance Method Details

#[](key) ⇒ Object



30
31
32
# File 'lib/microformats/results/collection.rb', line 30

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

#itemsObject



38
39
40
41
42
# File 'lib/microformats/results/collection.rb', line 38

def items
  @hash['items'].map do |item|
    ParserResult.new(item)
  end
end

#rel_urlsObject



48
49
50
# File 'lib/microformats/results/collection.rb', line 48

def rel_urls
  @hash['rel-urls']
end

#relsObject



44
45
46
# File 'lib/microformats/results/collection.rb', line 44

def rels
  @hash['rels']
end

#respond_to?(sym, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/microformats/results/collection.rb', line 52

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

#to_hObject



10
11
12
# File 'lib/microformats/results/collection.rb', line 10

def to_h
  @hash
end

#to_hashObject



14
15
16
# File 'lib/microformats/results/collection.rb', line 14

def to_hash
  @hash
end

#to_jsonObject



18
19
20
# File 'lib/microformats/results/collection.rb', line 18

def to_json
  to_hash.to_json
end

#to_sObject



22
23
24
25
26
27
28
# File 'lib/microformats/results/collection.rb', line 22

def to_s
  if @hash['value']
    return @hash['value']
  else
    super
  end
end

#valueObject



34
35
36
# File 'lib/microformats/results/collection.rb', line 34

def value
  @hash['value']
end