Class: HiddenHippo::Possibilities

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hidden_hippo/possibilities.rb

Overview

Represents multiple possible values

Each possible value has a support count. Adding possible values is done with #<<. Retrieving the support count for a possible value is done with #[].

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(counts = {}) ⇒ Possibilities

Returns a new instance of Possibilities.



33
34
35
36
37
# File 'lib/hidden_hippo/possibilities.rb', line 33

def initialize(counts={})
  @counts = counts
  @counts.keep_if {|_, c| c.kind_of?(Integer)}
  @counts.default_proc = proc {0}
end

Class Method Details

.demongoize(doc) ⇒ Object



10
11
12
# File 'lib/hidden_hippo/possibilities.rb', line 10

def demongoize(doc)
  HiddenHippo::Possibilities.new doc
end

.evolve(value) ⇒ Object



21
22
23
24
25
26
# File 'lib/hidden_hippo/possibilities.rb', line 21

def evolve(value)
  case value
    when Possibilities then value.mongoize
    else value
  end
end

.mongoize(value) ⇒ Object



14
15
16
17
18
19
# File 'lib/hidden_hippo/possibilities.rb', line 14

def mongoize(value)
  case value
    when Possibilities then value.mongoize
    else value
  end
end

.resizable?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/hidden_hippo/possibilities.rb', line 28

def resizable?
  true
end

Instance Method Details

#<<(value) ⇒ Object



43
44
45
# File 'lib/hidden_hippo/possibilities.rb', line 43

def <<(value)
  @counts[value.to_s.gsub(/[.$]/, '_')] += 1
end

#[](value) ⇒ Object



39
40
41
# File 'lib/hidden_hippo/possibilities.rb', line 39

def [](value)
  @counts[value]
end

#each(&block) ⇒ Object



47
48
49
# File 'lib/hidden_hippo/possibilities.rb', line 47

def each(&block)
  @counts.sort_by{|c| - c.last}.map(&:first).each &block
end

#each_with_support(&block) ⇒ Object



51
52
53
# File 'lib/hidden_hippo/possibilities.rb', line 51

def each_with_support(&block)
  @counts.sort_by{|c| - c.last}.each &block
end

#mongoizeObject



55
56
57
# File 'lib/hidden_hippo/possibilities.rb', line 55

def mongoize
  @counts
end

#resizable?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/hidden_hippo/possibilities.rb', line 59

def resizable?
  Possibilities.resizable?
end