Class: FixtureConverter

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ FixtureConverter

Returns a new instance of FixtureConverter.



2
3
4
5
6
7
# File 'lib/fixture_converter.rb', line 2

def initialize(opts = {})
  @body   = []
  @header = []
  @indent_depth = opts[:indent_depth] || 0
  @output_style = opts[:output_style]
end

Instance Method Details

#convert_fixture(path) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fixture_converter.rb', line 13

def convert_fixture(path)
  fixture = YAML.load_file(path)
  return if not fixture

  plural_model_name = path.basename.to_s.split('.').first
  model_name        = plural_model_name.singularize
  header do
    "#{plural_model_name} = {}"
  end

  body 'before(:all) do' if powder?
  indent(powder?) do
    body "# Setup #{plural_model_name}"
    body "# Generated from fixture in #{path.dirname.basename}/#{path.basename}"
    body '#'
    body ''
    fixture.each do |name, record|
      max_length = record.keys.collect {|key| key.length}.max
    
      body "#{plural_model_name}[:#{name}] = Factory.create_#{model_name}("
      indent do
        body do
          record.collect do |key,value|
            value = "\"#{value}\"" if value.kind_of?(String)
            key   = key.ljust(max_length)
            ":#{key} => #{value}"
          end
        end
      end
      body ')'
    end
  end
  body 'end' if powder?
  body ''
end

#convert_scenario(path) ⇒ Object



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

def convert_scenario(path)
  path.each_entry do |file|
    next if file.extname != '.yml'
    next if file.basename.to_s =~ /relationships/
    convert_fixture( path + file )
  end
end

#outObject



57
58
59
60
61
# File 'lib/fixture_converter.rb', line 57

def out
  puts @header.join("\n")
  puts "\n"
  puts @body.join("\n")
end

#powder?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/fixture_converter.rb', line 9

def powder?
  @output_style == :clay
end