Module: Metro::Models

Extended by:
Models
Included in:
Models
Defined in:
lib/metro/models/models.rb

Instance Method Summary collapse

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.

Parameters:

  • models (Class, Array<Class>)

    a model or array of models.

Returns:

  • (Array)

    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.

Parameters:

  • name (String)

    the name of the model that you want to return.

Returns:

  • (String)

    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

#listArray<String>

Returns all the names supported by the models hash.

Returns:

  • (Array<String>)

    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_hashHash

Returns a hash of the available models. The keys are the various supported names, with the values being the names of the model classes.

Returns:

  • (Hash)

    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