lib/genderize/genderize.rb in genderize-0.0.2 vs lib/genderize/genderize.rb in genderize-0.0.3
- old
+ new
@@ -1,42 +1,49 @@
-require "genderize/gender"
-
module Genderize::Genderize
def self.included(base)
base.extend(Genderize::Genderize::ClassMethods)
end
module ClassMethods
+ require "genderize/gender"
def genderize(col_name = "gender")
-
# Reads the DB column value for gender attribute and creates a new Gender
# object with it's value
#
# The object is memoized for future calls.
#
# Returns a Gender
define_method col_name do
if value = instance_variable_get("@#{col_name}")
return value
+ end
+ read_value = read_attribute(col_name)
+ if read_value.blank?
+ return read_value
else
- instance_variable_set("@#{col_name}", Gender.new(read_attribute(col_name)))
+ instance_variable_set("@#{col_name}", Genderize::Gender.new(read_attribute(col_name)))
end
end
# Writes to the DB column the new value for the gender attribute
# Sets the instance varaible value too
#
# string - A String indicating the gender. Must be either 'm', "M", 'f' or "F".
#
# Raises ArgumentError if gender is not a single alphanumeric character "m" or "f"
define_method "#{col_name}=" do |string|
- unless string.to_s =~ /\A(m|f)\Z/i
+ unless string.blank? or string.to_s =~ /\A(m|f)\Z/i
raise ArgumentError, "Gender must be a single alphanumeric character"
end
write_attribute(col_name, string)
- instance_variable_set("@#{col_name}", Gender.new(read_attribute(col_name)))
+
+ if string.blank?
+ instance_variable_set("@#{col_name}", string)
+ else
+ instance_variable_set("@#{col_name}", Genderize::Gender.new(read_attribute(col_name)))
+ end
end
end
end
\ No newline at end of file