fluke/devops-service/tests/templates/fixtures/fixture_formatter.rb

54 lines
1.0 KiB
Ruby
Raw Normal View History

2014-11-27 18:45:35 +03:00
require 'json'
require 'pp'
2014-11-27 18:45:35 +03:00
class FixtureFormatter
def initialize(fixtures)
@fixtures = fixtures
if ENV["DEBUG"]
puts "Loaded fixtures:"
pp @fixtures
end
2014-11-27 18:45:35 +03:00
end
def json(path, options={})
result = nil
2015-07-08 16:48:19 +03:00
begin
result = JSON.pretty_generate(get_fixture(path, options))
2015-07-08 16:48:19 +03:00
rescue
raise "Fixture '#{path}' is absent"
end
options[:spaces] = 4 unless options[:spaces]
result = shift_to_right(result, options[:spaces])
2014-11-27 18:45:35 +03:00
result
end
def get_fixture(path, options={})
2014-11-27 18:45:35 +03:00
keys = path.split('/')
hash = @fixtures
keys.each do |key|
hash = hash[key]
end
hash = hash.clone
hash.merge!(options[:value]) if options[:value]
hash.delete(options[:without_field]) if options[:without_field]
2014-11-27 18:45:35 +03:00
hash
end
private
2014-11-27 18:45:35 +03:00
def shift_to_right(text, spaces_count)
buffer = ''
first_line = true
text.each_line do |line|
if first_line
first_line = false
buffer += line
next
end
buffer += (' ' * spaces_count) + line
end
buffer
end
end