Class: BucketMaker::Bucket
- Inherits:
-
Object
- Object
- BucketMaker::Bucket
- Defined in:
- lib/bucket_maker/bucket.rb
Constant Summary collapse
- BUCKET_DESCRIPTION =
'description'- BUCKET_USER_AFTER =
'created_after'- BUCKET_DISTRIBUTION =
'distributions'
Instance Attribute Summary collapse
-
#created_after ⇒ Object
Returns the value of attribute created_after.
-
#distributions ⇒ Object
Returns the value of attribute distributions.
-
#distributions_percent ⇒ Object
readonly
Returns the value of attribute distributions_percent.
-
#name ⇒ Object
Returns the value of attribute name.
-
#summary ⇒ Object
Returns the value of attribute summary.
Instance Method Summary collapse
- #has_group?(group_name) ⇒ Boolean
-
#initialize(name, options = {}) ⇒ Bucket
constructor
A new instance of Bucket.
- #is_bucketable?(bucketable) ⇒ Boolean
- #random_group ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Bucket
Returns a new instance of Bucket.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/bucket_maker/bucket.rb', line 14 def initialize(name, ={}) @name = name.to_sym @summary = [BUCKET_DESCRIPTION] @created_after = if [BUCKET_USER_AFTER] DateTime.parse([BUCKET_USER_AFTER]) else DateTime.parse("1st Jan 1900") end @distributions_percent = nil @denominator = 1 @distributions = [BUCKET_DISTRIBUTION].inject({}) do |result, (dist_name, )| result[dist_name.to_sym] = result end if [BUCKET_DISTRIBUTION] end |
Instance Attribute Details
#created_after ⇒ Object
Returns the value of attribute created_after.
3 4 5 |
# File 'lib/bucket_maker/bucket.rb', line 3 def created_after @created_after end |
#distributions ⇒ Object
Returns the value of attribute distributions.
3 4 5 |
# File 'lib/bucket_maker/bucket.rb', line 3 def distributions @distributions end |
#distributions_percent ⇒ Object (readonly)
Returns the value of attribute distributions_percent.
8 9 10 |
# File 'lib/bucket_maker/bucket.rb', line 8 def distributions_percent @distributions_percent end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/bucket_maker/bucket.rb', line 3 def name @name end |
#summary ⇒ Object
Returns the value of attribute summary.
3 4 5 |
# File 'lib/bucket_maker/bucket.rb', line 3 def summary @summary end |
Instance Method Details
#has_group?(group_name) ⇒ Boolean
61 62 63 |
# File 'lib/bucket_maker/bucket.rb', line 61 def has_group?(group_name) @distributions[group_name.to_sym] != nil end |
#is_bucketable?(bucketable) ⇒ Boolean
57 58 59 |
# File 'lib/bucket_maker/bucket.rb', line 57 def is_bucketable?(bucketable) bucketable.created_at >= @created_after end |
#random_group ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bucket_maker/bucket.rb', line 33 def random_group # Is set after first randomization unless @distributions_percent @denominator = @distributions.inject(0) do |result, (_, dist_value)| result + dist_value end @distributions_percent = @distributions.inject({}) do |result, (dist_name, dist_value)| result[dist_name.to_sym] = (dist_value * 100.0)/@denominator result end @distributions_percent.inject(0) do |starter, (dist_name, percent_value)| ender = starter + percent_value @distributions_percent[dist_name.to_sym] = (starter .. ender) ender end end randomized = rand(@denominator * 100) @distributions_percent.find do |_, percent_range| percent_range.include?(randomized) end.first end |