Class: Kangaroo::Util::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/kangaroo/util/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file_or_hash, logger = Logger.new(STDOUT)) ⇒ Configuration

Initialize the Kangaroo configuration

Parameters:

  • config_file_or_hash (Hash, String)

    Configuration filename or Hash

  • logger (Logger) (defaults to: Logger.new(STDOUT))

    logger instance



15
16
17
18
19
20
21
22
23
# File 'lib/kangaroo/util/configuration.rb', line 15

def initialize config_file_or_hash, logger = Logger.new(STDOUT)
  @logger = logger
  case config_file_or_hash
  when String
    configure_by_file config_file_or_hash
  when Hash
    configure_by_hash config_file_or_hash
  end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



9
10
11
# File 'lib/kangaroo/util/configuration.rb', line 9

def client
  @client
end

#databaseObject

Returns the value of attribute database.



9
10
11
# File 'lib/kangaroo/util/configuration.rb', line 9

def database
  @database
end

#loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/kangaroo/util/configuration.rb', line 9

def logger
  @logger
end

#modelsObject

Returns the value of attribute models.



9
10
11
# File 'lib/kangaroo/util/configuration.rb', line 9

def models
  @models
end

Instance Method Details

#configure_by_file(filename) ⇒ Object

Configure Kangaroo by YAML file

Parameters:

  • filename (String)

    Filename to load YAML configuration from



43
44
45
# File 'lib/kangaroo/util/configuration.rb', line 43

def configure_by_file filename
  configure_by_hash YAML.load_file(filename)
end

#configure_by_hash(config) ⇒ Object

Configure Kangaroo by Hash

Parameters:

  • config (Hash)

    configuration Hash

Options Hash (config):

  • 'host' (String)

    Hostname or IP address

  • 'port' (String, Number)

    Port

  • 'database' (Hash)

    Configuration for database



53
54
55
56
# File 'lib/kangaroo/util/configuration.rb', line 53

def configure_by_hash config
  @client = Client.new config.slice('host', 'port')
  configure_database config['database']
end

#configure_database(db_config) ⇒ Object

Configure an OpenERP database

Parameters:

  • db_config (Hash)

    database configuration hash

Options Hash (db_config):

  • 'name' (String)

    Name of database

  • 'user' (String)

    Username to use for authentication

  • 'password' (String)

    Password for authentication

  • 'models' (Enumerable)

    List of models(-patterns) to load



65
66
67
68
69
# File 'lib/kangaroo/util/configuration.rb', line 65

def configure_database db_config
  @database = Database.new @client, *db_config.values_at('name', 'user', 'password')
  @models =  db_config['models']
  logger.info %Q(Configured OpenERP database "#{db_config['name']}" at "#{client.connection.host}")
end

#load_modelsObject

Load configured models with Loader



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kangaroo/util/configuration.rb', line 27

def load_models
  if models.blank?
    logger.info "No models to load."
    return
  end

  
  Loader.new(models, @database).load!
  logger.info "Loaded OpenERP models matching #{models.inspect}."
rescue Exception => e
  logger.error "Loading of OpenERP models failed.\n#{e.inspect}"
end

#loginObject

Login to the configured database



73
74
75
76
77
78
79
# File 'lib/kangaroo/util/configuration.rb', line 73

def 
  if @database.
    logger.info %Q(Authenticated user "#{@database.user}" for OpenERP database "#{@database.db_name}")
  else
    logger.warn %Q(Login to OpenERP database "#{@database.db_name}" with user "#{@database.user}" failed!)
  end
end