Module: Recorder

Defined in:
lib/recorder.rb,
lib/recorder/version.rb

Defined Under Namespace

Classes: Config

Constant Summary collapse

VERSION =
"0.0.2"
@@config =
Config.new

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.config(&block) ⇒ Object (readonly)

Configure Recorder

Examples

Recorder.config do |c|
  c.verbose = true
  c.stubb = true
  c.records_dir = 'records/'
end


21
22
23
# File 'lib/recorder.rb', line 21

def config
  @config
end

Class Method Details

.dump_to(file) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/recorder.rb', line 30

def dump_to file
  if @@config.records_dir
    Dir.mkdir @@config.records_dir unless File.directory? @@config.records_dir
    file = File.join(@@config.records_dir, file)
  end

  if !@@config.stubb
    result = yield
    yaml_result = result.to_yaml
    if File.exist? file
      old_result = YAML.load_file(file)
      if old_result != result
        outputs "Recorder: result is different from last run".red
        Differ.format = :color
        outputs Differ.(yaml_result.to_s, old_result.to_s)
      else
        outputs "Recorder: result is the same".green
      end
    else
      File.open(file, 'w' ) do |out|
        YAML.dump(result, out)
      end
      outputs "Recorder: recorded in #{file}".green
    end
    return result
  else
    if File.exist? file
      outputs "Recorder: loaded from #{file}".green
      return YAML.load_file(file)
    else
      result = yield
      yaml_result = result.to_yaml
      File.open(file, 'w' ) do |out|
        YAML.dump(result, out)
      end
      outputs "Recorder: recorded in #{file}".green
      return result
    end
  end
end