Class: Chanko::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/chanko/loader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Loader

Returns a new instance of Loader.



21
22
23
# File 'lib/chanko/loader.rb', line 21

def initialize(name)
  @name = name
end

Class Method Details

.cacheObject



10
11
12
# File 'lib/chanko/loader.rb', line 10

def cache
  @cache ||= {}
end

.eager_load_units!Object



14
15
16
17
18
# File 'lib/chanko/loader.rb', line 14

def eager_load_units!
  Pathname.glob("#{Rails.root}/#{Config.units_directory_path}/*").select(&:directory?).each do |path|
    load(path.to_s.split("/").last.to_sym) rescue nil
  end
end

.load(unit_name) ⇒ Object



6
7
8
# File 'lib/chanko/loader.rb', line 6

def load(unit_name)
  new(unit_name).load
end

Instance Method Details

#add_autoload_pathObject



50
51
52
53
# File 'lib/chanko/loader.rb', line 50

def add_autoload_path
  ActiveSupport::Dependencies.autoload_paths << autoload_path
  ActiveSupport::Dependencies.autoload_paths.uniq!
end

#autoload_pathObject



55
56
57
# File 'lib/chanko/loader.rb', line 55

def autoload_path
  Rails.root.join("#{directory_path}/#@name").to_s
end

#cacheObject



67
68
69
# File 'lib/chanko/loader.rb', line 67

def cache
  self.class.cache
end

#constantizeObject



63
64
65
# File 'lib/chanko/loader.rb', line 63

def constantize
  @name.to_s.camelize.constantize
end

#directory_pathObject



59
60
61
# File 'lib/chanko/loader.rb', line 59

def directory_path
  Config.units_directory_path
end

#loadObject



25
26
27
28
29
30
31
# File 'lib/chanko/loader.rb', line 25

def load
  if loaded?
    load_from_cache
  else
    load_from_file
  end
end

#load_from_cacheObject



37
38
39
# File 'lib/chanko/loader.rb', line 37

def load_from_cache
  cache[@name]
end

#load_from_fileObject



41
42
43
44
45
46
47
48
# File 'lib/chanko/loader.rb', line 41

def load_from_file
  add_autoload_path
  cache[@name] = constantize
rescue Exception => exception
  ExceptionHandler.handle(exception)
  cache[@name] = false
  nil
end

#loaded?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/chanko/loader.rb', line 33

def loaded?
  cache[@name] != nil
end