Module: RubyExts::ImportMixin

Defined in:
lib/rubyexts/mixins/import.rb

Overview

This mixin will be included also to project or your custom app main namespace

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

class Project

extend Rango::ImportMixin

end



11
12
13
14
15
16
17
18
19
# File 'lib/rubyexts/mixins/import.rb', line 11

def self.extended(base)
  unless base.respond_to?(:root)
    class << base
      def root
        File.dirname(caller[1].split(":").first)
      end
    end
  end
end

.included(base) ⇒ Object

class Project

class << self
  extend Rango::ImportMixin
end

end



26
27
28
29
30
31
32
33
34
# File 'lib/rubyexts/mixins/import.rb', line 26

def self.included(base)
  unless base.respond_to?(:root)
    base.class_eval do
      def root
        File.dirname(caller.last.split(":").first)
      end
    end
  end
end

Instance Method Details

#find(file) ⇒ Object



78
79
80
81
82
# File 'lib/rubyexts/mixins/import.rb', line 78

def find(file)
  ["#{file}.rb", file].find do |file|
    File.exist?(file)
  end
end

#find_absolute(file) ⇒ Object



84
85
86
87
# File 'lib/rubyexts/mixins/import.rb', line 84

def find_absolute(file)
  file = File.join(self.root, file) unless file.match(%r[^/])
  self.find(file)
end

#import(path, options = Hash.new) ⇒ Boolean

Returns If loading suceed.

Examples:

Project.import("blog/views")
Project.import("blog/views", soft: true)

Parameters:

  • path (String)

    Path to file which will be loaded using Kernel#load if Project.settings.debug is true or Kernel#require if not.

  • options (Hash[soft: Boolean(default true)], @optional) (defaults to: Hash.new)

Returns:

  • (Boolean)

    If loading suceed.

Raises:

  • (LoadError)

    Unless soft: true option is used, it will raise LoadError if the file wasn’t found.

Since:

  • 0.0.1



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rubyexts/mixins/import.rb', line 44

def import(path, options = Hash.new)
  # it is better than rescue LoadError, because
  # LoadError can be raise inside the required file
  fullpath = self.find_absolute(path)
  if fullpath.nil? && options[:soft]   # any file found and soft importing enabled
    # do nothing
  elsif fullpath.nil? && !options[:soft]            # any file found and soft importing disabled
    raise LoadError, "File #{path.inspect} (treated as #{fullpath.inspect}) doesn't exist"
  elsif !fullpath.nil?                     # the file was found
    Kernel.load(fullpath)
  elsif !fullpath.nil?                    # the file was found
    Kernel.require(fullpath)
  end
end

#import!(path) ⇒ Boolean

Returns If the loading was successful.

Parameters:

  • path (String)

    Path to loaded file.

Returns:

  • (Boolean)

    If the loading was successful.

Since:

  • 0.0.1



72
73
74
75
76
# File 'lib/rubyexts/mixins/import.rb', line 72

def import!(path)
  path = File.join(self.root, path)
  file = self.find_absolute(path)
  Kernel.load(file)
end

#import_first(paths, options = Hash.new) ⇒ Object

Raises:

  • (LoadError)

Since:

  • 0.0.2



60
61
62
63
64
65
66
67
# File 'lib/rubyexts/mixins/import.rb', line 60

def import_first(paths, options = Hash.new)
  paths.each do |path|
    fullpath = self.find_absolute(path)
    next if fullpath.nil?
    return self.import(fullpath, options)
  end
  raise LoadError unless options[:soft]
end