Module: Soaspec::ExeHelpers

Defined in:
lib/soaspec/exe_helpers.rb

Overview

Help with tasks common to soaspec executables

Instance Method Summary collapse

Instance Method Details

#create_file(filename: nil, content: nil, ignore_if_present: false) ⇒ Object

Parameters:

  • filename (String) (defaults to: nil)

    Name of the file to create

  • content (String) (defaults to: nil)

    Content to place inside file



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/soaspec/exe_helpers.rb', line 9

def create_file(filename: nil, content: nil, ignore_if_present: false)
  #filename = options[:filename]
  raise 'Need to pass filename' unless filename
  #content = options[:content]
  raise 'Need to pass contents to insert into file' unless content
  if File.exist? filename
    old_content = File.read(filename)
    if old_content != content && !ignore_if_present
      warn "!! #{filename} already exists and differs from template"
    end
  else
    File.open(filename, 'w') { |f| f.puts content }
    puts 'Created: ' + filename
  end
end

#create_folder(folder) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/soaspec/exe_helpers.rb', line 25

def create_folder(folder)
  if File.exist? folder
    unless File.directory? folder
      warn "!! #{folder} already exists and is not a directory"
    end
  else
    FileUtils.mkdir folder
    puts "Created folder: #{folder}/"
  end
end

#gem_contentObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/soaspec/exe_helpers.rb', line 53

def gem_content
  "source 'https://rubygems.org'\n\ngem 'data_magic'\ngem 'require_all'\ngem 'rspec_junit_formatter'\ngem 'rake'\ngem 'soaspec'\n\n"
end

#rake_contentObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/soaspec/exe_helpers.rb', line 36

def rake_content
  "# The list of task for a Rake file can be seen with `rake -T`\nrequire 'rspec/core/rake_task' # See See https://relishapp.com/rspec/rspec-core/docs/command-line/rake-task for details\n\n# This runs `rspec` command with the following options. Type `rake spec` to run this task\nRSpec::Core::RakeTask.new(:spec) do |t|\n  t.pattern = \"spec/*_spec.rb\" # Run all specs in 'spec' folder ending in '_spec'\n  # Next line shows output on the screen, Junit xml report and an HTML report\n  t.rspec_opts = \"--format documentation --format RspecJunitFormatter --out logs/spec.xml --format html --out logs/spec.html\"\n  t.fail_on_error = false\nend\n\ntask :default => :spec # This runs the 'spec' task by default when no task is mentioned. E.g., if only `rake` is typed\n"
end

#spec_helper_contentObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/soaspec/exe_helpers.rb', line 66

def spec_helper_content
"\nrequire 'soaspec'\nrequire 'require_all'\nrequire_all 'lib'\nrequire 'data_magic'\n\ninclude DataMagic # Used as example of loading data smartly. Use 'data_for' method to load yml data\n\nRSpec.configure do |config|\n  # This will make backtrace much shorter by removing many lines from rspec failure message\n  config.backtrace_exclusion_patterns = [\n/rspec/\n  ]\nend\n\n"
end

#underscore_key(key) ⇒ Object

Convert key in camelcase to underscore separated (snakecase)



87
88
89
90
91
92
93
94
# File 'lib/soaspec/exe_helpers.rb', line 87

def underscore_key(key)
  key.to_s.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
     .gsub(/([a-z\d])([A-Z])/,'\1_\2')
     .tr('-', '_')
     .gsub(/\s/, '_')
     .gsub(/__+/, '_')
     .downcase
end