Class: Asetus

Inherits:
Object
  • Object
show all
Defined in:
lib/asetus.rb,
lib/asetus/adapter/json.rb,
lib/asetus/adapter/toml.rb,
lib/asetus/adapter/yaml.rb,
lib/asetus/configstruct.rb

Overview

Examples:

common use case

CFGS = Asetus.new :name=>'my_sweet_program' :load=>false   # do not load config from filesystem
CFGS.default.ssh.port      = 22
CFGS.default.ssh.hosts     = %w(host1.example.com host2.example.com)
CFGS.default.auth.user     = lana
CFGS.default.auth.password = dangerzone
CFGS.load  # load system config and user config from filesystem and merge with defaults to #cfg
raise StandardError, 'edit ~/.config/my_sweet_program/config' if CFGS.create  # create user config from default config if no system or user config exists
# use the damn thing
CFG = CFGS.cfg
user      = CFG.auth.user
password  = CFG.auth.password
ssh_port  = CFG.ssh.port
ssh_hosts = CFG.ssh.hosts

Defined Under Namespace

Classes: Adapter, ConfigStruct

Constant Summary collapse

CONFIG_FILE =
'config'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cfgObject (readonly)

Returns the value of attribute cfg.



27
28
29
# File 'lib/asetus.rb', line 27

def cfg
  @cfg
end

#defaultObject (readonly)

Returns the value of attribute default.



27
28
29
# File 'lib/asetus.rb', line 27

def default
  @default
end

#fileObject (readonly)

Returns the value of attribute file.



27
28
29
# File 'lib/asetus.rb', line 27

def file
  @file
end

#systemObject

Returns the value of attribute system.



28
29
30
# File 'lib/asetus.rb', line 28

def system
  @system
end

#userObject

Returns the value of attribute user.



28
29
30
# File 'lib/asetus.rb', line 28

def user
  @user
end

Class Method Details

.cfg(*args) ⇒ Object



31
32
33
# File 'lib/asetus.rb', line 31

def cfg *args
  new(*args).cfg
end

Instance Method Details

#create(opts = {}) ⇒ boolean

Returns true if config didn’t exist and was created, false if config already exists.

Examples:

create user config from default config and raise error, if no config was found

raise StandardError, 'edit ~/.config/name/config' if asetus.create

Parameters:

  • opts (Hash) (defaults to: {})

    options for Asetus

Options Hash (opts):

  • :source (Symbol)

    source to use for settings to save, by defaylt :default

  • :destination (Symbol)

    destinatino to use for settings to save, by default :user

  • :load (boolean)

    load config once saved, by default false

Returns:

  • (boolean)

    true if config didn’t exist and was created, false if config already exists



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/asetus.rb', line 73

def create opts={}
  src   = opts.delete :source
  src ||= :default
  dst   = opts.delete :destination
  dst ||= :user
  no_config = false
  no_config = true if @system.empty? and @user.empty?
  if no_config
    src = instance_variable_get '@' + src.to_s
    instance_variable_set('@'+dst.to_s, src.dup)
    save dst
    load if opts.delete :load
  end
  no_config
end

#from_json(json) ⇒ Object



7
8
9
# File 'lib/asetus/adapter/json.rb', line 7

def from_json json
  Adapter::JSON.from json
end

#from_toml(toml) ⇒ Object



7
8
9
# File 'lib/asetus/adapter/toml.rb', line 7

def from_toml toml
  Adapter::TOML.from toml
end

#from_yaml(yaml) ⇒ Object



7
8
9
# File 'lib/asetus/adapter/yaml.rb', line 7

def from_yaml yaml
  Adapter::YAML.from yaml
end

#load(level = :all) ⇒ void

This method returns an undefined value.

When this is called, by default :system and :user are loaded from filesystem and merged with defefault, so that user overrides system which overrides default

Parameters:

  • level (Symbol) (defaults to: :all)

    which configuration level to load, by defaukt :all



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/asetus.rb', line 42

def load level=:all
  if level == :default or level == :all
    @cfg = merge @cfg, @default
  end
  if level == :system or level == :all
    @system = load_cfg @sysdir
    @cfg = merge @cfg, @system
  end
  if level == :user or level == :all
    @user = load_cfg @usrdir
    @cfg = merge @cfg, @user
  end
end

#save(level = :user) ⇒ void

This method returns an undefined value.

Parameters:

  • level (Symbol) (defaults to: :user)

    which configuration level to save, by default :user



58
59
60
61
62
63
64
# File 'lib/asetus.rb', line 58

def save level=:user
  if level == :user
    save_cfg @usrdir, @user
  elsif level == :system
    save_cfg @sysdir, @system
  end
end

#to_json(config) ⇒ Object



3
4
5
# File 'lib/asetus/adapter/json.rb', line 3

def to_json config
  Adapter::JSON.to config._asetus_to_hash
end

#to_toml(config) ⇒ Object



3
4
5
# File 'lib/asetus/adapter/toml.rb', line 3

def to_toml config
  Adapter::TOML.to config._asetus_to_hash
end

#to_yaml(config) ⇒ Object



3
4
5
# File 'lib/asetus/adapter/yaml.rb', line 3

def to_yaml config
  Adapter::YAML.to config._asetus_to_hash
end