Module: BucketMaker::Models::Bucketable

Included in:
ActiveRecordable::InstanceMethods, Redisable::InstanceMethods
Defined in:
lib/bucket_maker/models/bucketable.rb

Instance Method Summary collapse

Instance Method Details

#bucketize!Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bucket_maker/models/bucketable.rb', line 51

def bucketize!
  # Take each series and bucket
  BucketMaker::SeriesMaker.instance.for_each_series_with_bucketable do |series_maker, series_name, bucket_name|
    # Randomize
    group_name = series_maker.bucketize(series_name, bucket_name)
    # Get the series key
    series_key = series_maker.key_for_series(self, series_name, bucket_name)
    # Set the key
    set_group_for_key(series_key, group_name)
    true
  end
end

#bucketize_for_series_and_bucket!(series_name, bucket_name) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bucket_maker/models/bucketable.rb', line 64

def bucketize_for_series_and_bucket!(series_name, bucket_name)
  # Get the singleton Series Maker
  series_maker = BucketMaker::SeriesMaker.instance
  if series_maker.has_bucket_in_series?(series_name, bucket_name)
    # Randomize
    group_name = series_maker.bucketize(series_name, bucket_name)
    # Get the series key
    series_key = series_maker.key_for_series(self, series_name, bucket_name)
    # Set the key
    set_group_for_key(series_key, group_name)
    true
  else
    false
  end
end

#force_to_bucket!(series_name, bucket_name, group_name) ⇒ Object



46
47
48
49
# File 'lib/bucket_maker/models/bucketable.rb', line 46

def force_to_bucket!(series_name, bucket_name, group_name)
  # Forcefully place inside the bucket
  in_bucket?(series_name, bucket_name, group_name, true)
end

#in_bucket?(series_name, bucket_name, group_name, force = false) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bucket_maker/models/bucketable.rb', line 6

def in_bucket?(series_name, bucket_name, group_name, force=false)
  # Get the singleton Series Maker
  series_maker = BucketMaker::SeriesMaker.instance

  # Check if the parameters are valid
  if series_maker.bucketable?(self, series_name, bucket_name, group_name)
    # The Key is generated by the Series Maker
    series_key = series_maker.key_for_series(self, series_name, bucket_name)

    # Get the Group if created before from Persisted Storage
    saved_group = group_for_key(series_key)

    # If its a forced operation then set the Group to the parameter given
    if force
      set_group_for_key(series_key, group_name) if saved_group != group_name.to_s
      # Always return true for set
      true
    else
      # Lazy Load ??
      # If there is no Group then its lazy load/create time !
      unless saved_group
        # Get the random group for this instance
        saved_group = series_maker.bucketize(series_name, bucket_name)
        # Set the Group in Persisted Storage
        set_group_for_key(series_key, saved_group)
      end

      # return if its the same bucket as requested
      saved_group == group_name.to_s
    end
  else
    # return false if invalid parameters
    false
  end
end

#not_in_bucket?(series_name, bucket_name, group_name) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/bucket_maker/models/bucketable.rb', line 42

def not_in_bucket?(series_name, bucket_name, group_name)
  !in_bucket?(series_name, bucket_name, group_name)
end