Skip to content

Commit

Permalink
Change attributes to maintain a hash of priorities and attributes.
Browse files Browse the repository at this point in the history
This means attributes won't be resorted every time an attribute is
added.

Closes #184
  • Loading branch information
joshuaclayton committed Aug 20, 2011
1 parent 2b4a6c7 commit 4d4c01d
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions lib/factory_girl/attribute_list.rb
Expand Up @@ -3,31 +3,31 @@ class AttributeList
include Enumerable

def initialize
@attributes = []
@attributes = {}
end

def define_attribute(attribute)
if attribute_defined?(attribute.name)
raise AttributeDefinitionError, "Attribute already defined: #{attribute.name}"
end

@attributes << attribute
add_attribute attribute
end

def add_callback(name, &block)
unless valid_callback_names.include?(name.to_sym)
raise InvalidCallbackNameError, "#{name} is not a valid callback name. Valid callback names are #{valid_callback_names.inspect}"
end

@attributes << Attribute::Callback.new(name.to_sym, block)
add_attribute Attribute::Callback.new(name.to_sym, block)
end

def each(&block)
@attributes.each(&block)
flattened_attributes.each(&block)
end

def attribute_defined?(attribute_name)
!@attributes.detect do |attribute|
!@attributes.values.flatten.detect do |attribute|
attribute.name == attribute_name &&
!attribute.is_a?(FactoryGirl::Attribute::Callback)
end.nil?
Expand All @@ -38,22 +38,40 @@ def apply_attributes(attributes_to_apply)

attributes_to_apply.each do |attribute|
if attribute_defined?(attribute.name)
@attributes.delete_if do |attrib|
@attributes[attribute.priority].delete_if do |attrib|
new_attributes << attrib.clone if attrib.name == attribute.name
end
else
new_attributes << attribute.clone
end
end

@attributes.unshift *new_attributes
@attributes = @attributes.partition {|attr| attr.priority.zero? }.flatten
prepend_attributes new_attributes
end

private

def valid_callback_names
[:after_build, :after_create, :after_stub]
end

def add_attribute(attribute)
@attributes[attribute.priority] ||= []
@attributes[attribute.priority] << attribute
end

def prepend_attributes(new_attributes)
new_attributes.group_by {|attr| attr.priority }.each do |priority, attributes|
@attributes[priority] ||= []
@attributes[priority].unshift *attributes
end
end

def flattened_attributes
@attributes.keys.sort.inject([]) do |result, key|
result << @attributes[key]
result
end.flatten
end
end
end

0 comments on commit 4d4c01d

Please sign in to comment.