Module: RubyEnum::Associations
- Defined in:
- lib/rubyenum.rb
Instance Method Summary collapse
-
#create_helper_methods ⇒ Object
let ActiveRecord define these if using rails.
- #enumify(method_name, hash) ⇒ Object
- #to_camelcase(string) ⇒ Object
Instance Method Details
#create_helper_methods ⇒ Object
let ActiveRecord define these if using rails
74 75 76 77 78 79 80 81 82 83 |
# 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) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# 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) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# 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 |