Class: Lemon::CLI::Scaffold

Inherits:
Generate show all
Defined in:
lib/lemon/cli/scaffold.rb

Overview

Scaffold Command

Instance Method Summary collapse

Methods inherited from Generate

#command_run, #option_caps, #option_group

Methods inherited from Base

#initialize, #option_coverage, #option_dryrun, #option_format, #option_loadpath, #option_namespaces, #option_output, #option_parser, #option_private, #option_requires, #option_verbose, #option_zealous, #options, run, #run

Constructor Details

This class inherits a constructor from Lemon::CLI::Base

Instance Method Details

#append_test(file, test) ⇒ Object

Append tests to file.



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/lemon/cli/scaffold.rb', line 100

def append_test(file, test)
  return if test.strip.empty?
  if dryrun?
    puts "[DRYRUN] append #{file}"
  else
    dir = File.dirname(file)
    FileUtils.mkdir_p(dir) unless File.directory?(dir)
    File.open(file, 'a'){ |f| f << "\n" + test.to_s }
    puts "append #{file}"
  end
end

#command_parse(argv) ⇒ Object



33
34
35
36
37
# File 'lib/lemon/cli/scaffold.rb', line 33

def command_parse(argv)
  option_parser.banner = "Usage: lemons scaffold [options] [files ...]"
  setup_options
  option_parser.parse!(argv)
end

#dryrun?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/lemon/cli/scaffold.rb', line 58

def dryrun?
  options[:dryrun]
end

#generate_output(render_map) ⇒ Object

Unlike the Generate command, the Scaffold commnad writes output to test files.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lemon/cli/scaffold.rb', line 18

def generate_output(render_map)
  render_map.each do |group, test|
    file = test_file(group)

    if File.exist?(file)
      append_test(file, test)
    else
      write_test(file, test)
    end
  end
end

#outputObject

Output directory, default is ‘test`.



51
52
53
# File 'lib/lemon/cli/scaffold.rb', line 51

def output
  options[:output] || 'test'
end

#setup_optionsObject



42
43
44
45
46
# File 'lib/lemon/cli/scaffold.rb', line 42

def setup_options
  option_output
  option_dryrun
  super
end

#test_file(group) ⇒ Object

Given the group name, convert it to a suitable test file name.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lemon/cli/scaffold.rb', line 65

def test_file(group)
  if options[:group] == :file
    file = group
  else
    file = group.gsub('::', '/').downcase
  end

  dirname, basename = File.split(file)

  if i = dirname.index('/')
    dirname = dirname[i+1..-1]
    file = File.join(dirname, output, "case_#{basename}")
  else
    file = File.join(output, "case_#{basename}")
  end
end

#write_test(file, test) ⇒ Object

Write test file.



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/lemon/cli/scaffold.rb', line 85

def write_test(file, test)
  return if test.strip.empty?
  if dryrun?
    puts "[DRYRUN] write #{file}"
  else
    dir = File.dirname(file)
    FileUtils.mkdir_p(dir) unless File.directory?(dir)
    File.open(file, 'w'){ |f| f << test.to_s }
    puts "write #{file}"
  end       
end