Class: BucketMaker::Bucket

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

Instance Method Summary collapse

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, 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.



3
4
5
# File 'lib/bucket_maker/bucket.rb', line 3

def created_after
  @created_after
end

#distributionsObject

Returns the value of attribute distributions.



3
4
5
# File 'lib/bucket_maker/bucket.rb', line 3

def distributions
  @distributions
end

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

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/bucket_maker/bucket.rb', line 3

def name
  @name
end

#summaryObject

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

Returns:

  • (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

Returns:

  • (Boolean)


57
58
59
# File 'lib/bucket_maker/bucket.rb', line 57

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

#random_groupObject



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