Created
June 15, 2016 20:38
-
-
Save jcalvert/dd919e98fbba0cf085918e7ff6473302 to your computer and use it in GitHub Desktop.
constants to class methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constants(false).reject{|symbol| symbol.to_s =~ /[a-z]/}.each do |constant| | |
name = constant.to_s.downcase.gsub(/_name/, '') | |
var_name = "@#{name}" | |
define_singleton_method(name) do | |
unless instance_variable_defined?(var_name) | |
instance_variable_set(var_name, find_by(:name => const_get(constant))) | |
end | |
instance_variable_get(var_name) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment