# Adhearsion, open source technology integrator # Copyright 2006 Jay Phillips # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. class Definition def initialize(*constraints) @options = {} if constraints then @constraints = constraints.flatten! else @constraints = [] end end attr_reader :options # The method_missing method is a crown-jewel of Ruby. When a method is # called on an object that doesn't exist, Ruby will invoke the method_missing # method. The Kernel module holds the initial definition of this method and # the Ruby interpreter raises a NameError when Kernel#method_missing is # invoked. def method_missing(name, *args) # Let's ensure the names stay simple super unless name.to_s =~ /^[a-z][\w_]*=?$/i || args.empty? @options[name] = args.simplify end end # The user(), group() and use_users_and_group() methods are presently alpha and # really shouldn't be used yet. They're experimental ways of establishing user # and group objects in the database.rb file. def user &options establish_connection :internal her = Definition.new yield her User.create her.options end def group &options establish_connection :internal her = Definition.new yield her Group.create her.options end def use_users_and_groups clause estabish_connection :external case clause.class.inspect.to_sym when :Hash group_table = clause[:from].delete :group_table user_table = clause[:from].delete :user_table # XXX: Note: User.table doesn't exist! Needs fixing! User.table = user_table if user_table Group.table = group_table if group_table establish_connection :external, clause[:from] when :String || :File clause = File.open clause, 'a' if clause.is_a? String raise ArgumentError.new("Not a directory!") unless clause.directory? else raise ArgumentError.new("Wrong argument type!") end end alias use_groups_and_users use_users_and_groups