lib/belajar/loadable.rb in belajar-0.1.1 vs lib/belajar/loadable.rb in belajar-1.0.0
- old
+ new
@@ -1,23 +1,32 @@
module Belajar
module Loadable
-
- require 'active_support/inflector'
-
def load(path)
- if Dir.exist?(path)
- dirs = Dir.entries(path).select do |entry|
- !entry.match(/\./)
- end
+ return [] unless Dir.exist?(path)
- dirs.sort.map do |dir|
- dir_path = File.join(path, dir)
- class_name = self.to_s.demodulize.singularize
- "Belajar::#{class_name}".constantize.new(dir_path)
- end
- else
- Array.new
+ dirs = Dir.entries(path).select do |entry|
+ !entry.match(/\./)
end
+
+ dirs.sort.map do |dir|
+ dir_path = File.join(path, dir)
+ module_name = demodulize(to_s)
+ class_name = singularize(module_name)
+ belajar_class(class_name).new(dir_path)
+ end
end
+ private
+
+ def demodulize(string)
+ string.split('::').last
+ end
+
+ def singularize(string)
+ string.end_with?('s') ? string[0..-2] : string
+ end
+
+ def belajar_class(name)
+ Kernel.const_get("Belajar::#{name}")
+ end
end
end