Class: MerbAdmin::AbstractModel
- Inherits:
-
Object
- Object
- MerbAdmin::AbstractModel
- Defined in:
- lib/abstract_model.rb,
lib/generic_support.rb,
lib/datamapper_support.rb,
lib/active_record_support.rb,
lib/sequel_support.rb
Defined Under Namespace
Modules: ActiverecordSupport, DatamapperSupport, GenericSupport, SequelSupport
Instance Attribute Summary collapse
-
#model ⇒ Object
Returns the value of attribute model.
Class Method Summary collapse
- .add_model(model_name) ⇒ Object
-
.all ⇒ Object
Returns all models for a given Merb app.
-
.lookup(model_name) ⇒ Object
Given a string
model_name
, finds the corresponding model class.
Instance Method Summary collapse
-
#initialize(model) ⇒ AbstractModel
constructor
A new instance of AbstractModel.
Constructor Details
#initialize(model) ⇒ AbstractModel
Returns a new instance of AbstractModel.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/abstract_model.rb', line 52 def initialize(model) model = self.class.lookup(model.to_s.camel_case) unless model.is_a?(Class) orm = Merb.orm @model = model self.extend(GenericSupport) case orm when :activerecord require 'active_record_support' self.extend(ActiverecordSupport) when :datamapper require 'datamapper_support' self.extend(DatamapperSupport) when :sequel require 'sequel_support' self.extend(SequelSupport) else raise "MerbAdmin does not support the #{orm} ORM" end end |
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
50 51 52 |
# File 'lib/abstract_model.rb', line 50 def model @model end |
Class Method Details
.add_model(model_name) ⇒ Object
26 27 28 29 |
# File 'lib/abstract_model.rb', line 26 def self.add_model(model_name) model = lookup(model_name) @models << new(model) if model end |
.all ⇒ Object
Returns all models for a given Merb app
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/abstract_model.rb', line 6 def self.all @models = [] orm = Merb.orm case orm when :activerecord, :sequel Dir.glob(Merb.dir_for(:model) / Merb.glob_for(:model)).each do |filename| File.read(filename).scan(/class ([\w\d_\-:]+)/).flatten.each do |model_name| add_model(model_name) end end when :datamapper DataMapper::Model.descendants.each do |model| add_model(model.to_s) end else raise "MerbAdmin does not support the #{orm} ORM" end @models.sort!{|x, y| x.model.to_s <=> y.model.to_s} end |
.lookup(model_name) ⇒ Object
Given a string model_name
, finds the corresponding model class
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/abstract_model.rb', line 32 def self.lookup(model_name) return nil if MerbAdmin[:excluded_models].include?(model_name) begin model = Object.full_const_get(model_name) rescue NameError raise "MerbAdmin could not find model #{model_name}" end case Merb.orm when :activerecord model if superclasses(model).include?(ActiveRecord::Base) when :sequel model if superclasses(model).include?(Sequel::Model) else model end end |