class ActiveSupport::CurrentAttributes
def attribute(*names, default: NOT_SET)
constructed. Otherwise, the value will be duplicated with +#dup+.
is a proc or lambda, it will be called whenever an instance is
* :default - The default value for the attributes. If the value
==== Options
Declares one or more attributes that will be given both class and instance accessor methods.
def attribute(*names, default: NOT_SET) 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 Delegation.generate(singleton_class, names, to: :instance, nilable: false, signature: "") Delegation.generate(singleton_class, names.map { |n| "#{n}=" }, to: :instance, nilable: false, signature: "value") self.defaults = defaults.merge(names.index_with { default }) end