Method: Quant::Attributes::InstanceMethods#initialize_attributes

Defined in:
lib/quant/attributes.rb

#initialize_attributesObject

Initializes the defined attributes with default values and defines accessor methods for each attribute. If a child class redefines a parent’s attribute, the child’s definition will be used.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/quant/attributes.rb', line 140

def initialize_attributes
  each_attribute do |name, entry|
    # use the child's definition, skipping the parent's
    next if respond_to?(name)

    ivar_name = "@#{name}"
    define_singleton_method(name) do
      return instance_variable_get(ivar_name) if instance_variable_defined?(ivar_name)

      # Sets the default value when accessed and ivar is not already set
      default_value_for(entry).tap { |value| instance_variable_set(ivar_name, value) }
    end
    define_singleton_method("#{name}=") { |value| instance_variable_set(ivar_name, value) }
  end
end