Module: ObjectExtensions::ArrayExtensions

Defined in:
lib/vignette/object_extensions.rb

Overview

Extensions to the Array object

Instance Method Summary collapse

Instance Method Details

#crcObject



12
13
14
# File 'lib/vignette/object_extensions.rb', line 12

def crc
  Zlib.crc32(self.join)
end

#vignette(name = nil, expect_consistent_name: true) ⇒ Object

Test will select a random object from the Array



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vignette/object_extensions.rb', line 17

def vignette(name=nil, expect_consistent_name: true)
  vignette_crc = self.crc().abs.to_s(16)

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

  vig = Vignette.vig

  test_name = if expect_consistent_name && name.present?
    name
  elsif vig[vignette_crc]
    vig[vignette_crc]['n']
  elsif name.present? # this logic looks weird, but this is if we don't expect consistent names *and* we don't have a name in v[]
    name
  else
    loc = caller_locations(1,1).first
    "(#{Vignette::strip_path(loc.absolute_path)}:#{loc.lineno})"
  end

  result = if vig.has_key?(vignette_crc)
    vig[vignette_crc]['v']
  else
    # Store key into storage if not available
    new_value = self[Kernel.rand(length)]

    Vignette.set_vig( vig.merge(vignette_crc => { n: test_name, v: new_value, t: Time.now.to_i }) )

    new_value
  end

  return result
end