Module: ObjectExtensions::ArrayExtensions

Defined in:
lib/vignette/object_extensions.rb

Overview

Extensions to the Array object

Instance Method Summary collapse

Instance Method Details

#crcObject



6
7
8
# File 'lib/vignette/object_extensions.rb', line 6

def crc
  Zlib.crc32(self.join)
end

#vignette(name = nil) ⇒ Object

Test will select a random object from the Array



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vignette/object_extensions.rb', line 11

def vignette(name=nil)
  vignette_crc = self.crc().abs.to_s(16)

  key = "vignette_#{vignette_crc}"
  test_name = nil

  test_name = if name.blank?
    loc = caller_locations(1,1).first
    "(#{Vignette::strip_path(loc.absolute_path)}:#{loc.lineno})"
  else
    name
  end

  store = Vignette.get_store

  choice = store[key] ||= Kernel.rand(length) # Store key into storage if not available
  result = self[choice.to_i]

  # Let's store keys where they are (note, this truncates any other tests with the same name)
  store[:v] = ( store[:v].present? ? JSON(store[:v]) : {} ).merge(test_name => result).to_json

  return result
end