Module: Cinch::Syncable

Included in:
Channel, User
Defined in:
lib/cinch/syncable.rb

Overview

Provide blocking access to user/channel information.

Instance Method Summary collapse

Instance Method Details

#attr(attribute, data = false, unsync = false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • (defaults to: false)
  • (defaults to: false)

API:

  • private



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cinch/syncable.rb', line 62

def attr(attribute, data = false, unsync = false)
  unless unsync
    if @when_requesting_synced_attribute
      @when_requesting_synced_attribute.call(attribute)
    end
    wait_until_synced(attribute)
  end

  if data
    return @data[attribute]
  else
    return instance_variable_get("@#{attribute}")
  end
end

#attribute_synced?(attribute) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

API:

  • private



41
42
43
# File 'lib/cinch/syncable.rb', line 41

def attribute_synced?(attribute)
  @synced_attributes.include?(attribute)
end

#mark_as_synced(attribute)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

API:

  • private



79
80
81
# File 'lib/cinch/syncable.rb', line 79

def mark_as_synced(attribute)
  @synced_attributes << attribute
end

#sync(attribute, value, data = false)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

API:

  • private



30
31
32
33
34
35
36
37
# File 'lib/cinch/syncable.rb', line 30

def sync(attribute, value, data = false)
  if data
    @data[attribute] = value
  else
    instance_variable_set("@#{attribute}", value)
  end
  @synced_attributes << attribute
end

#unsync(attribute)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

API:

  • private



47
48
49
# File 'lib/cinch/syncable.rb', line 47

def unsync(attribute)
  @synced_attributes.delete(attribute)
end

#unsync_all

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Since:

  • 1.0.1

API:

  • private



54
55
56
# File 'lib/cinch/syncable.rb', line 54

def unsync_all
  @synced_attributes.clear
end

#wait_until_synced(attr)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Blocks until the object is synced.

API:

  • private



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cinch/syncable.rb', line 8

def wait_until_synced(attr)
  attr = attr.to_sym
  waited = 0
  while true
    return if attribute_synced?(attr)
    waited += 1

    if waited % 100 == 0
      bot.loggers.warn "A synced attribute ('%s' for %s) has not been available for %d seconds, still waiting" % [attr, self.inspect, waited / 10]
      bot.loggers.warn caller.map {|s| "  #{s}"}

      if waited / 10 >= 30
        bot.loggers.warn "  Giving up..."
        raise Exceptions::SyncedAttributeNotAvailable, "'%s' for %s" % [attr, self.inspect]
      end
    end
    sleep 0.1
  end
end