Class: Dister::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/dister/options.rb

Overview

Class for handling user- and application-specific settings

Constant Summary collapse

SUSE_STUDIO_DOT_COM_API_PATH =

Default API path, used unless a custom path is specified

'https://susestudio.com/api/v2/user'
GLOBAL_PATH =

Path to global (per-user) options file

"#{File.expand_path('~')}/.dister"
LOCAL_PATH =

Path to app-specific options file

"#{Dister::Core::APP_ROOT}/.dister/options.yml"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(use_only_local = false) ⇒ Options

Read options from file.

Parameters:

  • use_only_local (Boolean) (defaults to: false)

    Use only local options?



20
21
22
23
# File 'lib/dister/options.rb', line 20

def initialize use_only_local=false
  @use_only_local = use_only_local
  reload
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args)

Provides setter and getter for all options.



26
27
28
29
30
31
32
33
34
35
# File 'lib/dister/options.rb', line 26

def method_missing(method, *args)
  method_name = method.to_s
  if (method_name =~ /=$/).nil?
    # Getter
    provide[method_name]
  else
    # Setter
    store(method_name[0..-2], args.first)
  end
end

Instance Attribute Details

#use_only_local (readonly)

Returns the value of attribute use_only_local.



15
16
17
# File 'lib/dister/options.rb', line 15

def use_only_local
  @use_only_local
end

Instance Method Details

#reload

Read @global and @local option files. Run this method if their contents has changed.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dister/options.rb', line 39

def reload
  if @use_only_local
    @global = {}
  else
    # Global options hold the user's credentials to access SUSE Studio.
    # They are stored inside the user's home directory.
    @global = read_options_from_file(GLOBAL_PATH)

    # make sure the default api path is available
    unless @global.has_key? 'api_path'
      @global['api_path'] = SUSE_STUDIO_DOT_COM_API_PATH
    end
  end
  # Local options hold application specific data (e.g. appliance_id)
  # They are stored inside the application's root directory.
  @local = read_options_from_file(LOCAL_PATH)
end