Sha256: 02571538145e44cb6ae28a7937a031dbab47815c902115f2722e8d05dd822559

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

module SweetLoader
  module ClassMethods
    attr_writer :default_mode

    def translate name, options = {}    
      names = name.split('/')
      ns = namespaces.merge(options[:namespaces] || options[:ns] || {})
      names.map do |name|
        clazz_name = name.to_s.camelize
        folder = ns[clazz_name.to_sym] ? ns[clazz_name.to_sym] : name
        folder.sub /\/$/, ''
      end.join('/')
    end

    def root
      @root ||= ''
    end

    def root= root
      raise ArgumentError, "Must be a String, was: #{root}" if !root.kind_of?(String)
      @root = root
    end

    def namespaces
      @namespaces ||= {}
    end

    def namespaces= namespaces
      raise ArgumentError, "Must be a Hash, was: #{namespaces}" if !namespaces.kind_of?(Hash)
      @namespaces = namespaces
    end

    def mode
      @mode || default_mode
    end

    def mode= mode 
      @mode = mode if valid_mode? mode
    end

    def valid_mode? mode
      valid_modes.include? mode
    end

    def valid_modes
      [:autoload, :require]
    end
  
    def default_mode
      valid_modes.first
    end
  end
  extend ClassMethods
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sweetloader-0.1.6 lib/sweetloader/class_methods.rb
sweetloader-0.1.5 lib/sweetloader/class_methods.rb