Module: Genderize::ClassMethods

Defined in:
lib/genderize.rb

Instance Method Summary collapse

Instance Method Details

#genderize(col_name = "gender", opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/genderize.rb', line 17

def genderize(col_name = "gender", opts = {})
  # 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.
  #
  # col_name - String or Symbol name of gender column (default 'gender')
  #
  # opts - Hash options to handle storage and initialization scenarios:
  #        :no_empty_string - Boolean. If true, stores empty string as nil
  #        :set_invalid_to_nil - Boolean. If true, sets the gender to be 
  #                              an unknown Gender instead of throwing an
  #                              ArgumentError
  #
  # Returns a Gender

  no_empty_string = !!opts[:no_empty_string] || false
  set_invalid_to_nil = !!opts[:set_invalid_to_nil] || false

  define_method col_name do
    current_value = instance_variable_get("@#{col_name}")
    persist_value = Genderize::Gender.new(read_attribute(col_name))
    return current_value || instance_variable_set("@#{col_name}", persist_value)
  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. Valid parameters must have
  #          first letter be 'm', 'n', 'f' or ''. Case insensitive.
  #
  # Raises ArgumentError if gender isn't valid and set_invalid_to_nil is false
  define_method "#{col_name}=" do |string|
    string = string.to_s.first
    valid = string.to_s =~ /\A(m|f|n|)\Z/i

    unless valid
      if set_invalid_to_nil
        string = nil
      else
        raise ArgumentError, "Gender must be one of '', 'n', 'm', or 'f'"
      end
    end

    string = nil if (string == '' and no_empty_string)

    write_attribute(col_name, string)
    instance_variable_set("@#{col_name}", Genderize::Gender.new(read_attribute(col_name)))
  end

end