Class: BucketMaker::Bucket

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Bucket

Initializer

Parameters:

  • name (string)

    Name of the Bucket

  • options (Hash) (defaults to: {})

    Options for the Bucket like BUCKET_DESCRIPTION, BUCKET_USER_AFTER, BUCKET_DISTRIBUTION



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, options={})
  @name = name.to_sym
  @summary = options[BUCKET_DESCRIPTION]

  @created_after =  if options[BUCKET_USER_AFTER]
                            DateTime.parse(options[BUCKET_USER_AFTER])
                          else
                            DateTime.parse("1st Jan 1900")
                          end

  @distributions_percent = nil
  @denominator = 1
  @distributions = options[BUCKET_DISTRIBUTION].inject({}) do |result, (dist_name, dist_options)|
                    result[dist_name.to_sym] = dist_options
                    result
                  end if options[BUCKET_DISTRIBUTION]
end

Instance Attribute Details

#created_afterObject

Returns the value of attribute created_after.



5
6
7
# File 'lib/bucket_maker/bucket.rb', line 5

def created_after
  @created_after
end

#distributionsObject

Returns the value of attribute distributions.



5
6
7
# File 'lib/bucket_maker/bucket.rb', line 5

def distributions
  @distributions
end

#distributions_percentObject (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

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/bucket_maker/bucket.rb', line 5

def name
  @name
end

#summaryObject

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

Parameters:

  • group_name (String)

    Name of the group

Returns:

  • (Boolean)

    does the group exist?



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

Parameters:

  • bucketable (Object)

    an object which responds to :created_at

Returns:

  • (Boolean)

    is it bucketable?



81
82
83
# File 'lib/bucket_maker/bucket.rb', line 81

def is_bucketable?(bucketable)
  bucketable.created_at >= @created_after rescue false
end

#random_groupString

Randomize and get the group for this bucket

Returns:

  • (String)

    Group Name after randomization



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