2014-11-27 18:45:35 +03:00
|
|
|
require 'json'
|
2015-10-06 13:50:26 +03:00
|
|
|
require 'pp'
|
2014-11-27 18:45:35 +03:00
|
|
|
|
|
|
|
|
class FixtureFormatter
|
|
|
|
|
|
|
|
|
|
def initialize(fixtures)
|
|
|
|
|
@fixtures = fixtures
|
2015-10-06 13:50:26 +03:00
|
|
|
if ENV["DEBUG"]
|
|
|
|
|
puts "Loaded fixtures:"
|
|
|
|
|
pp @fixtures
|
|
|
|
|
end
|
2014-11-27 18:45:35 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def json(path, options={})
|
2015-10-06 13:50:26 +03:00
|
|
|
result = nil
|
2015-07-08 16:48:19 +03:00
|
|
|
begin
|
2015-10-06 13:50:26 +03:00
|
|
|
result = JSON.pretty_generate(get_fixture(path, options))
|
2015-07-08 16:48:19 +03:00
|
|
|
rescue
|
|
|
|
|
raise "Fixture '#{path}' is absent"
|
|
|
|
|
end
|
2015-10-06 13:50:26 +03:00
|
|
|
options[:spaces] = 4 unless options[:spaces]
|
|
|
|
|
result = shift_to_right(result, options[:spaces])
|
2014-11-27 18:45:35 +03:00
|
|
|
result
|
|
|
|
|
end
|
|
|
|
|
|
2015-10-06 13:50:26 +03:00
|
|
|
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
|
2015-10-06 13:50:26 +03:00
|
|
|
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
|
|
|
|
|
|
2015-10-06 13:50:26 +03:00
|
|
|
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
|
2015-10-06 13:50:26 +03:00
|
|
|
end
|