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



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/perennial/core_ext/misc.rb', line 71

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



84
85
86
87
88
89
90
91
# File 'lib/perennial/core_ext/misc.rb', line 84

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

#extends_library(*items) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/perennial/core_ext/misc.rb', line 60

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



53
54
55
56
57
58
# File 'lib/perennial/core_ext/misc.rb', line 53

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