Module: Helper::Stub

Defined in:
lib/cucumber_salad/helper/stub.rb

Class Method Summary collapse

Class Method Details

.clearObject



5
6
7
8
9
10
11
12
# File 'lib/cucumber_salad/helper/stub.rb', line 5

def self.clear
  Dir.foreach self.stub_file_path do |file|
    next if file == '.' or file == '..' or file == '.svn'
    if File.exists?(file)
      File.delete(File.join(self.stub_file_path, file))
    end
  end
end

.create_key(class_name, method, parameters = nil) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/cucumber_salad/helper/stub.rb', line 40

def self.create_key(class_name, method, parameters = nil)
  Digest::MD5.hexdigest([
    class_name,
    method,
    parameters
  ].delete_if{|value| value == nil}.to_json)
end

.init_stubsObject



14
15
16
# File 'lib/cucumber_salad/helper/stub.rb', line 14

def self.init_stubs
  @stubs ||= {}
end

.load_defaultsObject



30
31
32
33
34
35
36
37
38
# File 'lib/cucumber_salad/helper/stub.rb', line 30

def self.load_defaults
  Dir.glob(File.join(Helper::Environment.default_stub_path, '**/*')).each do |file|
    unless File.directory?(file)
      value = JSON.load(File.read(file))
      key = file.split('/').last
      self.register_default(key, value)
    end
  end
end

.register(key, value) ⇒ Object



18
19
20
21
# File 'lib/cucumber_salad/helper/stub.rb', line 18

def self.register(key, value)
  self.init_stubs
  @stubs[key] = value
end

.register_default(key, value) ⇒ Object



23
24
25
26
27
28
# File 'lib/cucumber_salad/helper/stub.rb', line 23

def self.register_default(key, value)
  self.init_stubs
  unless @stubs[key]
    self.register(key, value)
  end
end

.stub_file_pathObject



79
80
81
# File 'lib/cucumber_salad/helper/stub.rb', line 79

def self.stub_file_path
  Helper::Environment.stub_path
end

.writeObject



48
49
50
51
52
# File 'lib/cucumber_salad/helper/stub.rb', line 48

def self.write
  if @stubs and @stubs.size > 0
    self.write_to_store
  end
end

.write_to_fileObject



72
73
74
75
76
77
# File 'lib/cucumber_salad/helper/stub.rb', line 72

def self.write_to_file
  @stubs.each_pair do |key, value|
    path = self.stub_file_path + '/' + key
    File.open(path, 'w') {|f| f.write(value.to_json) }
  end
end

.write_to_redisObject



62
63
64
65
66
67
68
69
70
# File 'lib/cucumber_salad/helper/stub.rb', line 62

def self.write_to_redis
  @redis ||= Redis.new(:host => '176.34.255.217', :port => 6379)
  @redis.flushall
  @redis.pipelined do
    @stubs.each_pair do |key, value|
      @redis.set key, value.to_json
    end
  end
end

.write_to_storeObject



54
55
56
57
58
59
60
# File 'lib/cucumber_salad/helper/stub.rb', line 54

def self.write_to_store
  if Helper::Environment.remote?
    self.write_to_redis
  else
    self.write_to_file
  end
end