Module: BipolarCache::Sequel::PluginAlpha

Defined in:
lib/bipolar_cache/sequel/plugin_alpha.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



36
37
38
# File 'lib/bipolar_cache/sequel/plugin_alpha.rb', line 36

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#bcsp_cache(**procs) ⇒ Object



40
41
42
# File 'lib/bipolar_cache/sequel/plugin_alpha.rb', line 40

def bcsp_cache(**procs)
  BipolarCache.read!(**procs)
end

#bcsp_proc_actual_from(**opts) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/bipolar_cache/sequel/plugin_alpha.rb', line 64

def bcsp_proc_actual_from(**opts)
  case opts[:actual]
  when Proc
    opts[:actual]
  when String, Symbol
    -> { send(opts[:actual]) }
  else
    -> { send("#{opts[:name]}_dataset").count }
  end
end

#bcsp_proc_cached_from(**opts) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/bipolar_cache/sequel/plugin_alpha.rb', line 75

def bcsp_proc_cached_from(**opts)
  case opts[:cached]
  when Proc
    opts[:cached]
  when String, Symbol
    -> { send(opts[:cached]) }
  else
    -> { send("#{opts[:name]}_count_cache") }
  end
end

#bcsp_proc_chance_from(**opts) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/bipolar_cache/sequel/plugin_alpha.rb', line 103

def bcsp_proc_chance_from(**opts)
  case opts[:chance]
  when Proc
    opts[:chance]
  when Integer, Float
    normalised_chance = if opts[:chance] > 1
                          opts[:chance] / 100
                        else
                          opts[:chance]
                        end
    ->(_value) { normalised_chance }
  else # default
    ->(value) { value < 10 ? 0.1 : 0.9 }
  end
end

#bcsp_proc_if_from(**opts) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/bipolar_cache/sequel/plugin_alpha.rb', line 130

def bcsp_proc_if_from(**opts)
  case opts[:if]
  when Proc
    opts[:if]
  when TrueClass, FalseClass
    value = opts[:if]
    -> { value }
  else # default
    -> { true }
  end
end

#bcsp_proc_rescue_from(**opts) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/bipolar_cache/sequel/plugin_alpha.rb', line 119

def bcsp_proc_rescue_from(**opts)
  if opts[:rescue].is_a? Proc
    opts[:rescue]
  else # default
    lambda { |e|
      e.inspect
      0
    }
  end
end

#bcsp_proc_update_from(**opts) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bipolar_cache/sequel/plugin_alpha.rb', line 86

def bcsp_proc_update_from(**opts)
  cache_name = if opts[:cached].is_a?(String) || opts[:cached].is_a?(Symbol)
                 opts[:cached]
               else
                 "#{opts[:name]}_count_cache"
               end

  case opts[:update]
  when Proc
    opts[:update]
  when String, Symbol
    -> { send(opts[:update]) }
  else
    ->(value) { update({ cache_name => value }) }
  end
end

#fetch_or_build_procs(**opts) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bipolar_cache/sequel/plugin_alpha.rb', line 44

def fetch_or_build_procs(**opts)
  # create cached store for instance procs, if none
  instance_variable_set(:@bcsp_proc_store, {}) unless instance_variable_defined?(:@bcsp_proc_store)

  if instance_variable_get(:@bcsp_proc_store)[opts[:name]].nil?
    instance_variable_get(:@bcsp_proc_store)[opts[:name]] =
      {
        actual: bcsp_proc_actual_from(**opts),
        cached: bcsp_proc_cached_from(**opts),
        update: bcsp_proc_update_from(**opts),
        chance: bcsp_proc_chance_from(**opts),
        rescue: bcsp_proc_rescue_from(**opts),
        if: bcsp_proc_if_from(**opts)
      }
  end

  # stored procs
  instance_variable_get(:@bcsp_proc_store)[opts[:name]]
end