Sha256: 978522586574349d1f59c7e091c75130842a05d7ac401154bdf8761c9768a3e3

Contents?: true

Size: 1003 Bytes

Versions: 4

Compression:

Stored size: 1003 Bytes

Contents

module Factory
  # Build actions for the class
  def self.build(klass, &block)
    name = klass.to_s.underscore
    define_method("#{name}_attributes", block)
    
    module_eval <<-end_eval
      def valid_#{name}_attributes(attributes = {})
        #{name}_attributes(attributes)
        attributes
      end
      
      def new_#{name}(attributes = {})
        #{klass}.new(valid_#{name}_attributes(attributes))
      end
      
      def create_#{name}(*args)
        record = new_#{name}(*args)
        record.save!
        record.reload
        record
      end
    end_eval
  end
  
  build Car do |attributes|
    attributes.reverse_merge!(
      :name => 'Porsche'
    )
  end
  
  build Preference do |attributes|
    attributes[:owner] = create_user unless attributes.include?(:owner)
    attributes.reverse_merge!(
      :attribute => 'notifications',
      :value => false
    )
  end
  
  build User do |attributes|
    attributes.reverse_merge!(
      :login => 'admin'
    )
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
preferences-0.0.1 test/factory.rb
preferences-0.1.0 test/factory.rb
preferences-0.1.1 test/factory.rb
preferences-0.1.2 test/factory.rb