class Commands::Init::InitModel
Base class of ‘initializers’.
This actually acts more as a registry of defined classes.
Attributes
model_classes[RW]
Public Class Methods
inheritable_attributes(*args)
click to toggle source
A lot of our settings are actually class instance variables, which can be overridden. This allows the child classes that depend on these values to define what they are.
# File lib/commands/init/init_model.rb, line 39 def self.inheritable_attributes(*args) @inheritable_attributes += args args.each do |arg| class_eval %( class << self; attr_accessor :#{arg} end ) end @inheritable_attributes end
inherited(subclass)
click to toggle source
# File lib/commands/init/init_model.rb, line 49 def self.inherited(subclass) # Copy 'down' any inheritable attribute to the new child class @inheritable_attributes.each do |inheritable_attribute| instance_var = "@#{inheritable_attribute}" subclass.instance_variable_set(instance_var, instance_variable_get(instance_var)) end if subclass.respond_to?(:abstract) && subclass.send(:abstract) InitModel.model_classes.push(subclass) end end
run(p4port, auto)
click to toggle source
# File lib/commands/init/init_model.rb, line 61 def self.run(p4port, auto) system_settings = nil super_user = nil models = [] InitModel.model_classes.each do |model| if model <= DefaultSuperUser # do nothing elsif model <= SystemSettingsModel system_settings = model.new elsif !super_user and model <= UserModel and model.super super_user = model.new else models << model.new end end if !super_user super_user = DefaultSuperUser.new end models.sort! { |a, b| a.rank <=> b.rank } # Clear out any P4 Environment variables ENV.keys.select { |x| x =~ /^P4/ }.each { |x| ENV.delete(x) } p4 = P4.new p4.port = p4port p4.connect p4.exception_level = P4::RAISE_ERRORS p4.charset = 'auto' if auto if system_settings system_settings.execute(p4, super_user) else p4.user = super_user.login p4.password = super_user.password results = p4.run_login('-p') p4.password = results.first puts "log in as super, ticket is #{p4.password}" end models.each { |m| m.execute(p4, models, super_user) } end
Public Instance Methods
execute(p4)
click to toggle source
It’s likely that the main child class will handle overriding this method. Our default method does nothing.
# File lib/commands/init/init_model.rb, line 17 def execute(p4) end
rank()
click to toggle source
Child classes should define this number greater than one. 0 should be reserved for a single system settings instance.
# File lib/commands/init/init_model.rb, line 11 def rank 1 end