Module: Ohm::ZScores

Defined in:
lib/ohm-zset.rb

Defined Under Namespace

Modules: ZScore

Class Method Summary collapse

Class Method Details

.boolean(val) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/ohm-zset.rb', line 84

def boolean(val)
  case val
  when "true", "1", true 
    1
  when "false", "0", false, nil 
    0
  else 
    1
  end
end

.datetime(val) ⇒ Object



95
96
97
# File 'lib/ohm-zset.rb', line 95

def datetime(val)
  ::DateTime.parse(val.to_s).to_time.to_i
end

.string(val) ⇒ Object

scoring accurate until 9th character



100
101
102
103
104
105
106
107
108
109
# File 'lib/ohm-zset.rb', line 100

def string(val)
  total_score = 0

  val.each_char.with_index do |c, i|
    total_score += (c.ord-31) * ((126-32) ** (10 - i))
    break if i == 9        
  end

  total_score.to_f
end

.string_insensitive(val) ⇒ Object

scoring accurate until 9th character



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ohm-zset.rb', line 112

def string_insensitive(val)
  total_score = 0

  val.each_char.with_index do |c, i|
    char_ord = c.ord-31
    if ('a'..'z').include? c
      char_ord -= 32
    end
    total_score += (char_ord) * ((126-32) ** (10 - i))
    break if i == 9        
  end

  total_score.to_f
end

.string_insensitive_high(val) ⇒ Object

scoring accurate until 9th character



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ohm-zset.rb', line 128

def string_insensitive_high(val)
  total_score = 0

  val.each_char.with_index do |c, i|
    char_ord = c.ord-31
    if ('a'..'z').include? c
      char_ord -= 31.5
    end
    total_score += (char_ord) * ((126-32) ** (10 - i))
    break if i == 9        
  end

  total_score.to_f
end