Module: Metro::Models
Instance Method Summary collapse
-
#add(model) ⇒ Object
Add a model, and all it’s subclasses, to the list of available models.
-
#all_models_for(models) ⇒ Array
An array that contains the model itself and all of the models that are sub-classes of this model on down.
-
#find(name) ⇒ String
The name of the model class.
-
#list ⇒ Array<String>
All the names supported by the models hash.
-
#models_hash ⇒ Hash
A hash of the available models.
Instance Method Details
#add(model) ⇒ Object
Add a model, and all it’s subclasses, to the list of available models.
A model has several names added so that it accessible in many ways:
-
Model Class Name
-
Model Name
-
Model Name with slashes replaced with ‘::` separator
16 17 18 19 20 21 22 23 24 |
# File 'lib/metro/models/models.rb', line 16 def add(model) all_models_for(model).each do |model| models_hash[model.to_s] = model.to_s name_with_slashes = model.model_name models_hash[name_with_slashes] = model.to_s name_with_colons = name_with_slashes.gsub('/','::') models_hash[name_with_colons] = model.to_s end end |
#all_models_for(models) ⇒ Array
Returns an array that contains the model itself and all of the models that are sub-classes of this model on down.
54 55 56 57 58 59 |
# File 'lib/metro/models/models.rb', line 54 def all_models_for(models) Array(models).map do |model_class_name| model = model_class_name.constantize [ model ] + all_models_for(models.models) end.flatten.compact end |
#find(name) ⇒ String
Returns the name of the model class.
31 32 33 |
# File 'lib/metro/models/models.rb', line 31 def find(name) models_hash[name].constantize end |
#list ⇒ Array<String>
Returns all the names supported by the models hash.
38 39 40 |
# File 'lib/metro/models/models.rb', line 38 def list models_hash.keys end |
#models_hash ⇒ Hash
Returns a hash of the available models. The keys are the various supported names, with the values being the names of the model classes.
45 46 47 |
# File 'lib/metro/models/models.rb', line 45 def models_hash @models_hash ||= HashWithIndifferentAccess.new("Metro::UI::Generic") end |