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
34
35
36
37
38
39
# 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

  store = Vignette.get_store

  test_name = if name.present?
    name
  elsif store[:vt] && original_name = JSON(store[:vt])[vignette_crc]
    original_name
  else
    loc = caller_locations(1,1).first
    new_name = "(#{Vignette::strip_path(loc.absolute_path)}:#{loc.lineno})"

    store[:vt] = ( store[:vt].present? ? JSON(store[:vt]) : {} ).merge(vignette_crc => new_name).to_json

    new_name
  end

  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