Class: FactoryGirlJson::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_girl_json/exporter.rb

Instance Method Summary collapse

Constructor Details

#initialize(factory, size, serializer) ⇒ Exporter

Returns a new instance of Exporter.



3
4
5
6
7
# File 'lib/factory_girl_json/exporter.rb', line 3

def initialize(factory, size,  serializer)
  @factory = FactoryGirl.factories.find(factory.to_sym)
  @size = size ? size.to_i : 1
  @serializer = serializer.constantize if serializer
end

Instance Method Details

#exportObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/factory_girl_json/exporter.rb', line 9

def export
  $stdout.puts "Exporting factory #{@factory.name}"
  path = file_path
  unless File.exists? path
    model_or_collection = if @size > 1
                            FactoryGirl.create_list(@factory.name.to_sym, @size)
                          else
                            @factory.run(:create, {})
                          end

    json = to_json(model_or_collection)
    write_file(json, path)
  else
    $stderr.puts "File #{file_name} exists, delete file and run again to rewrite"
  end
end