Permalink
Browse files

AttributeList's @attributes is an array until it needs to sort attrib…

…utes
  • Loading branch information...
1 parent 2bf5cea commit d563b94e95e35e6c2b44a4600d484b8df2ab29ee @joshuaclayton joshuaclayton committed Nov 21, 2011
Showing with 18 additions and 16 deletions.
  1. +18 −16 lib/factory_girl/attribute_list.rb
@@ -4,7 +4,7 @@ class AttributeList
def initialize(name = nil)
@name = name
- @attributes = {}
+ @attributes = []
end
def define_attribute(attribute)
@@ -15,7 +15,7 @@ def define_attribute(attribute)
end
def each(&block)
- flattened_attributes.each(&block)
+ sorted_attributes.each(&block)
end
def apply_attributes(attributes_to_apply)
@@ -33,21 +33,19 @@ def apply_attributes(attributes_to_apply)
private
def add_attribute(attribute)
- @attributes[attribute.priority] ||= []
- @attributes[attribute.priority] << attribute
+ @attributes << attribute
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
+ @attributes.unshift *new_attributes
end
- def flattened_attributes
- @attributes.keys.sort.inject([]) do |result, key|
- result << @attributes[key]
+ def sorted_attributes
+ attributes_hash = attributes_hash_by_priority
+
+ attributes_hash.keys.sort.inject([]) do |result, key|
+ result << attributes_hash[key]
result
end.flatten
end
@@ -69,16 +67,20 @@ def attribute_defined?(attribute_name)
end
def find_attribute(attribute_name)
- @attributes.values.flatten.detect do |attribute|
+ @attributes.detect do |attribute|
attribute.name == attribute_name
end
end
def delete_attribute(attribute_name)
- if attribute_defined?(attribute_name)
- @attributes.each_value do |attributes|
- attributes.delete_if {|attrib| attrib.name == attribute_name }
- end
+ @attributes.delete_if {|attrib| attrib.name == attribute_name }
+ end
+
+ def attributes_hash_by_priority
+ @attributes.inject({}) do |result, attribute|
+ result[attribute.priority] ||= []
+ result[attribute.priority] << attribute
+ result
end
end
end

0 comments on commit d563b94

Please sign in to comment.