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:



291
292
293
294
295
296
297
# File 'lib/weechat/properties.rb', line 291

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



317
318
319
320
# File 'lib/weechat/properties.rb', line 317

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/weechat/properties.rb', line 115

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_infolist(*fields) ⇒ Hash{Symbol => Object}

Returns a hash representation of the associated infolist.

Returns:

  • (Hash{Symbol => Object})

    All properties in the infolist



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

def get_infolist(*fields)
  Weechat::Infolist.parse(self.class.type, @ptr, "", {}, *fields)
end

#get_infolist_property(property) ⇒ String

Returns a property obtained by an infolist.



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

def get_infolist_property(property)
  property = property.to_sym
  values = get_infolist(property).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:



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

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.



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

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.



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

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.



175
176
177
# File 'lib/weechat/properties.rb', line 175

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.



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

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:

  • (Object)

    The value

Raises:

  • (CannotSetProperties)

See Also:



252
253
254
255
256
257
# File 'lib/weechat/properties.rb', line 252

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:



220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/weechat/properties.rb', line 220

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:



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

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:



205
206
207
208
209
210
211
# File 'lib/weechat/properties.rb', line 205

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:

  • (Hash{Symbol => Object})


302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/weechat/properties.rb', line 302

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:



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/weechat/properties.rb', line 268

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
    sproperty = property.to_sym
    get_infolist(sproperty).first.has_key?(sproperty)
  end
end