loadable_component 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
@@ -35,4 +35,46 @@ class LoadableComponent
35
35
  "could not load #{self.class.name}"
36
36
  end
37
37
 
38
+ end
39
+
40
+ class SlowLoadableComponent < LoadableComponent
41
+ def initialize(timeout)
42
+ @timeout = timeout
43
+ end
44
+
45
+ def get
46
+ if loaded?
47
+ return self
48
+ end
49
+
50
+ end_time = Time.now + @timeout
51
+ until Time.now >= end_time
52
+ return self if loaded?
53
+ check_error
54
+ sleep sleep_interval
55
+ end
56
+
57
+ unless loaded?
58
+ raise UnableToLoadComponent, unable_to_load_message
59
+ end
60
+
61
+ self
62
+ end
63
+
64
+ #
65
+ # Override this method to check for well-known error cases, which
66
+ # means loading has finished, but an error condition was seen.
67
+ #
68
+
69
+ def check_error
70
+ # no-op by default
71
+ end
72
+
73
+ def sleep_interval
74
+ 0.2
75
+ end
76
+
77
+ def unable_to_load_message
78
+ "#{super} after #{@timeout} seconds"
79
+ end
38
80
  end
@@ -1,3 +1,3 @@
1
1
  class LoadableComponent
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- # specify any dependencies here; for example:
22
21
  s.add_development_dependency "rake"
23
- # s.add_runtime_dependency "rest-client"
22
+ s.add_development_dependency "rspec", "~> 2.0"
23
+ s.add_development_dependency "timecop"
24
24
  end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe LoadableComponent do
4
+
5
+ it "does nothing if component is already loaded" do
6
+ DetonatingComponent.new.get.should be_instance_of(DetonatingComponent)
7
+ end
8
+
9
+ it "calls #load if the component is not already loaded" do
10
+ ok = LoadsOk.new(true)
11
+ ok.get.should be_instance_of(LoadsOk)
12
+
13
+ ok.load_called?.should be_true
14
+ end
15
+
16
+ it "raises an error if calling load does not cause the component to load" do
17
+ ok = LoadsOk.new(false)
18
+ lambda { ok.get }.should raise_error(LoadableComponent::UnableToLoadComponent)
19
+ end
20
+
21
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe SlowLoadableComponent do
4
+
5
+ it "does nothing if component is already loaded" do
6
+ SlowlyDetonatingComponent.new.get.should be_instance_of(SlowlyDetonatingComponent)
7
+ end
8
+
9
+ it "calls the load method if the component is not already loaded" do
10
+ number_of_times_through_loop = 1
11
+ component = SlowLoading.new(number_of_times_through_loop).get
12
+ component.should be_instance_of(SlowLoading)
13
+
14
+ component.loop_count.should == number_of_times_through_loop
15
+ end
16
+
17
+ it "calls the load method once if the component takes a long time to load" do
18
+ OnlyOneLoad.new(5).get
19
+ end
20
+
21
+ it "should throw an error if calling load does not cause the component to load before timeout" do
22
+ lambda { BasicSlowLoader.new(1).get }.should raise_error(LoadableComponent::UnableToLoadComponent, /after 1 seconds/)
23
+ end
24
+
25
+ it "should cancel loading if an error is detected" do
26
+ lambda { HasError.new(1).get }.should raise_error(HasError::CustomError)
27
+ end
28
+
29
+ end
@@ -0,0 +1,120 @@
1
+ require 'rspec'
2
+ require 'loadable_component'
3
+ require 'timecop'
4
+
5
+ class DetonatingComponent < LoadableComponent
6
+ def load
7
+ raise "should never be called"
8
+ end
9
+
10
+ def loaded?
11
+ # already loaded
12
+ true
13
+ end
14
+ end
15
+
16
+ class LoadsOk < LoadableComponent
17
+ def initialize(second_load_call_passes)
18
+ @second_load_call_passes = second_load_call_passes
19
+ @load_called = false
20
+ end
21
+
22
+ def load
23
+ @load_called = true
24
+ end
25
+
26
+ def loaded?
27
+ unless @load_called
28
+ return false
29
+ end
30
+
31
+ unless @second_load_call_passes
32
+ return false
33
+ end
34
+
35
+ true
36
+ end
37
+
38
+ def load_called?
39
+ @load_called
40
+ end
41
+ end
42
+
43
+ class SlowlyDetonatingComponent < SlowLoadableComponent
44
+ def initialize
45
+ super(1)
46
+ end
47
+
48
+ def load
49
+ raise "should never be called"
50
+ end
51
+
52
+ def loaded?
53
+ true
54
+ end
55
+ end
56
+
57
+ class SlowLoading < SlowLoadableComponent
58
+ attr_reader :loop_count
59
+
60
+ def initialize(count)
61
+ @count = count
62
+ @loop_count = 0
63
+
64
+ super(1)
65
+ end
66
+
67
+ def load
68
+ # does nothing
69
+ end
70
+
71
+ def loaded?
72
+ if @loop_count > @count
73
+ false
74
+ else
75
+ @loop_count += 1
76
+ end
77
+ end
78
+ end
79
+
80
+ class OnlyOneLoad < SlowLoading
81
+ def initialize(count)
82
+ super(count)
83
+ @load_called = false
84
+ end
85
+
86
+ def load
87
+ if @load_called
88
+ raise "load already called"
89
+ end
90
+
91
+ @load_called = true
92
+ end
93
+ end
94
+
95
+ class BasicSlowLoader < SlowLoadableComponent
96
+ def load
97
+ # does nothing
98
+ end
99
+
100
+ def loaded?
101
+ Timecop.travel 1
102
+ false # never loads
103
+ end
104
+ end
105
+
106
+ class HasError < SlowLoadableComponent
107
+ class CustomError < StandardError; end
108
+
109
+ def load
110
+ # does nothing
111
+ end
112
+
113
+ def loaded?
114
+ false
115
+ end
116
+
117
+ def check_error
118
+ raise CustomError
119
+ end
120
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loadable_component
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jari Bakken
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-18 00:00:00 Z
18
+ date: 2011-12-30 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  type: :development
@@ -31,6 +31,35 @@ dependencies:
31
31
  prerelease: false
32
32
  name: rake
33
33
  version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ type: :development
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ hash: 3
42
+ segments:
43
+ - 2
44
+ - 0
45
+ version: "2.0"
46
+ prerelease: false
47
+ name: rspec
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ type: :development
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ prerelease: false
61
+ name: timecop
62
+ version_requirements: *id003
34
63
  description: Ruby implementation of http://code.google.com/p/selenium/wiki/LoadableComponent
35
64
  email:
36
65
  - jari.bakken@gmail.com
@@ -49,6 +78,9 @@ files:
49
78
  - lib/loadable_component.rb
50
79
  - lib/loadable_component/version.rb
51
80
  - loadable_component.gemspec
81
+ - spec/loadable_component_spec.rb
82
+ - spec/slow_loadable_component_spec.rb
83
+ - spec/spec_helper.rb
52
84
  homepage: http://github.com/jarib/loadable_component
53
85
  licenses: []
54
86
 
@@ -82,5 +114,7 @@ rubygems_version: 1.8.10
82
114
  signing_key:
83
115
  specification_version: 3
84
116
  summary: Ruby implementation of http://code.google.com/p/selenium/wiki/LoadableComponent
85
- test_files: []
86
-
117
+ test_files:
118
+ - spec/loadable_component_spec.rb
119
+ - spec/slow_loadable_component_spec.rb
120
+ - spec/spec_helper.rb