Skip to content

Commit

Permalink
Simplify Factory::Runner by always having attribute add_to(proxy)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed Nov 1, 2011
1 parent 5a48129 commit 3803be3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
8 changes: 8 additions & 0 deletions lib/factory_girl/attribute.rb
Expand Up @@ -52,5 +52,13 @@ def ensure_non_attribute_writer!
"rather than 'f.#{attribute_name} = value'"
end
end

def set_proxy_value(proxy, value)
if @ignored
proxy.set_ignored(name, value)
else
proxy.set(name, value)
end
end
end
end
6 changes: 1 addition & 5 deletions lib/factory_girl/attribute/dynamic.rb
Expand Up @@ -12,11 +12,7 @@ def add_to(proxy)
raise SequenceAbuseError
end

if @ignored
proxy.set_ignored(name, value)
else
proxy.set(name, value)
end
set_proxy_value(proxy, value)
end
end
end
Expand Down
6 changes: 1 addition & 5 deletions lib/factory_girl/attribute/sequence.rb
Expand Up @@ -9,11 +9,7 @@ def initialize(name, sequence, ignored)

def add_to(proxy)
value = FactoryGirl.generate(@sequence)
if @ignored
proxy.set_ignored(name, value)
else
proxy.set(name, value)
end
set_proxy_value(proxy, value)
end
end

Expand Down
6 changes: 1 addition & 5 deletions lib/factory_girl/attribute/static.rb
Expand Up @@ -11,11 +11,7 @@ def initialize(name, value, ignored)
end

def add_to(proxy)
if @ignored
proxy.set_ignored(name, @value)
else
proxy.set(name, @value)
end
set_proxy_value(proxy, @value)
end

def priority
Expand Down
13 changes: 6 additions & 7 deletions lib/factory_girl/factory.rb
Expand Up @@ -165,7 +165,7 @@ def apply_attributes
end

def apply_remaining_overrides
@overrides.each { |attr, val| proxy.set(attr, val) }
@overrides.each { |attr, val| add_static_attribute(attr, val) }
end

def overrides_for_attribute(attribute)
Expand All @@ -174,16 +174,15 @@ def overrides_for_attribute(attribute)

def handle_attribute_with_overrides(attribute)
overrides_for_attribute(attribute).each do |attr, val|
if attribute.ignored
proxy.set_ignored(attr, val)
else
proxy.set(attr, val)
end

add_static_attribute(attr, val, attribute.ignored)
@overrides.delete(attr)
end
end

def add_static_attribute(attr, val, ignored = false)
Attribute::Static.new(attr, val, ignored).add_to(proxy)
end

def handle_attribute_without_overrides(attribute)
attribute.add_to(proxy)
end
Expand Down

0 comments on commit 3803be3

Please sign in to comment.