Sha256: 75aaf2b1747b839fecfbdb018e4e7870ebe6ead174933b1ca49ddd883b833ae5

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

require "pathname"

module Nyanko
  class Loader
    UnitNotFound = Class.new(StandardError)

    class << self
      def load(unit_name)
        new(unit_name).load
      end

      def cache
        @cache ||= {}
      end
    end

    def initialize(name)
      @name = name
    end

    def load
      loaded? ? load_from_cache : load_from_file
    end

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

    def load_from_cache
      cache[@name]
    end

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

    def require_unit_files
      paths.each {|path| require Rails.root.join(path) }
    end

    def paths
      Pathname.glob("#{directory_path}/#@name/#@name.rb").sort
    end

    def directory_path
      Config.units_directory_path
    end

    def constantize
      @name.to_s.camelize.constantize
    rescue NameError
      raise UnitNotFound, "The unit for #{@name.inspect} is not found"
    end

    def cache
      self.class.cache
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nyanko-0.0.3 lib/nyanko/loader.rb