Class: Wukong::Datatypes::Binned

Inherits:
Enum show all
Defined in:
lib/wukong/datatypes/enum.rb

Overview

Note that bin 0 is

Constant Summary collapse

@@empty_bin_name =
'(none)'

Instance Attribute Summary

Attributes inherited from Enum

#val

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Enum

[], #index, #inspect, #to_flat, #to_i, to_pig, #to_s, to_sql_str

Constructor Details

#initialize(val) ⇒ Binned

FIXME – doesn’t respect a lower bound.



87
88
89
90
91
92
93
94
# File 'lib/wukong/datatypes/enum.rb', line 87

def initialize val
  return super(val) if val.nil?
  last_top = bins.first
  bins.each_with_index do |bin_top, idx|
    return super(idx) if val <= bin_top
  end
  return super(bins.length)
end

Class Method Details

.bin_name(lo_val, hi_val, options = { }) ⇒ Object

Bins



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/wukong/datatypes/enum.rb', line 112

def self.bin_name lo_val, hi_val, options = { }
  # case lo_val
  # when Integer then lo_val = [lo_val+1, hi_val].compact.min
  # end
  case
  when lo_val == -Infinity then "< #{hi_val}"
  when hi_val ==  Infinity then "#{lo_val}+"
  when (lo_val == hi_val)  then    lo_val
  else                         "#{lo_val} - #{hi_val}"
  end
end

.enumerates(*bins) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/wukong/datatypes/enum.rb', line 96

def self.enumerates *bins
  options = bins.extract_options!
  self.bins = bins
  last_top = bins.shift
  # bins.unshift bins.first if last_top == -Infinity
  names = bins.map do |bin_top|
    name = bin_name last_top, bin_top, options
    last_top = (last_top.is_a?(Integer) ? bin_top + 1 : bin_top)
    name
  end
  super(*names)
end

Instance Method Details

#binsObject



82
83
84
# File 'lib/wukong/datatypes/enum.rb', line 82

def bins
  self.class.bins
end