Sha256: d179a0eb840461eed1be8b2df694f010180858a1f19e7d7aa97dac1aaf696269

Contents?: true

Size: 918 Bytes

Versions: 2

Compression:

Stored size: 918 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 Person do |attributes|
    attributes.reverse_merge!(
      :name => 'John Doe'
    )
  end
  
  build PhoneNumber do |attributes|
    attributes[:phoneable] = create_person unless attributes.include?(:phoneable)
    attributes.reverse_merge!(
      :country_code => '1',
      :number => '1234567890'
    )
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
has_phone_numbers-0.0.3 test/factory.rb
has_phone_numbers-0.0.4 test/factory.rb