Class: Blueprints::FixturesConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/blueprints/convertable/fixtures.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FixturesConverter

Returns a new instance of FixturesConverter.



3
4
5
6
7
8
9
10
11
# File 'lib/blueprints/convertable/fixtures.rb', line 3

def initialize(options = {})
  @format = "fixtures"
  @source_files = options[:source_files]
  @output_file = options[:output_file]
  @blueprints_data = ""
  
  raise "No source files given" unless @source_files
  raise "No output file given" unless @output_file
end

Instance Method Details

#convertObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/blueprints/convertable/fixtures.rb', line 13

def convert
  @source_files.each do |fixture_file|
    klass = File.basename(fixture_file, '.yml').singularize.capitalize

    loaded_yaml = YAML.load(File.read(fixture_file))

    @blueprints_data = loaded_yaml.collect do |title,yaml_obj|
      params = yaml_obj.collect do |k,v|
        ":#{k} => #{parameterize(v)}"
      end.join(', ')

      "#{klass}.blueprint(:#{title}, {#{params}})\n"
    end
  end
  
  @blueprints_data
end

#parameterize(object) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/blueprints/convertable/fixtures.rb', line 31

def parameterize(object)
  if object =~ /<%=\s+?(.+)\s+%>/
    '('+$1+')'
  elsif object.is_a?(String)
    (%Q(#{object})).inspect
  elsif object.nil?
    'nil'
  else
    object.to_s
  end
end