Module: SlowLoadableComponent

Includes:
LoadableComponent
Defined in:
lib/loadable_component.rb

Constant Summary

Constants included from LoadableComponent

LoadableComponent::VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LoadableComponent

#load, #loaded?

Instance Attribute Details

#timeoutObject

Returns the value of attribute timeout.



42
43
44
# File 'lib/loadable_component.rb', line 42

def timeout
  @timeout
end

Instance Method Details

#check_errorObject

Override this method to check for well-known error cases, which means loading has finished, but an error condition was seen.



72
73
74
# File 'lib/loadable_component.rb', line 72

def check_error
  # no-op by default
end

#getObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/loadable_component.rb', line 44

def get
  if loaded?
    return self
  end

  load

  end_time = Time.now
  end_time += @timeout if @timeout

  until Time.now >= end_time
    return self if loaded?
    check_error
    sleep sleep_interval
  end

  unless loaded?
    raise UnableToLoadComponent, unable_to_load_message
  end

  self
end

#sleep_intervalObject



76
77
78
# File 'lib/loadable_component.rb', line 76

def sleep_interval
  0.2
end

#unable_to_load_messageObject



80
81
82
# File 'lib/loadable_component.rb', line 80

def unable_to_load_message
  "#{super} after #{@timeout} seconds"
end