Module: BucketMaker::Models::Bucketable

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

Overview

Module which holds the base methods for Bucketable objects

Instance Method Summary collapse

Instance Method Details

#bucketize!Boolean

Iteratively group object into buckets

Returns:

  • (Boolean)

    true if all the operations are successfull



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bucket_maker/models/bucketable.rb', line 79

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) ⇒ Boolean

Group object for series_name and bucket_name

Parameters:

  • series_name (String)

    Name of the Series

  • bucket_name (String)

    Name of the Bucket

Returns:

  • (Boolean)

    true if the operation is successfull



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/bucket_maker/models/bucketable.rb', line 97

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) ⇒ Boolean

Force object into the group_name under bucket under series

Parameters:

  • series_name (String)

    Name of the Series

  • bucket_name (String)

    Name of the Bucket

  • group_name (String)

    Name of the Group

Returns:

  • (Boolean)

    Always true because forcing is ok by us



71
72
73
74
# File 'lib/bucket_maker/models/bucketable.rb', line 71

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

Used to test if this object has been grouped under a certain group

Parameters:

  • series_name (String)

    Name of the Series

  • bucket_name (String)

    Name of the Bucket

  • group_name (String)

    Name of the Group

  • force (Boolean) (defaults to: false)

    To force the object into the group_name

Returns:

  • (Boolean)

    if the given group_name is the same after randomization of groups



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bucket_maker/models/bucketable.rb', line 17

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

Used to test if this object has not been grouped under a certain group

Parameters:

  • series_name (String)

    Name of the Series

  • bucket_name (String)

    Name of the Bucket

  • group_name (String)

    Name of the Group

Returns:

  • (Boolean)

    if the given group_name is the not the same after randomization of groups



60
61
62
# File 'lib/bucket_maker/models/bucketable.rb', line 60

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