Module: ActiveEnum::Extensions::ClassMethods

Defined in:
lib/active_enum/extensions.rb

Instance Method Summary collapse

Instance Method Details

#define_active_enum_question_method(attribute) ⇒ Object

Define question method to check enum value against attribute value

user.sex?(:male)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/active_enum/extensions.rb', line 117

def define_active_enum_question_method(attribute)
  define_question_method(attribute) unless instance_method_already_implemented?("#{attribute}?")

  old_method = "#{attribute}_without_enum?"
  define_method("#{attribute}_with_enum?") do |*arg|
    arg = arg.first
    if arg
      send(attribute) == self.class.enum_for(attribute)[arg]
    else
      send(old_method)
    end
  end
  alias_method_chain "#{attribute}?".to_sym, :enum
end

#define_active_enum_read_method(attribute) ⇒ Object

Define read method to allow an argument for the enum component

user.sex
user.sex(:id)
user.sex(:name)
user.sex(:enum)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/active_enum/extensions.rb', line 63

def define_active_enum_read_method(attribute)
  unless instance_method_already_implemented?(attribute)
    define_read_method(attribute, attribute.to_s, columns_hash[attribute.to_s])
  end

  old_method = "#{attribute}_without_enum"
  define_method("#{attribute}_with_enum") do |*arg|
    arg = arg.first
    value = send(old_method)

    enum = self.class.enum_for(attribute)
    case arg
    when :id
      value if enum[value]
    when :name
      enum[value]
    when :enum
      enum
    else
      ActiveEnum.use_name_as_value ? enum[value] : value
    end
  end

  alias_method_chain attribute, :enum
end

#define_active_enum_write_method(attribute) ⇒ Object

Define write method to also handle enum value

user.sex = 1
user.sex = :male


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/active_enum/extensions.rb', line 94

def define_active_enum_write_method(attribute)
  unless instance_method_already_implemented?("#{attribute}=")
    define_write_method(attribute)
  end

  old_method = "#{attribute}_without_enum="
  define_method("#{attribute}_with_enum=") do |arg|
    enum = self.class.enum_for(attribute)
    if arg.is_a?(Symbol)
      value = enum[arg]
      send(old_method, value)
    else
      send(old_method, arg)
    end
  end

  alias_method_chain "#{attribute}=".to_sym, :enum
end

#enum_for(attribute) ⇒ Object



52
53
54
# File 'lib/active_enum/extensions.rb', line 52

def enum_for(attribute)
  self.enumerated_attributes[attribute.to_sym]
end

#enumerate(*attributes, &block) ⇒ Object

enumerate :to, :from, :with => Sex



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/active_enum/extensions.rb', line 28

def enumerate(*attributes, &block)
				options = attributes.extract_options!
				attributes.each do |attribute|
					begin
if block_given?
	enum_name = "#{self.to_s.underscore}_#{attribute}"
	ActiveEnum.define { enum(enum_name, &block) }
	enum = enum_name.classify.constantize
else
	enum = options[:with] || attribute.to_s.classify.constantize
end
attribute = attribute.to_sym
self.enumerated_attributes[attribute] = enum

define_active_enum_read_method(attribute)
define_active_enum_write_method(attribute)
define_active_enum_question_method(attribute)
					rescue NameError => e
raise e unless e.message =~ /uninitialized constant/
raise ActiveEnum::EnumNotFound, "Enum class could not be found for attribute '#{attribute}' in class #{self}. Specify the enum class using the :with option."
					end
				end
end