Class: Almicube::Ranking::DataRanking

Inherits:
Base
  • Object
show all
Defined in:
lib/almicube/ranking/data_ranking.rb

Constant Summary collapse

KEY_PATTERN =

Example) %sample_key

/%{([a-z_]+)}/

Instance Attribute Summary

Attributes inherited from Base

#aggregator, #bundler, #class_name, #per_page, #selector

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#aggregate, #aggregate!, #bundled_keys, #connection, #page, #records

Constructor Details

#initialize(options = {}) ⇒ DataRanking

Returns a new instance of DataRanking.

Raises:

  • (TypeError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/almicube/ranking/data_ranking.rb', line 26

def initialize(options={})
  @options = self.class.default_options.merge options.symbolize_keys

  raise TypeError, ":as option is only allowed Integer or Float" unless [Integer, Float].include? @options[:as]

  super(@options)

  @selector ||= Selector::AllSelector.new self.options
  @options.delete(:selector)
  @selector.ranking = self

  @options[:aggregator] ||= Aggregator::SumAggregator.new
  @options[:aggregator].ranking = self
end

Class Method Details

.build(options = {}) ⇒ Object



21
22
23
# File 'lib/almicube/ranking/data_ranking.rb', line 21

def build(options={})
  self.new options
end

.default_optionsObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/almicube/ranking/data_ranking.rb', line 8

def default_options
  { attribute_name: :score,
    prefix: 'ranking',
    suffix: '',
    distinction: '%{date}',
    date_format: '%Y%m%d',
    date: Date.today,
    per_page: 10,
    as: Float,
    default_score: 0,
    class_name: nil }
end

Instance Method Details

#attribute_nameObject



51
52
53
# File 'lib/almicube/ranking/data_ranking.rb', line 51

def attribute_name
  @options.fetch(:attribute_name, :score).to_s.to_sym
end

#data?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/almicube/ranking/data_ranking.rb', line 83

def data?
  selector.data_provider?
end

#data_keyObject



47
48
49
# File 'lib/almicube/ranking/data_ranking.rb', line 47

def data_key
  key_format(@options[:data_key], @options.merge({ type: :data }))
end

#dateObject



55
56
57
# File 'lib/almicube/ranking/data_ranking.rb', line 55

def date
  @options.fetch(:date, Date.today)
end

#default_scoreObject



91
92
93
# File 'lib/almicube/ranking/data_ranking.rb', line 91

def default_score
  @options.fetch(:default_score, 0)
end

#exists?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/almicube/ranking/data_ranking.rb', line 87

def exists?
  connection.exists key
end

#in(target) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/almicube/ranking/data_ranking.rb', line 59

def in(target)
  association_name = class_name.to_s.underscore.pluralize

  if ( target.class != class_name ) && target.respond_to?(association_name)
    _selector = Selector::HasManySelector.new target: target, association_name: association_name
    _selector.ranking = self
    return SubRanking.new self, selector: _selector
  end

  self
end

#incr(item, score = 1) ⇒ Object



111
112
113
# File 'lib/almicube/ranking/data_ranking.rb', line 111

def incr(item, score = 1)
  connection.zincrby key, score, item.to_param
end

#key(options = {}) ⇒ Object



43
44
45
# File 'lib/almicube/ranking/data_ranking.rb', line 43

def key(options={})
  Almicube::Key.new self, options.merge( type: :data )
end

#optionsObject



79
80
81
# File 'lib/almicube/ranking/data_ranking.rb', line 79

def options
  @options.select{ |k| k != :type }.merge({ selector: selector, aggregator: aggregator })
end

#per_page=(value) ⇒ Object



75
76
77
# File 'lib/almicube/ranking/data_ranking.rb', line 75

def per_page=(value)
  @options[:per_page] = value
end

#ranged(name) ⇒ Object



71
72
73
# File 'lib/almicube/ranking/data_ranking.rb', line 71

def ranged(name)
  SubRanking.new(self).ranged(name)
end

#rank(item) ⇒ Object



107
108
109
# File 'lib/almicube/ranking/data_ranking.rb', line 107

def rank(item)
  connection.zrevrank(key, item.to_param) + 1
end

#score(item) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/almicube/ranking/data_ranking.rb', line 95

def score(item)
  actual_score = ( connection.zscore key, item.to_param ) || @options.fetch(:default_score, 0)
  case @options[:as].to_s
  when "Integer"
    actual_score.to_i
  when "Float"
    actual_score.to_f
  else
    actual_score
  end
end