Skip to content

Instantly share code, notes, and snippets.

@jsl
Created November 23, 2010 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsl/711058 to your computer and use it in GitHub Desktop.
Save jsl/711058 to your computer and use it in GitHub Desktop.
cattr_reader
# File activesupport/lib/active_support/core_ext/class/attribute_accessors.rb, line 10
def cattr_reader(*syms)
options = syms.extract_options!
syms.each do |sym|
next if sym.is_a?(Hash)
class_eval("unless defined? @@\#{sym}\n@@\#{sym} = nil\nend\n\ndef self.\#{sym}\n@@\#{sym}\nend\n", __FILE__, __LINE__ + 1)
unless options[:instance_reader] == false
class_eval("def \#{sym}\n@@\#{sym}\nend\n", __FILE__, __LINE__ + 1)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment