Module: Minitwin::ClassMethods::Caches

Included in:
Minitwin::ClassMethods
Defined in:
lib/minitwin/class_methods/caches.rb,
sig/generated/minitwin/class_methods/caches.rbs

Instance Method Summary collapse

Instance Method Details

#allowed_attribute_keysObject

Returns:

  • (Object)


71
72
73
74
75
76
77
78
# File 'lib/minitwin/class_methods/caches.rb', line 71

def allowed_attribute_keys
  # Setters defined directly on the twin class hierarchy. Uses the same
  # candidate set as serializable_getters so mixed-in module setters
  # (e.g. ActionView's #output_buffer=) are not treated as assignable
  # attributes.
  @allowed_attribute_keys ||= serializable_method_candidates.
                                grep(/=\z/).to_set { |m| m.to_s.delete_suffix("=").to_sym }
end

#allowed_attribute_keys_arrayObject

Returns:

  • (Object)


80
81
82
83
84
85
86
87
# File 'lib/minitwin/class_methods/caches.rb', line 80

def allowed_attribute_keys_array
  @allowed_attribute_keys_array ||= begin
    allowed = allowed_attribute_keys
    ordered = property_order.select { |k| allowed.include?(k) }
    remaining = allowed.to_a - ordered
    (ordered + remaining).freeze
  end
end

#declared_property_keysObject

Base names of every declared property and collection across the twin class hierarchy. Only methods whose declaration name is one of these serialize, so plain getters hand-defined on a twin are excluded. as: aliases pass because #original_name maps them back to the original property (the aliased reader is made protected and rejected separately).

Returns:

  • (Object)


58
59
60
61
62
63
# File 'lib/minitwin/class_methods/caches.rb', line 58

def declared_property_keys
  twin_class_hierarchy.each_with_object(Set.new) do |klass, keys|
    keys.merge(klass.properties.keys)
    keys.merge(klass.collections.keys)
  end
end

#dynamic_aliases?Boolean

: () -> bool

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
# File 'lib/minitwin/class_methods/caches.rb', line 9

def dynamic_aliases?
  return @has_dynamic_aliases_cache unless @has_dynamic_aliases_cache.nil?

  @has_dynamic_aliases_cache = begin
    procs = properties.any? { |_, m| m[:as].is_a?(Proc) } ||
      collections.any? { |_, m| m[:as].is_a?(Proc) }
    nested = respond_to?(:dynamic_nested_aliases) && dynamic_nested_aliases.any?
    procs || nested
  end
end

#invalidate_cachesObject

Returns:

  • (Object)


22
23
24
25
26
27
28
# File 'lib/minitwin/class_methods/caches.rb', line 22

def invalidate_caches
  @serializable_getters = nil
  @allowed_attribute_keys = nil
  @allowed_attribute_keys_array = nil
  @setter_methods = nil
  @has_dynamic_aliases_cache = nil
end

#serializable_gettersObject

Returns:

  • (Object)


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/minitwin/class_methods/caches.rb', line 30

def serializable_getters
  @serializable_getters ||= begin
    unexposed = unexposed_properties.to_set(&:to_sym)
    prot = (protected_instance_methods - Minitwin.protected_instance_methods).to_set
    declared = declared_property_keys
    own_and_inherited = serializable_method_candidates
    own_and_inherited.reject do |m|
      s = m.to_s
      s.end_with?("=", "?", "_attributes") || unexposed.include?(m) || prot.include?(m) ||
        !declared.include?(instance_method(m).original_name)
    end
  end
end

#serializable_method_candidatesObject

Methods defined directly on the twin class hierarchy: this class and any intermediate Minitwin subclasses, but not Minitwin itself. Methods mixed in via modules (e.g. ActionView helpers, which include arg-taking methods like #link_to) are excluded because instance_methods(false) reports only methods owned by the class, not by included modules.

Returns:

  • (Object)


49
50
51
# File 'lib/minitwin/class_methods/caches.rb', line 49

def serializable_method_candidates
  twin_class_hierarchy.flat_map { |klass| klass.instance_methods(false) }.uniq
end

#setter_methodsObject

Returns:

  • (Object)


89
90
91
# File 'lib/minitwin/class_methods/caches.rb', line 89

def setter_methods
  @setter_methods ||= public_instance_methods(false).select { |m| m.to_s.end_with?("=") }
end

#twin_class_hierarchyObject

This class and any intermediate Minitwin subclasses, but not Minitwin itself or mixed-in modules.

Returns:

  • (Object)


67
68
69
# File 'lib/minitwin/class_methods/caches.rb', line 67

def twin_class_hierarchy
  ancestors.take_while { |a| a != Minitwin }.select { |a| a.instance_of?(Class) }
end