Class: Rumbly::Model::ActiveRecord::Klass
- 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
Class Method Summary collapse
-
.all_from_base_descendents(app) ⇒ Object
Returns an array of
Klassobjects representingActiveRecordmodel classes (be they persistent or abstract) in the currently loaded environment. -
.all_from_polymorphic_associations(app) ⇒ Object
Returns an array of
Klassobjects representing “virtual” classes that are named as part of any polymorphic associations.
Instance Method Summary collapse
-
#abstract ⇒ Object
Returns
trueif thisRumbly::Model::ActiveRecord::Klassis abstract. -
#attributes ⇒ Object
Returns an array of
Rumbly::Model::ActiveRecord::Attributes, each of which describes an attribute of theActiveRecordclass for thisKlass. -
#cls ⇒ Object
Returns the
ActiveRecordmodel class associated with thisKlass. -
#initialize(app, cls, name = nil) ⇒ Klass
constructor
Initializes a new
Klassfrom the givenActiveModelmodel class. -
#name ⇒ Object
Returns the name of this
Rumbly::Model::ActiveRecord::Klass. -
#operations ⇒ Object
Returns nil, since
ActiveRecordmodels don’t declare their operations. -
#virtual ⇒ Object
Returns
trueif thisRumbly::Model::ActiveRecord::Klassis a “virtual” class, i.e.
Methods inherited from Klass
Methods included from Rumbly::Model::Abstract
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
#abstract ⇒ Object
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 |
#attributes ⇒ Object
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 |
#cls ⇒ Object
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 |
#name ⇒ Object
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 |
#operations ⇒ Object
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 |
#virtual ⇒ Object
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 |