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
40
41
42
43
44
45
46
47
48
49
# File 'lib/vignette/object_extensions.rb', line 11

def vignette(name=nil)
  key = "vignette_#{name || crc.abs.to_s(16)}"
  test_name = nil

  if name.blank?
    if caller[0].include?('filter.rb')
      # E.g /Users/hayesgm/animals/shadow/app/views/landing/family.html.haml:11:in `_app_views_landing_family_html_haml__3008026497873467685_70232967999960'
      # -> app/views/landing/family.html.haml:313c7f3a472883ba
      filename = caller[1].split(':')[0].gsub(Rails.root.to_s+'/','') # Take the view name
      test_name = "#{filename}:#{crc.abs.to_s(16)}"
    else
      # E.g /Users/hayesgm/animals/shadow/app/controllers/home_controller.rb:27:in `home'
      # -> app/controllers/home_controller:home:313c7f3a472883ba
      line = caller[0].gsub(Rails.root.to_s+'/','') # Take everything but the Rails root portion
      
      m = /(?<filename>[\w.\/]+):(?<line>\d+):in `(?<function>\w+)'/.match(line)
      
      if m && !m[:filename].blank? && !m[:function].blank?
        test_name = "#{m[:filename]}:#{m[:function]}:#{crc.abs.to_s(16)}"
      else # Fallback
        test_name = key
      end
    end
  else
    name # TODO: This should be test_name = name
  end

  # p ['Test name',test_name]

  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
  store[:v] = ( store[:v].present? ? JSON(store[:v]) : {} ).merge(test_name => result).to_json
  
  return result
end