Class: BucketMaker::Bucket
- Inherits:
-
Object
- Object
- BucketMaker::Bucket
- Defined in:
- lib/bucket_maker/bucket.rb
Overview
Class which holds all the Bucket information
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
Check if there is a group in this bucket.
-
#initialize(name, options = {}) ⇒ Bucket
constructor
Initializer.
-
#is_bucketable?(bucketable) ⇒ Boolean
Check if the Bucketable conforms to the pre-conditions.
-
#random_group ⇒ String
Randomize and get the group for this bucket.
Constructor Details
#initialize(name, options = {}) ⇒ Bucket
Initializer
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/bucket_maker/bucket.rb', line 21 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.
5 6 7 |
# File 'lib/bucket_maker/bucket.rb', line 5 def created_after @created_after end |
#distributions ⇒ Object
Returns the value of attribute distributions.
5 6 7 |
# File 'lib/bucket_maker/bucket.rb', line 5 def distributions @distributions end |
#distributions_percent ⇒ Object (readonly)
Returns the value of attribute distributions_percent.
10 11 12 |
# File 'lib/bucket_maker/bucket.rb', line 10 def distributions_percent @distributions_percent end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/bucket_maker/bucket.rb', line 5 def name @name end |
#summary ⇒ Object
Returns the value of attribute summary.
5 6 7 |
# File 'lib/bucket_maker/bucket.rb', line 5 def summary @summary end |
Instance Method Details
#has_group?(group_name) ⇒ Boolean
Check if there is a group in this bucket
90 91 92 |
# File 'lib/bucket_maker/bucket.rb', line 90 def has_group?(group_name) @distributions[group_name.to_sym] != nil if group_name end |
#is_bucketable?(bucketable) ⇒ Boolean
Check if the Bucketable conforms to the pre-conditions
81 82 83 |
# File 'lib/bucket_maker/bucket.rb', line 81 def is_bucketable?(bucketable) bucketable.created_at >= @created_after rescue false end |
#random_group ⇒ String
Randomize and get the group for this bucket
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/bucket_maker/bucket.rb', line 43 def random_group # Is set after first randomization unless @distributions_percent # Get the total value of the distributions # @denominator = @distributions.inject(0) do |result, (_, dist_value)| result + dist_value end # Populate the variable with Distribution Percentages # @distributions_percent = @distributions.inject({}) do |result, (dist_name, dist_value)| result[dist_name.to_sym] = (dist_value * 100.0)/@denominator result end # Change the Distribution Percentages with Ranges for easy checks # @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 # Randomize within the 0..max_range # randomized = rand(@denominator * 100) # Find where the randomized number falls in the calculated range # @distributions_percent.detect do |_, percent_range| percent_range.include?(randomized) end.first end |