Class: BucketMaker::SeriesMaker

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/bucket_maker/series_maker.rb

Overview

Singleton which holds all information regarding the series, buckets and groups

Constant Summary collapse

BUCKET_ROOT =
'series'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



14
15
16
# File 'lib/bucket_maker/series_maker.rb', line 14

def configuration
  @configuration
end

#seriesObject

Returns the value of attribute series.



12
13
14
# File 'lib/bucket_maker/series_maker.rb', line 12

def series
  @series
end

Instance Method Details

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

Check if a Bucketable Object conform to the pre-conditions

Parameters:

  • bucketable (Object)

    any object which has methods :has_attribute?, :created_at and :id

  • series_name (String)

    Series Name

  • bucket_name (String)

    Bucket Name

  • group_name (String)

    Group Name

Returns:

  • (Boolean)

    is it bucketable?



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/bucket_maker/series_maker.rb', line 125

def bucketable?(bucketable, series_name, bucket_name, group_name)
  if  bucketable.has_attribute?(:created_at) &&
      has_group_in_bucket_in_series?(series_name, bucket_name, group_name)

    series_with_name(series_name).bucket_with_name(bucket_name).is_bucketable?(bucketable) ||
    series_with_name(series_name).is_bucketable?(bucketable) rescue false

  else
    false
  end
end

#bucketize(series_name, bucket_name) ⇒ String

Get a random group for the bucket in a series

Parameters:

  • series_name (String)

    Series Name

  • bucket_name (String)

    Bucket Name

Returns:

  • (String)

    Random Group Name according to the distribution



113
114
115
# File 'lib/bucket_maker/series_maker.rb', line 113

def bucketize(series_name, bucket_name)
  series_with_name(series_name).bucket_with_name(bucket_name).random_group rescue nil
end

#configured?Boolean

Check if the SeriesMaker is configured

Returns:

  • (Boolean)

    Signifies if the Object is configured



41
42
43
# File 'lib/bucket_maker/series_maker.rb', line 41

def configured?
  @configuration != nil
end

#for_each_series_with_bucketableObject

Iterator for each series to be run on a Bucketable instance Expects a block parameter to be passed



48
49
50
51
52
53
54
# File 'lib/bucket_maker/series_maker.rb', line 48

def for_each_series_with_bucketable
  @series.map do |series_name, series|
    series.each_bucket do |bucket_name, bucket|
      yield self, series_name, bucket_name
    end
  end.inject(true) {|result, value| result && value } if block_given?
end

#has_bucket_in_series?(series_name, bucket_name) ⇒ Boolean

Check if the bucket exist in the series

Parameters:

  • series_name (String)

    Series Name to search

  • bucket_name (String)

    Bucket Name to search

Returns:

  • (Boolean)

    is it there?



80
81
82
# File 'lib/bucket_maker/series_maker.rb', line 80

def has_bucket_in_series?(series_name, bucket_name)
  has_series?(series_name) && series_with_name(series_name).has_bucket?(bucket_name) rescue false
end

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

Check if the group exists in the bucket in the series

Parameters:

  • series_name (String)

    Series Name to search

  • bucket_name (String)

    Bucket Name to search

  • group_name (String)

    Group Name to search

Returns:

  • (Boolean)

    is it there?



91
92
93
94
# File 'lib/bucket_maker/series_maker.rb', line 91

def has_group_in_bucket_in_series?(series_name, bucket_name, group_name)
  has_bucket_in_series?(series_name, bucket_name) &&
  series_with_name(series_name).bucket_with_name(bucket_name).has_group?(group_name) rescue false
end

#has_series?(series_name) ⇒ Boolean

Check if the series exist

Parameters:

  • series_name (String)

    Series Name to search

Returns:

  • (Boolean)

    is it there?



70
71
72
# File 'lib/bucket_maker/series_maker.rb', line 70

def has_series?(series_name)
  @series[series_name.to_sym] != nil rescue false
end

#key_for_series(bucketable, series_name, bucket_name) ⇒ String

Get the key for the combination

Parameters:

  • bucketable (Object)

    any object which responds to :id

  • series_name (String)

    Series Name

  • bucket_name (String)

    Bucket Name

Returns:

  • (String)

    The key for future use



103
104
105
# File 'lib/bucket_maker/series_maker.rb', line 103

def key_for_series(bucketable, series_name, bucket_name)
  "bucket_maker:#{bucketable.class.to_s.underscore}_#{bucketable.id}:#{series_name.to_s}:#{bucket_name.to_s}" if bucketable && bucketable.id rescue nil
end

#make!(config) ⇒ Object

Set the class variables with configuration details

Parameters:

  • config (String)

    Path of the config file WRT the rails app



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bucket_maker/series_maker.rb', line 22

def make!(config)
  @series = []

  absolute_config_path = Rails.root + config

  if File.exists?(absolute_config_path)
    @configuration = YAML.load_file(absolute_config_path)

    @series = @configuration[BUCKET_ROOT].inject({}) do |result, (series_name, series_options)|
                result[series_name.to_sym] = BucketMaker::Series.new(series_name, series_options)
                result
              end if @configuration && @configuration[BUCKET_ROOT]
  end
end

#series_with_name(series_name) ⇒ BucketMaker::Series

Get the Series object when given the series_name

Parameters:

  • series_name (String)

    Series Name to search

Returns:



61
62
63
# File 'lib/bucket_maker/series_maker.rb', line 61

def series_with_name(series_name)
  @series[series_name.to_sym] if series_name
end