Class: RWB::Builder
- Inherits:
-
Object
- Object
- RWB::Builder
- Defined in:
- lib/rwb.rb
Instance Attribute Summary collapse
-
#total_weight ⇒ Object
readonly
Returns the value of attribute total_weight.
-
#urls ⇒ Object
readonly
Returns the value of attribute urls.
Instance Method Summary collapse
- #add_url(weight, url) ⇒ Object
- #add_url_group(weight, base, extension_array) ⇒ Object
- #build_urls(tests) ⇒ Object
- #get_url(seed = nil) ⇒ Object
-
#initialize(testhash = nil) ⇒ Builder
constructor
A new instance of Builder.
Constructor Details
#initialize(testhash = nil) ⇒ Builder
Returns a new instance of Builder.
173 174 175 176 177 178 179 |
# File 'lib/rwb.rb', line 173 def initialize(testhash = nil) @urls = Hash.new @total_weight = 0 if testhash build_urls(testhash) end end |
Instance Attribute Details
#total_weight ⇒ Object (readonly)
Returns the value of attribute total_weight.
171 172 173 |
# File 'lib/rwb.rb', line 171 def total_weight @total_weight end |
#urls ⇒ Object (readonly)
Returns the value of attribute urls.
171 172 173 |
# File 'lib/rwb.rb', line 171 def urls @urls end |
Instance Method Details
#add_url(weight, url) ⇒ Object
181 182 183 184 |
# File 'lib/rwb.rb', line 181 def add_url(weight, url) @total_weight += weight.to_i @urls[@total_weight] = Url.new(weight, url) end |
#add_url_group(weight, base, extension_array) ⇒ Object
186 187 188 189 |
# File 'lib/rwb.rb', line 186 def add_url_group(weight, base, extension_array) @total_weight += weight.to_i @urls[@total_weight] = UrlGroup.new(weight, base, extension_array) end |
#build_urls(tests) ⇒ Object
203 204 205 206 207 |
# File 'lib/rwb.rb', line 203 def build_urls(tests) tests.keys.each do |key| add_url(tests[key], key) end end |
#get_url(seed = nil) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/rwb.rb', line 191 def get_url(seed = nil) if seed srand seed end pick = rand(@total_weight) @urls.keys.sort.each do |key| if pick <= key return @urls[key] end end end |