Module: Formed::Inheritance::ClassMethods

Defined in:
lib/formed/inheritance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#abstract_classObject

Returns the value of attribute abstract_class.



55
56
57
# File 'lib/formed/inheritance.rb', line 55

def abstract_class
  @abstract_class
end

#base_classObject (readonly)

Returns the class descending directly from ActiveRecord::Base, or an abstract class, if any, in the inheritance hierarchy.

If A extends ActiveRecord::Base, A.base_class will return A. If B descends from A through some arbitrarily deep hierarchy, B.base_class will return A.

If B < A and C < B and if A is an abstract_class then both B.base_class and C.base_class would return B as the answer since A is an abstract_class.



47
48
49
# File 'lib/formed/inheritance.rb', line 47

def base_class
  @base_class
end

Instance Method Details

#abstract_class?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/formed/inheritance.rb', line 57

def abstract_class?
  defined?(@abstract_class) && @abstract_class == true
end

#base_class?Boolean

Returns whether the class is a base class. See #base_class for more information.

Returns:

  • (Boolean)


51
52
53
# File 'lib/formed/inheritance.rb', line 51

def base_class?
  base_class == self
end

#dupObject

:nodoc:



69
70
71
72
73
74
75
# File 'lib/formed/inheritance.rb', line 69

def dup # :nodoc:
  # `initialize_dup` / `initialize_copy` don't work when defined
  # in the `singleton_class`.
  other = super
  other.set_base_class
  other
end

#inherited(subclass) ⇒ Object



63
64
65
66
67
# File 'lib/formed/inheritance.rb', line 63

def inherited(subclass)
  subclass.set_base_class
  subclass.instance_variable_set(:@_type_candidates_cache, Concurrent::Map.new)
  super
end

#initialize_clone(other) ⇒ Object

:nodoc:



77
78
79
80
# File 'lib/formed/inheritance.rb', line 77

def initialize_clone(other) # :nodoc:
  super
  set_base_class
end

#new(attributes = nil, &block) ⇒ Object

Determines if one of the attributes passed in is the inheritance column, and if the inheritance column is attr accessible, it initializes an instance of the given subclass instead of the base class.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/formed/inheritance.rb', line 17

def new(attributes = nil, &block)
  if abstract_class? || self == Formed
    raise NotImplementedError, "#{self} is an abstract class and cannot be instantiated."
  end

  if _has_attribute?(inheritance_column)
    subclass = subclass_from_attributes(attributes)

    if subclass.nil? && (scope_attributes = current_scope&.scope_for_create)
      subclass = subclass_from_attributes(scope_attributes)
    end

    subclass = subclass_from_attributes(column_defaults) if subclass.nil? && base_class?
  end

  if subclass && subclass != self
    subclass.new(attributes, &block)
  else
    super
  end
end

#primary_abstract_classObject



61
# File 'lib/formed/inheritance.rb', line 61

def primary_abstract_class; end