method
attribute
rails latest stable - Class:
ActiveSupport::CurrentAttributes
attribute(*names)public
Declares one or more attributes that will be given both class and instance accessor methods.
# File activesupport/lib/active_support/current_attributes.rb, line 104
def attribute(*names)
invalid_attribute_names = names.map(&:to_sym) & INVALID_ATTRIBUTE_NAMES
if invalid_attribute_names.any?
raise ArgumentError, "Restricted attribute names: #{invalid_attribute_names.join(", ")}"
end
ActiveSupport::CodeGenerator.batch(generated_attribute_methods, __FILE__, __LINE__) do |owner|
names.each do |name|
owner.define_cached_method(name, namespace: :current_attributes) do |batch|
batch <<
"def #{name}" <<
"attributes[:#{name}]" <<
"end"
end
owner.define_cached_method("#{name}=", namespace: :current_attributes) do |batch|
batch <<
"def #{name}=(value)" <<
"attributes[:#{name}] = value" <<
"end"
end
end
end
ActiveSupport::CodeGenerator.batch(singleton_class, __FILE__, __LINE__) do |owner|
names.each do |name|
owner.define_cached_method(name, namespace: :current_attributes_delegation) do |batch|
batch <<
"def #{name}" <<
"instance.#{name}" <<
"end"
end
owner.define_cached_method("#{name}=", namespace: :current_attributes_delegation) do |batch|
batch <<
"def #{name}=(value)" <<
"instance.#{name} = value" <<
"end"
end
end
end
end Related methods
- Instance methods
- reset
- set
- Class methods
- after_reset
- attribute
- before_reset
- clear_all
- instance
- new
- reset_all
- resets
- Private methods
-
current_instances -
current_instances_key -
generated_attribute_methods -
method_missing -
respond_to_missing? -
assign_attributes -
compute_attributes