Class: Spinach::Orderers::Random

Inherits:
Object
  • Object
show all
Defined in:
lib/spinach/orderers/random.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seed:) ⇒ Random

Returns a new instance of Random.



8
9
10
# File 'lib/spinach/orderers/random.rb', line 8

def initialize(seed:)
  @seed = seed.to_s
end

Instance Attribute Details

#seedObject (readonly)

Returns the value of attribute seed.



6
7
8
# File 'lib/spinach/orderers/random.rb', line 6

def seed
  @seed
end

Instance Method Details

#attach_summary(io) ⇒ Object

Output the randomization seed in the report summary.

Parameters:

  • io (IO)

    Output buffer for report.



18
19
20
# File 'lib/spinach/orderers/random.rb', line 18

def attach_summary(io)
  io.puts("Randomized with seed #{seed}\n\n")
end

#order(items) ⇒ Object

Returns a reordered version of the provided array

Parameters:

  • items (Array)

    Items to order



28
29
30
31
32
# File 'lib/spinach/orderers/random.rb', line 28

def order(items)
  items.sort_by do |item|
    Digest::MD5.hexdigest(seed + item.ordering_id)
  end
end