Module: RVM::PluginHost

Included in:
Classes, Functions, Languages
Defined in:
lib/rvm/plugin.rb

Overview

Thanks a lot to Murphy, this code is borrowed from coderay.

Constant Summary collapse

PluginNotFound =

Raised if Encoders::[] fails because:

  • a file could not be found

  • the requested Encoder is not registered

Class.new Exception
HostNotFound =
Class.new Exception
PLUGIN_HOSTS =
[]
PLUGIN_HOSTS_BY_ID =

dummy hash

{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(mod) ⇒ Object

Adds the module/class to the PLUGIN_HOSTS list.



60
61
62
# File 'lib/rvm/plugin.rb', line 60

def extended mod
  PLUGIN_HOSTS << mod
end

.host_by_id(host_id) ⇒ Object

Find the PluginHost for host_id.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rvm/plugin.rb', line 70

def host_by_id host_id
  unless PLUGIN_HOSTS_BY_ID.default_proc
    ph = Hash.new do |h, a_host_id|
      for host in PLUGIN_HOSTS
        h[host.host_id] = host
      end
      h.fetch a_host_id, nil
    end
    PLUGIN_HOSTS_BY_ID.replace ph
  end
  PLUGIN_HOSTS_BY_ID[host_id]
end

.included(mod) ⇒ Object

Warns you that you should not #include this module.



65
66
67
# File 'lib/rvm/plugin.rb', line 65

def included mod
  warn "#{name} should not be included. Use extend."
end

Instance Method Details

#[](id, *args, &blk) ⇒ Object Also known as: load

Returns the Plugin for id.

Example:

yaml_plugin = MyPluginHost[:yaml]


46
47
48
49
50
51
52
# File 'lib/rvm/plugin.rb', line 46

def [] id, *args, &blk
  plugin = validate_id(id)
  begin
    plugin = plugin_hash.[] plugin, *args, &blk
  end while plugin.is_a? Symbol
  plugin
end

#default(id) ⇒ Object

Define the default plugin to use when no plugin is found for a given id.

See also map.

class MyColorHost < PluginHost
  map :navy => :dark_blue
  default :gray
end


122
123
124
125
# File 'lib/rvm/plugin.rb', line 122

def default id
  id = validate_id id
  plugin_hash[nil] = id
end

#has?(id) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/rvm/plugin.rb', line 146

def has? id
  plugin_hash.has_key? id
end

#host_idObject

The host’s ID.

If PLUGIN_HOST_ID is not set, it is simply the class name.



88
89
90
91
92
93
94
# File 'lib/rvm/plugin.rb', line 88

def host_id
  if self.const_defined? :PLUGIN_HOST_ID
    self::PLUGIN_HOST_ID
  else
    name
  end
end

#inspectObject

Makes a map of all loaded plugins.



151
152
153
154
155
156
157
# File 'lib/rvm/plugin.rb', line 151

def inspect
  map = plugin_hash.dup
  map.each do |id, plugin|
    map[id] = plugin.to_s[/(?>[\w_]+)$/]
  end
"#{name}[#{host_id}]#{map.inspect}"
end

#map(hash) ⇒ Object

Map a plugin_id to another.

Usage: Put this in a file plugin_path/_map.rb.

class MyColorHost < PluginHost
  map :navy => :dark_blue,
    :maroon => :brown,
    :luna => :moon
end


105
106
107
108
109
110
111
# File 'lib/rvm/plugin.rb', line 105

def map hash
  for from, to in hash
    from = validate_id from
    to = validate_id to
    plugin_hash[from] = to unless plugin_hash.has_key? from
  end
end

#plugin_hashObject

A Hash of plugion_id => Plugin pairs.



142
143
144
# File 'lib/rvm/plugin.rb', line 142

def plugin_hash
  @plugin_hash ||= create_plugin_hash
end

#register(plugin, *ids) ⇒ Object

Every plugin must register itself for one or more ids by calling register_for, which calls this method.

See Plugin#register_for.



131
132
133
134
135
136
137
138
139
# File 'lib/rvm/plugin.rb', line 131

def register plugin, *ids
  for id in ids
    unless id.is_a? Symbol
      raise ArgumentError,
      "id must be a Symbol, but it was a #{id.class}"
    end
    plugin_hash[validate_id(id)] = plugin
  end
end