Module: Weechat::Properties::InstanceMethods

Defined in:
lib/weechat/properties.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ String

method_missing returns buffer local variables.

Returns:



278
279
280
281
282
283
284
# File 'lib/weechat/properties.rb', line 278

def method_missing(m, *args)
  if args.empty? && valid_property?(m.to_s)
    get_property(m.to_s)
  else
    super
  end
end

Class Method Details

.alias_methods(type) ⇒ Object



304
305
306
307
# File 'lib/weechat/properties.rb', line 304

def self.alias_methods(type)
  alias_method "#{type}_get_integer", :get_integer
  alias_method "#{type}_get_string", :get_string
end

Instance Method Details

#__get_property(property) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/weechat/properties.rb', line 103

def __get_property(property)
  property = property.to_s
  if valid_property?(property, :integer)
    v = get_integer_property(property)
  elsif valid_property?(property, :string)
    v = get_string_property(property)
  elsif valid_property?(property, :infolist)
    v = get_infolist_property(property)
  else
    raise Exception::UnknownProperty, property
  end

  return self.class.apply_transformation(property, v)
end

#get_infolistHash{Symbol => Object}

Returns a hash representation of the associated infolist.

Returns:

  • (Hash{Symbol => Object})

    All properties in the infolist



170
171
172
# File 'lib/weechat/properties.rb', line 170

def get_infolist
  Weechat::Infolist.parse(self.class.type, @ptr)
end

#get_infolist_property(property) ⇒ String

Returns a property obtained by an infolist.



181
182
183
184
185
186
# File 'lib/weechat/properties.rb', line 181

def get_infolist_property(property)
  property = property.to_sym
  values = get_infolist.first
  raise Exception::UnknownProperty, property.to_s unless values.has_key?(property)
  values[property]
end

#get_integer(property) ⇒ Number

Returns an integer property, not doing any checks.

Returns:

  • (Number)

See Also:



138
139
140
# File 'lib/weechat/properties.rb', line 138

def get_integer(property)
  Weechat.__send__("#{self.class.type}_get_integer", @ptr, property.to_s).to_i
end

#get_integer_property(property) ⇒ Number

Returns an integer property.



126
127
128
129
130
# File 'lib/weechat/properties.rb', line 126

def get_integer_property(property)
  property = property.to_s
  raise Exception::UnknownProperty, property unless valid_property?(property, :integer)
  get_integer(property)
end

#get_property(property) ⇒ String, ...

Get a property. Transformations, if appropriate, will be applied to the value before returning it. This means that e.g. 0 and 1 might be turned into false and true.



92
93
94
95
96
97
98
99
100
# File 'lib/weechat/properties.rb', line 92

def get_property(property)
  raise Exception::UnknownProperty, property unless valid_property?(property)
  case ret = __get_property(property)
  when true, false, nil
    ret
  else
    Property.new(self, property)
  end
end

#get_string(property) ⇒ String

Returns a string property, not doing any checks.



163
164
165
# File 'lib/weechat/properties.rb', line 163

def get_string(property)
  Weechat.__send__("#{self.class.type}_get_string", @ptr, property.to_s)
end

#get_string_property(property) ⇒ String

Returns a string property.



150
151
152
153
154
# File 'lib/weechat/properties.rb', line 150

def get_string_property(property)
  property = property.to_s
  raise Exception::UnknownProperty, property unless valid_property?(property, :string)
  get_string(property)
end

#set(property, value) ⇒ Object

Sets a property, not doing any checks or conversions whatsoever.

Returns:

Raises:

  • (CannotSetProperties)

See Also:



240
241
242
243
244
245
# File 'lib/weechat/properties.rb', line 240

def set(property, value)
  set_method = "#{self.class.type}_set"
  raise CannotSetProperties unless Weechat.respond_to?(set_method)
  Weechat.__send__(set_method, @ptr, property.to_s, value.to_s)
  value
end

#set_property(property, v, freeze = false) ⇒ String, Integer

Sets a property. Transformations, if appropriate, will be applied to the value before setting it. This means that e.g. true and false will be turned into 1 and 0.

Returns:

Raises:

See Also:



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/weechat/properties.rb', line 208

def set_property(property, v, freeze = false)
  property = property.to_s
  raise Exception::UnsettableProperty, property unless settable_property?(property)
  v = Utilities.apply_transformation(property, v, self.class.rtransformations)

  set(property, v)
  if freeze
    ObjectSpace.each_object(Weechat::Property).each do |prop|
      if prop.__weechat_obj__.ptr == @ptr and prop.__property__ == property
        prop.__freeze__
      end
    end
  end
end

#set_string_property(property, v) ⇒ String

Sets a string property, not applying any transformations.

Returns:

Raises:

See Also:



229
230
231
232
233
# File 'lib/weechat/properties.rb', line 229

def set_string_property(property, v)
  property = property.to_s
  raise Exception::UnsettableProperty, property unless settable_property?(property)
  set(property, v)
end

#settable_property?(property) ⇒ Boolean

Checks if a property can be set.

Returns:

See Also:



193
194
195
196
197
198
199
# File 'lib/weechat/properties.rb', line 193

def settable_property?(property)
  set_method = "#{self.class.type}_set"
  return false unless Weechat.respond_to?(set_method)

  property = property.to_s
  self.class.settable_properties.include?(property)
end

#to_hHash{Symbol => Object}

Returns a Hash representation of the object.

Returns:



289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/weechat/properties.rb', line 289

def to_h
  h = {}
  self.class.known_properties.each do |property|
    val = __get_property(property)
    h[property.to_sym] = val
  end

  get_infolist.first.each do |property, value|
    prop = self.class.apply_transformation(property, value)
    h[property] = prop
  end

  h
end

#valid_property?(property, type = :all) ⇒ Boolean

Checks if a property is valid. That is, if get_(integer|string|infolist)_property are able to return a value.

Parameters:

  • property (#to_s)

    The name of the property

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

    The type of properties to check for. Can be one of :all, :string, :integer, :localvar or :infolist

Returns:

See Also:



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/weechat/properties.rb', line 256

def valid_property?(property, type = :all)
  property = property.to_s
  case type
  when :all
    valid_property?(property, :string) or
      valid_property?(property, :integer) or
      valid_property?(property, :localvar) or
      valid_property?(property, :infolist)
  when :string
    self.class.known_string_properties.include?(property) or valid_property?(property, :localvar)
  when :integer
    self.class.known_integer_properties.include?(property)
  when :localvar
    property =~ /^localvar_.+$/
  when :infolist
    get_infolist.first.has_key?(property.to_sym)
  end
end