cattr_accessor
एक रेल विस्तार that acts like attr_accessor
, but is accessible on both the class and its instances हो रहा है।
आप cattr_accessor
विधि के स्रोत को कॉपी, बाहर की जाँच this documentation चाहते हैं:
# File vendor/rails/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb, line 46
def cattr_accessor(*syms)
cattr_reader(*syms)
cattr_writer(*syms)
end
# File vendor/rails/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb, line 4
def cattr_reader(*syms)
syms.flatten.each do |sym|
next if sym.is_a?(Hash)
class_eval("unless defined? @@\#{sym}\[email protected]@\#{sym} = nil\nend\n\ndef self.\#{sym}\[email protected]@\#{sym}\nend\n\ndef \#{sym}\[email protected]@\#{sym}\nend\n", __FILE__, __LINE__)
end
end
# File vendor/rails/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb, line 24
def cattr_writer(*syms)
options = syms.extract_options!
syms.flatten.each do |sym|
class_eval("unless defined? @@\#{sym}\[email protected]@\#{sym} = nil\nend\n\ndef self.\#{sym}=(obj)\[email protected]@\#{sym} = obj\nend\n\n\#{\"\ndef \#{sym}=(obj)\[email protected]@\#{sym} = obj\nend\n\" unless options[:instance_writer] == false }\n", __FILE__, __LINE__)
end
end
स्रोत
2010-06-09 00:06:46
'google_search' मणि के लेखकों को सतर्क करें कि उन्हें यह समस्या मिली है। उन्हें रेल आईएमओ से 'cattr_accessor' पर निर्भर नहीं होना चाहिए। – horseyguy