Sha256: 00a1f3d9699b074404b834c2c0a58745b9a08ab35c54a259e3085d4f022bf566

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

require "genderize/gender"
require "genderize/engine"

unless Rails
  raise "Genderize is a Rails gem and expects to used in a Rails (3 or higher) application"
end

module Genderize

  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods


    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
        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. 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|
        string = string.to_s.first
        unless string.to_s =~ /\A(m|f|)\Z/i
          raise ArgumentError, "Gender must be one of '', 'm', or 'f'"
        end
        write_attribute(col_name, string)

        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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
genderize-0.0.8 lib/genderize.rb
genderize-0.0.7 lib/genderize.rb