Class: Module

Inherits:
Object show all
Defined in:
lib/perennial/core_ext/misc.rb

Instance Method Summary collapse

Instance Method Details

#add_extension(name, &blk) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/perennial/core_ext/misc.rb', line 76

def add_extension(name, &blk)
  item = name.to_s.camelize.to_sym
  target = const_get(item) rescue nil
  raise "Didn't find library for #{name}" if target.nil?
  if target.is_a?(Class)
    target.class_eval(&blk)
  elsif target.is_a?(Module)
    target.module_eval(&blk)
  else
    raise "Unable to extend #{target.inspect}"
  end
end

#attempt_require(*files) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/perennial/core_ext/misc.rb', line 89

def attempt_require(*files)
  files.each do |file|
    begin
      require file
    rescue LoadError
    end
  end
end

#extends_library(*items) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/perennial/core_ext/misc.rb', line 65

def extends_library(*items)
  namespace = self.to_s.underscore
  items.each do |item|
    klass = item.to_s.camelize.to_sym
    # Load if it isn't loaded already.
    const_get(klass) unless const_defined?(klass)
    # And finally load the file.
    require File.join(namespace, item.to_s.underscore)
  end 
end

#has_library(*items) ⇒ Object



58
59
60
61
62
63
# File 'lib/perennial/core_ext/misc.rb', line 58

def has_library(*items)
  namespace = self.to_s.underscore
  items.each do |item|
    require File.join(namespace, item.to_s.underscore)
  end
end