Class: Fix2factory::FixtureParser
- Inherits:
-
Object
- Object
- Fix2factory::FixtureParser
- Defined in:
- lib/fix2factory/fixture_parser.rb
Instance Attribute Summary collapse
-
#factory_names ⇒ Object
readonly
Returns the value of attribute factory_names.
-
#factory_writer ⇒ Object
readonly
Returns the value of attribute factory_writer.
-
#output_buffer ⇒ Object
readonly
Returns the value of attribute output_buffer.
Instance Method Summary collapse
- #define_attributes_for(name) ⇒ Object
- #define_factories ⇒ Object
-
#initialize(fixture_file) ⇒ FixtureParser
constructor
A new instance of FixtureParser.
- #model_name ⇒ Object
- #parse_fixture ⇒ Object
- #write_factory_attribute(name, attribute) ⇒ Object
Constructor Details
#initialize(fixture_file) ⇒ FixtureParser
Returns a new instance of FixtureParser.
11 12 13 14 15 |
# File 'lib/fix2factory/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_names ⇒ Object (readonly)
Returns the value of attribute factory_names.
9 10 11 |
# File 'lib/fix2factory/fixture_parser.rb', line 9 def factory_names @factory_names end |
#factory_writer ⇒ Object (readonly)
Returns the value of attribute factory_writer.
9 10 11 |
# File 'lib/fix2factory/fixture_parser.rb', line 9 def factory_writer @factory_writer end |
#output_buffer ⇒ Object (readonly)
Returns the value of attribute output_buffer.
9 10 11 |
# File 'lib/fix2factory/fixture_parser.rb', line 9 def output_buffer @output_buffer end |
Instance Method Details
#define_attributes_for(name) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/fix2factory/fixture_parser.rb', line 46 def define_attributes_for(name) attributes = @current_fixture[name].map { |k, v| k } # Remove newline which treated as attributes attributes = attributes.select { |attr| !attr.nil? } attributes.map { |attr| write_factory_attribute(name, attr) } end |
#define_factories ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/fix2factory/fixture_parser.rb', line 28 def define_factories factory_defination = [] @factory_names.each do |name| factory_defination << "FactoryGirl.define do" factory_defination << " factory :#{model_name}, class: #{model_name.camelize} do" define_attributes_for(name).each do |attr| factory_defination << attr end factory_defination << " end" factory_defination << "end" factory_defination << "" end factory_defination end |
#model_name ⇒ Object
17 18 19 20 |
# File 'lib/fix2factory/fixture_parser.rb', line 17 def model_name @fixture_file.match(/(\w+)\.yml/) $1.singularize end |
#parse_fixture ⇒ Object
22 23 24 25 26 |
# File 'lib/fix2factory/fixture_parser.rb', line 22 def parse_fixture @current_fixture = ::Psych.load_file(@fixture_file) @factory_names = @current_fixture.map { |k, v| k } @output_buffer = define_factories end |
#write_factory_attribute(name, attribute) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/fix2factory/fixture_parser.rb', line 53 def write_factory_attribute(name, attribute) attr_value = @current_fixture[name][attribute] # String needs quotation marks in Factory Girl # @fixture: # one: # name: Teddy # # @factory # name 'Teddy' attr_value = attr_value.is_a?(Numeric) ? "#{attr_value}" : "'#{attr_value}'" " #{attribute} #{attr_value}" end |