Module: Utilrb::Models::Registration

Defined in:
lib/utilrb/models/registration.rb

Overview

Handling of registration of model hierarchies

It depends on the mixed-in object to provide a #supermodel method that returns the model that is parent of self

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#definition_locationArray<(String,Integer,Symbol)>

The place where this model got defined in the source code The tuple is (file,lineno,method), and can be obtained with facet’s #call_stack

Returns:



16
17
18
# File 'lib/utilrb/models/registration.rb', line 16

def definition_location
  @definition_location
end

Instance Method Details

#clear_submodelsObject

Clears all registered submodels



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/utilrb/models/registration.rb', line 67

def clear_submodels
    children = self.submodels.find_all { |m| !m.permanent_model? }
    if !deregister_submodels(children)
        return
    end

    # This contains the permanent submodels
    #
    # We can call #clear_submodels while iterating here as it is a
    # constraint that all models in #submodels are permanent (and
    # will therefore not be removed)
    submodels.each { |m| m.clear_submodels }
    # And this the non-permanent ones
    children.each { |m| m.clear_submodels }
    true
end

#deregister_submodels(set) ⇒ Object

Deregisters a set of submodels on this model and all its supermodels

This is usually not called directly. Use #clear_submodels instead

Parameters:

  • set (ValueSet)

    the set of submodels to remove



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/utilrb/models/registration.rb', line 90

def deregister_submodels(set)
    current_size = submodels.size
    submodels.difference!(set.to_value_set)
    if (submodels.size != current_size)
        if m = supermodel
            m.deregister_submodels(set)
        end
        true
    else false
    end
end

#each_submodelObject

Enumerates all models that are submodels of this class



59
60
61
62
63
64
# File 'lib/utilrb/models/registration.rb', line 59

def each_submodel
    return enum_for(:each_submodel) if !block_given?
    submodels.each do |obj|
        yield(obj)
    end
end

#inherited(subclass) ⇒ Object

Registers submodels when a subclass is created (when models are represented as classes)



104
105
106
107
108
109
# File 'lib/utilrb/models/registration.rb', line 104

def inherited(subclass)
    subclass.definition_location = call_stack
    super
    register_submodel(subclass)
    subclass.permanent_model = true
end

#permanent_model?Boolean

Tells #clear_submodels whether this model should be removed from the model set or not. The default is false (it should be removed)

Returns:

  • (Boolean)


22
# File 'lib/utilrb/models/registration.rb', line 22

attr_predicate :permanent_model?, true

#register_submodel(klass) ⇒ Object

Call to register a model that is a submodel of self



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/utilrb/models/registration.rb', line 38

def register_submodel(klass)
    if !klass.definition_location
        klass.definition_location = call_stack
    end

    if klass.name && !klass.permanent_model?
        begin
            if constant("::#{klass.name}") == klass
                klass.permanent_model = true
            end
        rescue NameError
        end
    end

    submodels << klass
    if m = supermodel
        m.register_submodel(klass)
    end
end

#supermodelObject

Returns the model that is parent of this one

The default implementation returns superclass if it is extended by this Registration module, and nil otherwise



31
32
33
34
35
# File 'lib/utilrb/models/registration.rb', line 31

def supermodel
    if superclass.respond_to?(:register_submodel)
        superclass
    end
end