Class: Pickup

Inherits:
Object
  • Object
show all
Defined in:
lib/pickup.rb,
lib/pickup/version.rb

Defined Under Namespace

Classes: CircleIterator, MappedList

Constant Summary collapse

VERSION =
"0.0.11"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list, opts = {}, &block) ⇒ Pickup

Returns a new instance of Pickup.



7
8
9
10
11
12
13
# File 'lib/pickup.rb', line 7

def initialize(list, opts={}, &block)
  @list = list
  @uniq = opts[:uniq] || false
  @pick_func = block if block_given?
  @key_func = opts[:key_func]
  @weight_func = opts[:weight_func]
end

Instance Attribute Details

#key_func=(value) ⇒ Object (writeonly)

Sets the attribute key_func

Parameters:

  • value

    the value to set the attribute key_func to.



5
6
7
# File 'lib/pickup.rb', line 5

def key_func=(value)
  @key_func = value
end

#listObject (readonly)

Returns the value of attribute list.



4
5
6
# File 'lib/pickup.rb', line 4

def list
  @list
end

#pick_funcObject



24
25
26
27
28
29
30
# File 'lib/pickup.rb', line 24

def pick_func
  @pick_func ||= begin
    Proc.new do |val|
      val
    end
  end
end

#uniqObject (readonly)

Returns the value of attribute uniq.



4
5
6
# File 'lib/pickup.rb', line 4

def uniq
  @uniq
end

#weight_func=(value) ⇒ Object (writeonly)

Sets the attribute weight_func

Parameters:

  • value

    the value to set the attribute weight_func to.



5
6
7
# File 'lib/pickup.rb', line 5

def weight_func=(value)
  @weight_func = value
end

Instance Method Details

#pick(count = 1, opts = {}, &block) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/pickup.rb', line 15

def pick(count=1, opts={}, &block)
  func = block || pick_func
  key_func = opts[:key_func] || @key_func
  weight_func = opts[:weight_func] || @weight_func
  mlist = MappedList.new(list, func, uniq: uniq, key_func: key_func, weight_func: weight_func)
  result = mlist.random(count)
  count == 1 ? result.first : result
end