Class: Rumbly::Model::ActiveRecord::Klass

Inherits:
Klass
  • Object
show all
Defined in:
lib/rumbly/model/active_record/klass.rb

Overview

This class is an ActiveRecord-specific implementation of the abstract Rumbly::Model::Klass class used to represent model classes within the currently loaded environment. All model class, both persistent and abstract, are modeled as Klass objects. Also, “virtual” classes (more like interfaces) that are named as part of any polymorphic associations are also modeled as Klasses. These objects have a name but no underlying ActiveRecord model class.

Constant Summary

Constants inherited from Klass

Klass::ATTRIBUTES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Klass

#<=>, #abstract?, #virtual?

Methods included from Rumbly::Model::Abstract

#stub_required_methods

Constructor Details

#initialize(app, cls, name = nil) ⇒ Klass

Initializes a new Klass from the given ActiveModel model class. Keeps a back pointer to the top-level Application object. For “virtual” classes (see above), the cls will be nil and the name will be explicitly given.



49
50
51
52
53
# File 'lib/rumbly/model/active_record/klass.rb', line 49

def initialize (app, cls, name=nil)
  @app = app
  @cls = cls
  @name = name
end

Class Method Details

.all_from_base_descendents(app) ⇒ Object

Returns an array of Klass objects representing ActiveRecord model classes (be they persistent or abstract) in the currently loaded environment.



21
22
23
24
25
# File 'lib/rumbly/model/active_record/klass.rb', line 21

def all_from_base_descendents (app)
  ::ActiveRecord::Base.descendants.select do
    |cls| class_valid?(cls) 
  end.map { |cls| new(app, cls) }
end

.all_from_polymorphic_associations(app) ⇒ Object

Returns an array of Klass objects representing “virtual” classes that are named as part of any polymorphic associations. These virtual classes are more like interfaces, but we model them as Klasses for the purposes of showing them in a UML class diagram.



31
32
33
34
35
# File 'lib/rumbly/model/active_record/klass.rb', line 31

def all_from_polymorphic_associations (app)
  Relationship.associations_matching(app, :belongs_to, :polymorphic).map do |a|
    new(app, nil, a.name)
  end
end

Instance Method Details

#abstractObject

Returns true if this Rumbly::Model::ActiveRecord::Klass is abstract.



85
86
87
# File 'lib/rumbly/model/active_record/klass.rb', line 85

def abstract
  @abstract ||= (@cls.nil? ? false : @cls.abstract_class?)
end

#attributesObject

Returns an array of Rumbly::Model::ActiveRecord::Attributes, each of which describes an attribute of the ActiveRecord class for this Klass. Don’t bother to lookup attributes if this Klass represents an abstract model class or is a “virtual” class (interface) stemming from a polymorphic association.



71
72
73
74
75
76
77
# File 'lib/rumbly/model/active_record/klass.rb', line 71

def attributes
  @attributes ||= if @cls.nil? or self.abstract?
    []
  else
    Attribute.all_from_klass(self)
  end
end

#clsObject

Returns the ActiveRecord model class associated with this Klass. Should only be used by other Rumbly::Model::ActiveRecord classes (but no way in Ruby to enforce that). May be nil if this is a “virtual” class (see above).



58
59
60
# File 'lib/rumbly/model/active_record/klass.rb', line 58

def cls
  @cls
end

#nameObject

Returns the name of this Rumbly::Model::ActiveRecord::Klass.



63
64
65
# File 'lib/rumbly/model/active_record/klass.rb', line 63

def name
  @name ||= @cls.name
end

#operationsObject

Returns nil, since ActiveRecord models don’t declare their operations.



80
81
82
# File 'lib/rumbly/model/active_record/klass.rb', line 80

def operations
  nil
end

#virtualObject

Returns true if this Rumbly::Model::ActiveRecord::Klass is a “virtual” class, i.e. one stemming from a polymorphic association (more like an interface).



91
92
93
# File 'lib/rumbly/model/active_record/klass.rb', line 91

def virtual
  @virtual ||= @cls.nil?
end