Class: FixToChix::FixtureParser

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

Constant Summary collapse

DEFAULT_NAMES =
["one", "two"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fixture_file) ⇒ FixtureParser

Returns a new instance of FixtureParser.

Raises:

  • (ArgumentError)


11
12
13
14
15
# File 'lib/fix_to_chix/fixture_parser.rb', line 11

def initialize(fixture_file)
  raise ArgumentError if fixture_file.nil?
  raise FileNotFoundError unless File.exists?(fixture_file)
  @fixture_file = fixture_file
end

Instance Attribute Details

#factory_namesObject (readonly)

Returns the value of attribute factory_names.



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

def factory_names
  @factory_names
end

#factory_writerObject (readonly)

Returns the value of attribute factory_writer.



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

def factory_writer
  @factory_writer
end

#output_bufferObject (readonly)

Returns the value of attribute output_buffer.



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

def output_buffer
  @output_buffer
end

Instance Method Details

#attributes_for(factory_name) ⇒ Object



28
29
30
# File 'lib/fix_to_chix/fixture_parser.rb', line 28

def attributes_for(factory_name)
  @current_fixture[factory_name].map {|key, value| key if key != "id" }
end

#define_attributes_for(name) ⇒ Object



45
46
47
48
# File 'lib/fix_to_chix/fixture_parser.rb', line 45

def define_attributes_for(name)
  attributes = attributes_for(name)
  attributes.map {|attr| write_factory_attribute(name, attr) }.reverse
end

#define_factoriesObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fix_to_chix/fixture_parser.rb', line 32

def define_factories
  factory_definition = []
  @factory_names.each do |name|
    factory_definition << "Factory.define :#{self.factory_name(name)}, :class => #{model_name.camelize} do |#{model_name[0].chr}|"
    define_attributes_for(name).each do |attrib|
      factory_definition << attrib
    end
    factory_definition << "end"
    factory_definition << ""
  end
  factory_definition
end

#factory_name(node_name) ⇒ Object



56
57
58
# File 'lib/fix_to_chix/fixture_parser.rb', line 56

def factory_name(node_name)
  DEFAULT_NAMES.include?(node_name) ? "#{self.model_name}_#{node_name}" : node_name
end

#model_nameObject



17
18
19
20
# File 'lib/fix_to_chix/fixture_parser.rb', line 17

def model_name
  @fixture_file.match(/(\w+)\.yml/)
  $1.singularize
end

#parse_fixtureObject



22
23
24
25
26
# File 'lib/fix_to_chix/fixture_parser.rb', line 22

def parse_fixture
  @current_fixture = YAML.load_file(@fixture_file)
  @factory_names = @current_fixture.map{ |key, value| key }
  @output_buffer = define_factories
end

#write_factory_attribute(name, attribute) ⇒ Object



50
51
52
53
54
# File 'lib/fix_to_chix/fixture_parser.rb', line 50

def write_factory_attribute(name, attribute)
  attr_value = @current_fixture[name][attribute]
  attr_value = attr_value.to_i == 0 ? "'#{attr_value}'" : "#{attr_value}"
  "  #{model_name[0].chr}.#{attribute} #{attr_value}"
end