Sha256: 4a36b8ad782b575b61f6f4ee83df404b36a351c9dac80635ee2fc4a8db8ff093

Contents?: true

Size: 914 Bytes

Versions: 15

Compression:

Stored size: 914 Bytes

Contents

class Factory

  cattr_accessor :aliases #:nodoc:
  self.aliases = [
    [/(.*)_id/, '\1'],
    [/(.*)/, '\1_id']
  ]

  # Defines a new alias for attributes
  #
  # Arguments:
  #   pattern: (Regexp)
  #     A pattern that will be matched against attributes when looking for
  #     aliases. Contents captured in the pattern can be used in the alias.
  #   replace: (String)
  #     The alias that results from the matched pattern. Captured strings can
  #     be insert like String#sub.
  #
  # Example:
  #   
  #   Factory.alias /(.*)_confirmation/, '\1'
  def self.alias (pattern, replace)
    self.aliases << [pattern, replace]
  end

  def self.aliases_for (attribute) #:nodoc:
    aliases.collect do |params|
      pattern, replace = *params
      if pattern.match(attribute.to_s)
        attribute.to_s.sub(pattern, replace).to_sym
      else
        nil
      end
    end.compact << attribute
  end

end

Version data entries

15 entries across 15 versions & 7 rubygems

Version Path
gabrielg-factory_girl-1.1.6 lib/factory_girl/aliases.rb
gabrielg-factory_girl-1.1.7 lib/factory_girl/aliases.rb
gabrielg-factory_girl-1.1.8 lib/factory_girl/aliases.rb
gabrielg-factory_girl-1.1.9 lib/factory_girl/aliases.rb
gabrielg-factory_girl-1.2.0 lib/factory_girl/aliases.rb
gabrielg-factory_girl-1.2.1 lib/factory_girl/aliases.rb
gabrielg-factory_girl-1.2.2 lib/factory_girl/aliases.rb
handcrafted-factory_girl-1.1.14 lib/factory_girl/aliases.rb
jrun-factory_girl-1.1.3.9999 lib/factory_girl/aliases.rb
multiplay-factory_girl-1.1.5 lib/factory_girl/aliases.rb
thoughtbot-factory_girl-1.1.3 lib/factory_girl/aliases.rb
thoughtbot-factory_girl-1.1.4 lib/factory_girl/aliases.rb
threedaymonk-factory_girl-1.1.4 lib/factory_girl/aliases.rb
factory_girl-1.1.3 lib/factory_girl/aliases.rb
factory_girl-1.1.4 lib/factory_girl/aliases.rb