Sha256: 2b189e91e5962471824ffb5ad549e120cd9b0ff6ac4ef510aa464697524ff2c3

Contents?: true

Size: 931 Bytes

Versions: 1

Compression:

Stored size: 931 Bytes

Contents

require "pathname"

module Nyanko
  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
      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
      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
    end

    def cache
      self.class.cache
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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