DRYed up "@foo=self.class.foo unless @foo" statements

Using the example from the existing code:

  CHECKSUM_TYPES.each do |type|
    if !instance_variable_defined?("@#{type}")
      class_value = self.class.send(type)
      instance_variable_set("@#{type}", class_value) if class_value
    end
  end

I extracted that block into a method 'set_instance_variable' which I
then used in all places where this behavior was being used.
This commit is contained in:
Clinton R. Nixon 2009-09-22 13:03:46 -04:00 committed by Max Howell
parent 44a3d59630
commit 2668e2aeff

View File

@ -39,9 +39,9 @@ end
class Formula class Formula
# Homebrew determines the name # Homebrew determines the name
def initialize name='__UNKNOWN__' def initialize name='__UNKNOWN__'
@url=self.class.url unless @url set_instance_variable 'url'
set_instance_variable 'head'
@head=self.class.head unless @head
if @head and (not @url or ARGV.flag? '--HEAD') if @head and (not @url or ARGV.flag? '--HEAD')
@url=@head @url=@head
@version='HEAD' @version='HEAD'
@ -50,15 +50,15 @@ class Formula
raise if @url.nil? raise if @url.nil?
@name=name @name=name
validate_variable :name validate_variable :name
@version=self.class.version unless @version
@version=Pathname.new(@url).version unless @version set_instance_variable 'version'
@version ||= Pathname.new(@url).version
validate_variable :version if @version validate_variable :version if @version
@homepage=self.class.homepage unless @homepage
set_instance_variable 'homepage'
CHECKSUM_TYPES.each do |type| CHECKSUM_TYPES.each do |type|
if !instance_variable_defined?("@#{type}") set_instance_variable type
class_value = self.class.send(type)
instance_variable_set("@#{type}", class_value) if class_value
end
end end
end end
@ -324,6 +324,13 @@ private
v=eval "@#{name}" v=eval "@#{name}"
raise "Invalid @#{name}" if v.to_s.empty? or v =~ /\s/ raise "Invalid @#{name}" if v.to_s.empty? or v =~ /\s/
end end
def set_instance_variable(type)
if !instance_variable_defined?("@#{type}")
class_value = self.class.send(type)
instance_variable_set("@#{type}", class_value) if class_value
end
end
def method_added method def method_added method
raise 'You cannot override Formula.brew' if method == 'brew' raise 'You cannot override Formula.brew' if method == 'brew'