]>
gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/plugin.rb
1 require 'common/domain'
4 # Methods that all plugins must provide. Certain operations -- for
5 # example, user listing -- must be supported by all plugins. These
6 # operations are defined here, often with naive default
7 # implementations, but it is up to each individual plugin to ensure
8 # that they are in fact implemented (well).
12 # These are class methods for runnable plugins, meant to be
13 # _extended_. Those runnable plugins get a magic *run* method but
14 # need to define their own *runner* and *dummy_runner* to make it
19 # A callback function, called whenever another class or module
20 # includes this one. This is used to build a list of all things
21 # that inherited this class. Having such a list lets us run a
22 # collection of plugins without knowing in advance what they are.
24 # @param c [Class,Module] the name of the class or module that
34 # Obtain the list of classes and modules that have included this one.
36 # @return [Array<Class,Module>] the list of classes and modules
37 # that have included this one.
45 # The runner class associated with this plugin. This method must
46 # be supplied by the child class, since they will all have
49 # @return [Class] the runner class associated with this plugin.
52 raise NotImplementedError
56 # The "dummy" runner class associated with this plugin. This method
57 # must be supplied by the child class, since they will all have
58 # different dummy runners.
60 # @return [Class] the dummy runner class associated with this
64 raise NotImplementedError
68 # Run all of the plugins that have included this module.
70 # @param cfg [Configuration] the configuration options to pass to
71 # each of the plugins.
73 # @param args [Array<Object>] a variable number of additional
74 # arguments to be passed to the plugins we're running.
77 includers().each
do |includer
|
78 plugin
= includer
.new(cfg
)
80 if cfg
.i_mean_business
then
81 runner
= runner().new()
83 runner
= dummy_runner().new()
86 # The splat passes the correct (we hope) number of arguments to the
87 # appropriate runner. The Rm(Dummy)Runner have splats on their
88 # *target arguments as well, to turn ARGV back into an array.
89 runner
.run(cfg
, plugin
, *args
)
95 # A generic version of {#describe_user}/{#describe_domain} that
96 # dispatches base on the class of the target.
98 # @param target [User,Domain] either a user or a domain to describe.
100 # @return [String] a string describing the *target*. The contents of
101 # the string depend on the plugin.
104 if target
.is_a
?(User
)
105 if user_exists(target
) then
106 return describe_user(target
)
108 return 'User not found'
110 elsif target
.is_a
?(Domain
)
111 if domain_exists(target
) then
112 return describe_domain(target
)
114 return 'Domain not found'
117 raise NotImplementedError
122 # Provide a description of the given *domain*. This is output along
123 # with the domain name and can be anything of use to the system
124 # administrator. The default doesn't do anything useful and should
125 # be overridden if possible.
127 # @param domain [Domain] the domain to describe.
129 # @return [String] a string description of *domain*.
131 def describe_domain(domain
)
136 # Provide a description of the given *user*. This is output along
137 # with the username and can be anything of use to the system
138 # administrator. The default doesn't do anything useful and should
139 # be overridden if possible.
141 # @param user [User] the domain to describe.
143 # @return [String] a string description of *user*.
145 def describe_user(user
)
150 # Return a list of all users managed by this plugin. This must be
151 # supplied by the individual plugins (who know how to find their
154 # @return [Array<User>] a list of the users that this plugin knows
158 raise NotImplementedError
162 # Return a list of all domains managed by this plugin. This must be
163 # supplied by the individual plugins (who know how to find their
164 # domains). Many plugins will not have a separate concept of
165 # "domain", so the default implementation constructs a list of
166 # domains resulting from {#list_users}.
168 # For plugins that do know about domains, smarter implementations
169 # are surely possible.
171 # @return [Array<Domain>] a list of the domains that this plugin knows
176 domains
= users
.map
{ |u
| u
.domain() }
177 return domains
.uniq()
181 # Does the given *user* exist for this plugin? We use a naive
182 # implementation here based on {#list_users}. Plugins should override
183 # this with something faster.
185 # @param user [User] the user whose existence is in question.
187 # @return [Boolean] true if *user* exists for this plugin, and
190 def user_exists(user
)
192 return users
.include?(user
)
196 # Does the given *domain* exist for this plugin? We use a naive
197 # implementation here based on {#list_domains}. Plugins that know
198 # about domains should override this with something fast.
200 # @param domain [Domain] the domain whose existence is in question.
202 # @return [Boolean] true if *domain* exists for this plugin, and
205 def domain_exists(domain
)
206 domains
= list_domains()
207 return domains
.include?(domain
)
211 # List all users belonging to the given domains. We say that a user
212 # belongs to a domain "example.com" if the domain part of the user's
213 # email address is "example.com".
215 # This uses a naive loop, but relies only on the existence of
216 # {#list_users}. Plugins that know about domains should provide a
217 # more efficient implementation.
219 # @param domains [Array<Domain>] the domains whose users we want.
221 # @return [Array<User>] a list of {User} objects belonging to
222 # *domains* for this plugin.
224 def list_domains_users(domains
)
227 users
= list_users();
229 matches
= users
.select
do |user
|
230 user
.domainpart() == d
.to_s()
233 domains_users +
= matches