Class: RSpec::Approvals::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/approvals/formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(approval) ⇒ Formatter

Returns a new instance of Formatter.



5
6
7
# File 'lib/rspec/approvals/formatter.rb', line 5

def initialize(approval)
  self.approval = approval
end

Instance Attribute Details

#approvalObject

Returns the value of attribute approval.



4
5
6
# File 'lib/rspec/approvals/formatter.rb', line 4

def approval
  @approval
end

Instance Method Details

#as_array(contents) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/rspec/approvals/formatter.rb', line 48

def as_array(contents)
  s = ""
  contents.each_with_index do |v,i|
    s << "[#{i.inspect}] #{v.inspect}\n"
  end
  s
end

#as_hash(contents) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/rspec/approvals/formatter.rb', line 40

def as_hash(contents)
  s = ""
  contents.each_pair do |k,v|
    s << "#{k.inspect} => #{v.inspect}\n"
  end
  s
end

#as_json(contents) ⇒ Object



31
32
33
# File 'lib/rspec/approvals/formatter.rb', line 31

def as_json(contents)
  JSON.pretty_generate(JSON.parse(contents))
end

#as_s(contents) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rspec/approvals/formatter.rb', line 9

def as_s(contents)
  if xml?
    as_xml(contents)
  elsif json?
    as_json(contents)
  elsif contents.respond_to?(:each_pair)
    as_hash(contents)
  elsif contents.respond_to?(:each_with_index)
    as_array(contents)
  else
    contents.inspect
  end
end

#as_xml(contents) ⇒ Object



35
36
37
38
# File 'lib/rspec/approvals/formatter.rb', line 35

def as_xml(contents)
  parser = XML::Parser.string contents.strip
  parser.parse.to_s
end

#json?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/rspec/approvals/formatter.rb', line 27

def json?
  approval.options[:format] == :json
end

#xml?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/rspec/approvals/formatter.rb', line 23

def xml?
  [:xml, :html].include? approval.options[:format]
end