Class: CouchShell::PluginInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/couch-shell/plugin.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin_name, plugin_class) ⇒ PluginInfo

Returns a new instance of PluginInfo.



123
124
125
126
127
128
# File 'lib/couch-shell/plugin.rb', line 123

def initialize(plugin_name, plugin_class)
  @plugin_class = plugin_class
  @plugin_name = plugin_name
  @variables = []
  @commands = {}
end

Instance Attribute Details

#commandsObject (readonly)

Map of name/CommandInfo instances.



121
122
123
# File 'lib/couch-shell/plugin.rb', line 121

def commands
  @commands
end

#plugin_classObject (readonly)

Returns the value of attribute plugin_class.



116
117
118
# File 'lib/couch-shell/plugin.rb', line 116

def plugin_class
  @plugin_class
end

#plugin_nameObject (readonly)

Returns the value of attribute plugin_name.



117
118
119
# File 'lib/couch-shell/plugin.rb', line 117

def plugin_name
  @plugin_name
end

#variablesObject (readonly)

Enumerable of VariableInfo instances.



119
120
121
# File 'lib/couch-shell/plugin.rb', line 119

def variables
  @variables
end

Class Method Details

.[](class_or_name) ⇒ Object



110
111
112
# File 'lib/couch-shell/plugin.rb', line 110

def [](class_or_name)
  @by_class_map[class_or_name] || @by_name_map[class_or_name]
end

.class_name_to_plugin_name(class_name) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/couch-shell/plugin.rb', line 68

def class_name_to_plugin_name(class_name)
  String.new.tap do |plugin_name|
    was_uppercase = false
    extract = class_name[/[^:]+\z/].sub(/Plugin\z/, '')
    i = 0
    lastcase = nil
    while i < extract.length
      c = extract[i]
      if UnicodeUtils.uppercase_char? c
        if lastcase != :upper
          if i > 0 && plugin_name[-1] != "_"
            plugin_name << "_"
          end
          lastcase = :upper
        end
        plugin_name << UnicodeUtils.downcase(c, nil)
      elsif UnicodeUtils.lowercase_char? c
        if lastcase == :upper &&
             (plugin_name.length > 1 && plugin_name[-2] != "_")
          plugin_name.insert(-2, "_")
        end
        lastcase = :lower
        plugin_name << c
      else
        lastcase = nil
        plugin_name << c
      end
      i = i + 1
    end
  end
end

.register(plugin_class) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/couch-shell/plugin.rb', line 100

def register(plugin_class)
  plugin_name = class_name_to_plugin_name(plugin_class.name)
  unless plugin_name =~ /\A\p{Alpha}(\p{Alpha}|\p{Digit}|_)*\z/
    raise "invalid plugin name #{plugin_name}"
  end
  pi = PluginInfo.new(plugin_name, plugin_class)
  @by_class_map[plugin_class] = pi
  @by_name_map[plugin_name] = pi
end

Instance Method Details

#register_command(ci) ⇒ Object



130
131
132
133
# File 'lib/couch-shell/plugin.rb', line 130

def register_command(ci)
  raise "command #{ci.name} already registered" if @commands[ci]
  @commands[ci.name] = ci
end

#register_variable(variable_info) ⇒ Object



135
136
137
# File 'lib/couch-shell/plugin.rb', line 135

def register_variable(variable_info)
  @variables << variable_info
end