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



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



31
32
33
# File 'lib/metro/models/models.rb', line 31

def find(name)
  models_hash[name].constantize
end

#listArray<String>



38
39
40
# File 'lib/metro/models/models.rb', line 38

def list
  models_hash.keys
end

#models_hashHash



45
46
47
# File 'lib/metro/models/models.rb', line 45

def models_hash
  @models_hash ||= HashWithIndifferentAccess.new("Metro::UI::Generic")
end