Sha256: 59c4572deffb55d138ecaedbf2db774214ebf970d4f949d3d5f342ba3fbcd00a

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

require "pathname"

module Chanko
  class Loader
    class << self
      def load(unit_name)
        new(unit_name).load
      end

      def cache
        @cache ||= {}
      end
    end

    def initialize(name)
      @name = name
    end

    def load
      if loaded?
        load_from_cache
      else
        load_from_file
      end
    end

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

    def load_from_cache
      cache[@name]
    end

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

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

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

    def directory_path
      Config.units_directory_path
    end

    def constantize
      @name.to_s.camelize.constantize
    end

    def cache
      self.class.cache
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
chanko-2.0.2 lib/chanko/loader.rb
chanko-2.0.1 lib/chanko/loader.rb
chanko-2.0.0 lib/chanko/loader.rb