Module: FlickrMocks::Helpers

Defined in:
lib/flickr_mocks/helpers.rb

Overview

contains various helper methods for comparing and marshaling FlickRaw::Response and ResponseList objects.

Class Method Summary collapse

Class Method Details

.dump(response, file) ⇒ Object

saves the marshaled version of the supplied object into a file



39
40
41
42
43
44
45
46
# File 'lib/flickr_mocks/helpers.rb', line 39

def dump(response,file)
  begin
    f  = File.open(file,'w')
    f.write Marshal.dump(response)
  ensure
    f.close
  end
end

.equivalent?(a, b) ⇒ Boolean

compares two FlickRaw::Response or two FlickRaw::ResponseList. It recursively checks the internal state of these two objects.

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/flickr_mocks/helpers.rb', line 19

def equivalent?(a,b)
  return false if a.class != b.class
  case a
  when FlickRaw::Response,FlickRaw::ResponseList then a.methods(false).collect do |m|
      b.respond_to?(m) ? equivalent?(a.send(m),b.send(m)) : false
    end.inject(true) {|r1,r2| r1 && r2}

  when Hash then a.keys.collect  do |k|
      b.has_key?(k) ? equivalent?(a[k],b[k]) : false
    end.inject(true){|r1,r2| r1 && r2}

  when Array then a.each_with_index.collect do |v,i|
      b.length == a.length ? equivalent?(v,b[i]) : false
    end.inject(true) {|r1,r2| r1 && r2}
  else
    a == b
  end
end

.extensionObject

returns the file extension used to store the marshaled FlickRaw fixtures



7
8
9
# File 'lib/flickr_mocks/helpers.rb', line 7

def extension
  ".marshal"
end

.fname_fixture(symbol) ⇒ Object

returns the file name for the marshaled FlickRaw fixture

Raises:

  • (RunTimeError)


12
13
14
15
# File 'lib/flickr_mocks/helpers.rb', line 12

def fname_fixture(symbol)
  raise RunTimeError unless symbol.is_a? Symbol
  symbol.to_s + extension
end

.load(file) ⇒ Object

returns the un-marshaled contents of the user-specified file



49
50
51
52
53
54
55
56
# File 'lib/flickr_mocks/helpers.rb', line 49

def load(file)
  begin
    f=File.open(file,'r')
    Marshal.load(f)
  ensure
    f.close
  end
end