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, expect_consistent_name: true) ⇒ 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
40
41
# File 'lib/vignette/object_extensions.rb', line 11

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

  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 Vignette.force_choice && self.include?(Vignette.force_choice)
    Vignette.force_choice
  elsif vig.has_key?(vignette_crc)
    vig[vignette_crc]['v']
  else
    # Store key into storage if not available
    new_value = self[Vignette::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