Module: FeedNormalizer::ElementEquality

Included in:
Entry, Feed
Defined in:
lib/structures.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



31
32
33
34
35
# File 'lib/structures.rb', line 31

def ==(other)
  other.equal?(self) ||
    (other.instance_of?(self.class) &&
      self.class::ELEMENTS.all?{ |el| self.send(el) == other.send(el)} )
end

#diff(other, elements = self.class::ELEMENTS) ⇒ Object

Returns the difference between two Feed instances as a hash. Any top-level differences in the Feed object as presented as:

{ :title => [content, other_content] }

For differences at the items level, an array of hashes shows the diffs on a per-entry basis. Only entries that differ will contain a hash:

{ :items => [
   {:title => ["An article tile", "A new article title"]},
   {:title => ["one title", "a different title"]} ]}

If the number of items in each feed are different, then the count of each is provided instead:

{ :items => [4,5] }

This method can also be useful for human-readable feed comparison if its output is dumped to YAML.



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
# File 'lib/structures.rb', line 56

def diff(other, elements = self.class::ELEMENTS)
  diffs = {}

  elements.each do |element|
    if other.respond_to?(element)
      self_value = self.send(element)
      other_value = other.send(element)

      next if self_value == other_value

      diffs[element] = if other_value.respond_to?(:diff)
        self_value.diff(other_value)

      elsif other_value.is_a?(Enumerable) && other_value.all?{|v| v.respond_to?(:diff)}

        if self_value.size != other_value.size
          [self_value.size, other_value.size]
        else
          enum_diffs = []
          self_value.each_with_index do |val, index|
            enum_diffs << val.diff(other_value[index], val.class::ELEMENTS)
          end
          enum_diffs.reject{|h| h.empty?}
        end

      else
        [other_value, self_value] unless other_value == self_value
      end
    end
  end

  diffs
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/structures.rb', line 27

def eql?(other)
  self == (other)
end