Class: Weechat::Script::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/weechat/script/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(specification) ⇒ Config

Returns a new instance of Config.



4
5
6
7
# File 'lib/weechat/script/config.rb', line 4

def initialize(specification)
  @specification = specification
  @script        = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/weechat/script/config.rb', line 130

def method_missing(m, *args)
  ms = m.to_s
  if ms[-1..-1] != '='
    if @specification.has_key?(ms)
      return get!(ms)
    end
  else
    if @specification.has_key?(ms[0..-2])
      return set!(ms[0..-2], args[0], true)
    end
  end

  super
end

Instance Method Details

#__get(option) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/weechat/script/config.rb', line 100

def __get(option)
  if set?(option)
    return @specification[option][0].from_weechat_config(Weechat.config_get_plugin(option))
  else
    return @specification[option][1]
  end
end

#clear!void

This method returns an undefined value.

Unsets all options.



51
52
53
# File 'lib/weechat/script/config.rb', line 51

def clear!
  @specification.keys.each {|key| unset!(key)}
end

#get!(option) ⇒ Object

Returns an option. If it isn’t set, return the default value.

Parameters:

Returns:



91
92
93
94
95
96
97
98
# File 'lib/weechat/script/config.rb', line 91

def get!(option)
  case ret = __get(option)
  when true, false, nil
    ret
  else
    Option.new(self, option)
  end
end

#hook!(evaluate = true, &callback) ⇒ Object

Hook for config changes



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/weechat/script/config.rb', line 23

def hook!(evaluate = true, &callback)
  @specification.each do |config, spec|
    type    = spec[0]
    default = spec[1]
    Weechat::Hooks::Config.new("plugins.var.ruby.#{@script}.#{config}") {|config, value|
      if evaluate
        value = type.from_weechat_config(value) rescue default
      end
      callback.call(config, value)
    }
  end
end

#init!Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/weechat/script/config.rb', line 9

def init!
  populate!
  hook!(false) {|config, value|
    config.gsub!(/^plugins\.var\.ruby\.#{@script}\./, '')
    spec = @specification[config]
    begin
      spec[0].from_weechat_config(value)
    rescue
      set!(config, spec[1])
    end
  }
end

#populate!void

This method returns an undefined value.

This will set the default values for all unset options.



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

def populate!
  @specification.each do |key, value|
    if not set?(key)
      set!(key, value[1])
    end
  end
end

#reset!void

This method returns an undefined value.

Resets all options to their default.



43
44
45
46
# File 'lib/weechat/script/config.rb', line 43

def reset!
  clear!
  populate!
end

#set!(option, value, freeze = false) ⇒ Weechat::CONFIG_OPTION_SET_OK_CHANGED, ...

Sets the value of an option.

Parameters:

Returns:

  • (Weechat::CONFIG_OPTION_SET_OK_CHANGED, Weechat::CONFIG_OPTION_SET_OK_SAME_VALUE, Weechat::CONFIG_OPTION_SET_OPTION_NOT_FOUND, Weechat::CONFIG_OPTION_SET_ERROR)

    Integer denoting in how far setting the option worked.

See Also:



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/weechat/script/config.rb', line 118

def set!(option, value, freeze = false)
  value = value.to_weechat_config
  Weechat.config_set_plugin(option.to_s, value)
  if freeze
    ObjectSpace.each_object(Weechat::Option).each do |opt|
      if opt.__config__ == self and opt.__option__ == option
        opt.__freeze__
      end
    end
  end
end

#set?(option) ⇒ Boolean

Checks if an option is set.

Parameters:

Returns:



70
71
72
# File 'lib/weechat/script/config.rb', line 70

def set?(option)
  Weechat.integer_to_bool(Weechat.config_is_set_plugin(option.to_s))
end

#set_script_name!(name) ⇒ Object



36
37
38
# File 'lib/weechat/script/config.rb', line 36

def set_script_name!(name)
  @script = name
end

#unset!(option) ⇒ Weechat::CONFIG_OPTION_UNSET_OK_NO_RESET, ...

Unsets an option.

Parameters:

  • The (#to_s)

    option to unset

Returns:

  • (Weechat::CONFIG_OPTION_UNSET_OK_NO_RESET, Weechat::CONFIG_OPTION_UNSET_RESET, Weechat::CONFIG_OPTION_UNSET_REMOVED, Weechat::CONFIG_OPTION_UNSET_ERROR)

    Integer denoting in how far unsetting the option worked.

See Also:



83
84
85
# File 'lib/weechat/script/config.rb', line 83

def unset!(option)
  Weechat.config_unset_plugin(option.to_s)
end