Module: Monkey::Backend::AbstractBackend

Defined in:
lib/monkey/backend.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#backend_nameObject

Returns the value of attribute backend_name


11
12
13
# File 'lib/monkey/backend.rb', line 11

def backend_name
  @backend_name
end

#backend_pathObject

Returns the value of attribute backend_path


11
12
13
# File 'lib/monkey/backend.rb', line 11

def backend_path
  @backend_path
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)

13
14
15
16
17
18
# File 'lib/monkey/backend.rb', line 13

def available?
  return true if Object.const_defined? backend_name
  $LOADED_FEATURES.any? do |f|
    f =~ /^(.*lib\/)?#{backend_path}|#{backend_path}(-[^\/]+)?\/lib/
  end
end

#expects_module(name) ⇒ Object


44
45
46
47
48
49
50
51
52
# File 'lib/monkey/backend.rb', line 44

def expects_module(name)
  name.split("::").inject(Object) do |parent, name|
    if name.empty?
      parent
    else
      parent.class_eval "module #{name}; self; end"
    end
  end
end

#gem_nameObject


71
72
73
# File 'lib/monkey/backend.rb', line 71

def gem_name
  @gem_name ||= name[/[^:]*$/].downcase
end

#load_libs(*data) ⇒ Object Also known as: load_lib


25
26
27
# File 'lib/monkey/backend.rb', line 25

def load_libs(*data)
  load_with_prefix backend_path, data
end

#load_with_prefix(prefix, libs = nil) ⇒ Object


31
32
33
34
35
36
37
38
# File 'lib/monkey/backend.rb', line 31

def load_with_prefix(prefix, libs = nil)
  case libs
  when String, Symbol then require File.join(prefix.to_s, libs.to_s)
  when Array then libs.each { |lib| load_with_prefix prefix, lib }
  when Hash then libs.each { |k, v| load_with_prefix File.join(prefix.to_s, k.to_s), v }
  else raise ArgumentError, "cannot handle #{libs.inspect}"
  end
end

#missing(*libs) ⇒ Object


40
41
42
# File 'lib/monkey/backend.rb', line 40

def missing(*libs)
  load_with_prefix "monkey/backend/common", libs
end

#setup_completeObject


20
21
22
23
# File 'lib/monkey/backend.rb', line 20

def setup_complete
  require backend_path
  setup
end

#version(default = "0") ⇒ Object


54
55
56
57
58
59
60
61
# File 'lib/monkey/backend.rb', line 54

def version(default = "0")
  return version(nil) || default unless default.nil?
  return @version if @version
  return unless defined? Gem
  Gem.send :attr_accessor, :loaded_specs
  return unless Gem.loaded_specs.respond_to? :[]
  @version = Gem.loaded_specs[gem_name].version.to_s if Gem.loaded_specs.include? gem_name
end

#version!Object


63
64
65
# File 'lib/monkey/backend.rb', line 63

def version!
  version(nil) or raise RuntimeError, "unable to determine backend version"
end

#version?Boolean

Returns:

  • (Boolean)

67
68
69
# File 'lib/monkey/backend.rb', line 67

def version?
  !!version(false)
end