module Cattri::InternalStore

def __cattri_set_variables

Returns:
  • (Set) -
def __cattri_set_variables
  @__cattri_set_variables ||= Set.new
end

def __cattri_store

Returns:
  • (Hash) -
def __cattri_store
  @__cattri_store ||= {}
end

def cattri_variable_defined?(key)

Returns:
  • (Boolean) - true if a value is present

Parameters:
  • key (String, Symbol) -- the attribute name or instance variable
def cattri_variable_defined?(key)
  __cattri_store.key?(normalize_ivar(key))
end

def cattri_variable_get(key)

Returns:
  • (Object, nil) - the stored value, or nil if not present

Parameters:
  • key (String, Symbol) -- the attribute name or instance variable
def cattri_variable_get(key)
  __cattri_store[normalize_ivar(key)]&.value
end

def cattri_variable_memoize(key, final: false)

Raises:
  • (Cattri::AttributeError) - if attempting to overwrite a final value

Returns:
  • (Object) - the existing or newly memoized value

Other tags:
    Yieldreturn: - the value to memoize if not already present

Parameters:
  • final (Boolean) -- whether to mark the value as final (immutable once set)
  • key (String, Symbol) -- the attribute name or instance variable
def cattri_variable_memoize(key, final: false)
  key = normalize_ivar(key)
  return cattri_variable_get(key) if cattri_variable_defined?(key)
  value = yield
  cattri_variable_set(key, value, final: final)
end

def cattri_variable_set(key, value, final: false)

Returns:
  • (Object) - the stored value

Parameters:
  • final (Boolean) -- whether the value should be locked as final
  • value (Object) -- the value to store
  • key (String, Symbol) -- the attribute name or instance variable
def cattri_variable_set(key, value, final: false)
  key = normalize_ivar(key)
  guard_final!(key)
  __cattri_store[key] = AttributeValue.new(value, final)
  __cattri_set_variables << key
  value
end

def guard_final!(key)

Raises:
  • (Cattri::AttributeError) - if the key is final and already set

Parameters:
  • key (Symbol) --
def guard_final!(key)
  return unless cattri_variable_defined?(key)
  existing = __cattri_store[key]
  raise Cattri::AttributeError, "Cannot modify final attribute :#{key}" if existing.final
end

def normalize_ivar(key)

Returns:
  • (Symbol) -

Parameters:
  • key (String, Symbol) --
def normalize_ivar(key)
  key.to_s.delete_prefix("@").to_sym.freeze
end