module RubyEnum::Associations
Public Instance Methods
create_helper_methods()
click to toggle source
let ActiveRecord define these if using rails
# File lib/rubyenum.rb, line 74 def create_helper_methods unless self.new.respond_to? :read_attribute define_method(:read_attribute) do |name| instance_variable_get(:"@#{name}") end define_method(:write_attribute) do |name, value| instance_variable_set(:"@#{name}", value) end end end
enumify(method_name, hash)
click to toggle source
# File lib/rubyenum.rb, line 95 def enumify(method_name, hash) create_helper_methods klass = Object.const_get(to_camelcase(hash[:with].to_s)) define_method(method_name) do enum_to_return = nil var = read_attribute(method_name) klass.all.each do |item| if item.send(hash[:use]) == var enum_to_return = item break end end enum_to_return end define_method(:"#{method_name.to_s}=") do |enum| write_attribute(method_name, enum.send(hash[:use])) nil end end
to_camelcase(string)
click to toggle source
# File lib/rubyenum.rb, line 85 def to_camelcase(string) return_string = "" parts = string.split("_") parts.each do |part| part[0] = part[0].upcase return_string += part end return_string end