Class: Lamed::ObjectLoader

Inherits:
Object
  • Object
show all
Extended by:
Lamed, Helper
Defined in:
lib/lamed/object_loader.rb

Constant Summary collapse

ORIG_OBJECT_CONSTANTS =
Object.constants.freeze
ROOT_EXT =
File.join(ROOT, "/ext")
VIEW_PATH =
File.join(ROOT_EXT, "/views")
FILE_FILTER =
{
  :model      => File.join(ROOT_EXT, "/models", file_pattern),
  :controller => File.join(ROOT_EXT, "/controllers", file_pattern),
  :view       => File.join(ROOT_EXT, "/views", file_pattern)
}

Constants included from Lamed

DB_OPTIONS, OPTIONS, SYS_OPTIONS, VERSION

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Helper

camelize_path, camelize_string, class_to_path, mysql_time, sorted_array, sorted_hash, sorted_weighted_hash, symbolize_hash_keys, uncamelize_path, uncamelize_string

Methods included from Lamed

initialize_logger, load_controller, load_lib, load_model, logger, opts

Class Attribute Details

.mapped_classObject (readonly)

Returns the value of attribute mapped_class.



23
24
25
# File 'lib/lamed/object_loader.rb', line 23

def mapped_class
  @mapped_class
end

Class Method Details

.camelize_ext_subdir(subdir) ⇒ Object



45
46
47
48
49
# File 'lib/lamed/object_loader.rb', line 45

def camelize_ext_subdir(subdir)
  ext_subdir = subdir.split(ROOT_EXT + "/controllers")[1]
  camelized_ext_subdir = camelize_path ext_subdir if ext_subdir
  return camelized_ext_subdir
end

.create_object_from_camelized_path(camelized_ext_subdir) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lamed/object_loader.rb', line 51

def create_object_from_camelized_path(camelized_ext_subdir)
  klass = Lamed::Controller
  if camelized_ext_subdir
    camelized_ext_subdir.each do |symbol|
      if klass.constants.include?(symbol)
        klass_prime = klass.const_get(symbol)
      else 
        klass_prime = klass.const_set(symbol, Module.new)
      end
      klass = klass_prime
    end
  end
  return klass
end

.create_view_path(camelized_ext_subdir) ⇒ Object



74
75
76
77
78
# File 'lib/lamed/object_loader.rb', line 74

def create_view_path(camelized_ext_subdir)
  camelized_ext_subdir_dup = camelized_ext_subdir.nil? ? [] : camelized_ext_subdir.dup
  view_path = File.join(VIEW_PATH, uncamelize_path(camelized_ext_subdir_dup))
  return view_path
end

.get_files(type) ⇒ Object

Load objects in this order:

Libraries - Lib
Records or models - Model
Controllers or apps - Controller


30
31
32
# File 'lib/lamed/object_loader.rb', line 30

def get_files(type)
  @paths = Dir.glob(FILE_FILTER[type])
end

.load_controller_into_new_class(subdir, file_name) ⇒ Object

Load new controller(s) into new klass(es) as defined by their path. First load the files then build the class/modules(klass) from the path. Then move the newly created objects into the newly built class. Finally, remove the newly loaded objects from Object. Change the path= for the new controllers to the location of the mustache templates VIEW_PATH.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/lamed/object_loader.rb', line 85

def load_controller_into_new_class(subdir, file_name)
  orig_object_constants = Object.constants.dup
  load_new_object(subdir, file_name)
  camelized_ext_subdir = camelize_ext_subdir(subdir)
  klass = create_object_from_camelized_path(camelized_ext_subdir)
  view_path = create_view_path(camelized_ext_subdir)
  (Object.constants - orig_object_constants).each do |o|
    o_klass = Object.const_get(o)
    # Change path to mustache path location if it has the path method (Check for a Controller object)
    o_klass.path = create_view_path(camelized_ext_subdir) if o_klass.respond_to?(:path)
    klass.const_set(o, o_klass)
    Object.instance_eval { remove_const o }
    complete_klass_str = klass.to_s + "::" + o_klass.to_s
    map_new_class(complete_klass_str) if o_klass.respond_to?(:path)
  end
end

.load_controller_objectObject



114
115
116
117
118
# File 'lib/lamed/object_loader.rb', line 114

def load_controller_object
  get_files(:controller)
  map_file_to_subdir
  @mapped_file.each_pair { |subdir, file_name| load_controller_into_new_class(subdir, file_name) }
end

.load_model_objectObject

Load model(s)



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/lamed/object_loader.rb', line 121

def load_model_object
  orig_object_constants = Object.constants.dup
  klass = Lamed::Model
  get_files(:model)
  @paths.each { |f| load f }
  (Object.constants - orig_object_constants).each do |o|
    o_klass = Object.const_get(o)
    klass.const_set(o, o_klass)
    Object.instance_eval { remove_const o }
  end
end

.load_new_object(subdir, file_name) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/lamed/object_loader.rb', line 66

def load_new_object(subdir, file_name)
  file_name.each { |f|
    file = File.join(subdir, f)
    load file
  }
  return nil
end

.map_class_to_path(path, klass_str) ⇒ Object



109
110
111
112
# File 'lib/lamed/object_loader.rb', line 109

def map_class_to_path(path, klass_str)
  @mapped_class = Hash.new unless defined?(@mapped_class)
  @mapped_class[path] = eval(klass_str)
end

.map_file_to_subdirObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/lamed/object_loader.rb', line 34

def map_file_to_subdir
  @mapped_file = Hash.new
  @paths.each { |path|
    path_parts = File.split(path)
    dir = path_parts[0]
    file_name = path_parts[1]
    @mapped_file[dir] = @mapped_file[dir].nil? ? [file_name] : @mapped_file[dir] << file_name
  }
  @mapped_file
end

.map_new_class(klass_str) ⇒ Object

Create a new map for the Controller using Rack::Builder map



103
104
105
106
107
# File 'lib/lamed/object_loader.rb', line 103

def map_new_class(klass_str)
  path_prime = File.join(klass_str.split('::').collect { |s| uncamelize_string s })
  path = path_prime.split('/controller').last
  map_class_to_path(path, klass_str)
end