Class: Rays::AppModule::Manager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/rays/models/appmodule/manager.rb

Overview

Facade class for application modules.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.



34
35
36
# File 'lib/rays/models/appmodule/manager.rb', line 34

def initialize
  @module_types = {}
end

Instance Attribute Details

#module_typesObject (readonly)

Returns the value of attribute module_types.



32
33
34
# File 'lib/rays/models/appmodule/manager.rb', line 32

def module_types
  @module_types
end

Instance Method Details

#all(type = nil) ⇒ Object

Finds all modules in the current project. If type is specified it will find only the modules of a given type.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rays/models/appmodule/manager.rb', line 91

def all(type=nil)
  app_modules = []
  module_classes = []
  if type.nil?
    @module_types.each_value do |module_class|
      module_classes << module_class
    end
  else
    module_classes = @module_types[type] unless @module_types[type].nil?
  end

  module_classes.each do |module_class|
    base_path = module_class.base_path
    Utils::FileUtils.find_down(base_path, '\.module$').each do |descriptor_file|
      app_modules << get_module_from_descriptor(descriptor_file)
    end
  end

  app_modules
end

#create(type, name, generator = nil) ⇒ Object

Create a new module or initialize if a directory exists but no .module file.



115
116
117
118
119
120
# File 'lib/rays/models/appmodule/manager.rb', line 115

def create(type, name, generator=nil)
  module_type_class = @module_types[type]
  raise RaysException.new ("Cannot find module type #{type}") if module_type_class.nil?
  module_instance = module_type_class.new name
  module_instance.create generator
end

#get(type, name) ⇒ Object

Find an application module by the given type and name. Returns application module object or nil if no module is found.



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rays/models/appmodule/manager.rb', line 65

def get(type, name)
  app_module = nil
  module_class = @module_types[type]
  unless module_class.nil?
    base_path = module_class.base_path
    in_directory(base_path) do
      if File.exists?(File.join(base_path, "#{name}/.module")) or 'application'.eql? name
        app_module = module_class.new(name)
      end
    end
  end
  app_module
end

#get_from_descriptor(descriptor_file) ⇒ Object

Instantiate a module from a given descriptor file.



82
83
84
85
# File 'lib/rays/models/appmodule/manager.rb', line 82

def get_from_descriptor(descriptor_file)
  return nil unless File.exists?(descriptor_file)
  get_module_from_descriptor(descriptor_file)
end

#get_from_path(path) ⇒ Object

Instantiate module which has a given directory



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rays/models/appmodule/manager.rb', line 49

def get_from_path(path)
  return nil if path.nil? or !path.start_with?($rays_config.project_root)

  module_instance = nil
  directory = Utils::FileUtils.find_up '.module', path, $rays_config.project_root
  unless directory.nil?
    module_instance = get_module_from_descriptor(File.join(directory, '.module'))
  end

  module_instance
end

#register_module_type(type, module_class) ⇒ Object

Registering new application module type. Normally used from inside base application module.



42
43
44
# File 'lib/rays/models/appmodule/manager.rb', line 42

def register_module_type(type, module_class)
  @module_types[type] = module_class
end