temp 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ require 'temp/options'
1
2
  require 'temp/config'
2
3
  require 'temp/tempfile'
3
4
  require 'temp/template'
@@ -12,9 +12,9 @@ module Temp
12
12
  attr_reader :file, :vals, :template_dir
13
13
  attr_accessor :template_options
14
14
 
15
- # Load a configuration file and set the configuration variables to the
16
- # values in the file. If not given a filename, load the default
17
- # configuration file. If the configuration file doesn't exist, use default
15
+ # Loads a configuration file and sets the configuration variables to the
16
+ # values in the file. If not given a filename, loads the default
17
+ # configuration file. If the configuration file doesn't exist, uses default
18
18
  # configuration values. Also accepts template options to make it easier to
19
19
  # pass to tempfiles.
20
20
  def initialize(file = DEFAULT_CONF_FILE, template_options = {})
@@ -0,0 +1,44 @@
1
+ module Temp
2
+
3
+ # The Options module contains functions for parsing command line options.
4
+ module Options
5
+
6
+ # Parses arguments and returns a hash of options.
7
+ def self.parse(args)
8
+ options = { :args => [] }
9
+
10
+ opt_stop = false
11
+ args.each do |arg|
12
+ if arg == '--'
13
+ opt_stop = true
14
+ next
15
+ end
16
+
17
+ if opt_stop
18
+ options[:args] << arg
19
+ else
20
+ match = /^(-)?(-no)?((-\w+)+)(=(.+))?$/.match(arg)
21
+ if match
22
+ key = match[3].sub('-', '').gsub('-', '_').to_sym
23
+
24
+ if match[5]
25
+ options[key] = match[6]
26
+ elsif match[2]
27
+ options[key] = false
28
+ else
29
+ options[key] = true
30
+ end
31
+
32
+ opt_stop = true if match[1]
33
+ else
34
+ options[:args] << arg
35
+ end
36
+ end
37
+ end
38
+
39
+ options
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -5,41 +5,27 @@ module Temp
5
5
  # The Runner module contains methods for Temp's command line interface.
6
6
  module Runner
7
7
 
8
- # Run Temp as a command line program
8
+ def self.help
9
+ puts 'Usage: temp [options] [project] [template]'
10
+ end
11
+
12
+ # Runs Temp as a command line program
9
13
  def self.start!(args)
10
14
  conf = Temp::Config.new(Temp::Config::DEFAULT_CONF_FILE)
11
- options = {}
12
-
13
- OptionParser.new do |o|
14
- o.banner = 'Usage: temp [options] [project] [template]'
15
-
16
- o.on('-v', '--version', 'Show version information') do
17
- puts "Temp #{Temp::VERSION}"
18
- exit
19
- end
20
-
21
- o.on('-l', '--list [name]', 'List all templates') do |name|
22
- if name
23
- t = Temp::Template.new(name, conf)
24
- puts "#{t.name} - #{t.desc}"
25
- else
26
- Temp::Template.all(conf).each do |t|
27
- puts "#{t.name} - #{t.desc}"
28
- end
29
- end
30
- exit
31
- end
32
-
33
- o.on('-t', '--template-options [opts]',
34
- 'Pass comma-separated option flags to the template') do |opts|
35
- opts.split(',').each { |o| options[o.to_sym] = true }
36
- end
37
- end.parse!(args)
15
+ options = Temp::Options.parse(args)
38
16
  conf.template_options = options
39
17
 
40
- if ARGV.size == 0
18
+ if options[:h] || options[:help]
19
+ help
20
+ exit
21
+ elsif options[:v] || options[:version]
22
+ puts "Temp #{Temp::VERSION}"
23
+ exit
24
+ end
25
+
26
+ if options[:args].size == 0
41
27
  puts 'No template specified.'
42
- elsif ARGV.size == 1
28
+ elsif options[:args].size == 1
43
29
  template = Temp::Template.new(ARGV[0], conf)
44
30
  Temp::Project.new(Dir.pwd, template).create
45
31
  else
@@ -52,8 +38,6 @@ module Temp
52
38
  puts 'Cannot create project because file exists there.'
53
39
  when Temp::Exceptions::TemplateNonExistentError
54
40
  puts 'The specified template does not exist.'
55
- when OptionParser::InvalidOption
56
- puts e.message
57
41
  else
58
42
  raise e
59
43
  end
@@ -5,7 +5,7 @@ module Temp
5
5
 
6
6
  attr_reader :options, :info, :ignore_files, :erb_files
7
7
 
8
- # Load a Tempfile in the specified directory. Options can be passed so that
8
+ # Loads a Tempfile in the specified directory. Options can be passed so that
9
9
  # they can be accessed from the Tempfile.
10
10
  def initialize(dir, options = {})
11
11
  @dir = File.expand_path(dir)
@@ -17,29 +17,29 @@ module Temp
17
17
  instance_eval File.read(@file) if File.file? @file
18
18
  end
19
19
 
20
- # Public method to get the object's binding
20
+ # Gets the object's binding
21
21
  def get_binding
22
22
  binding
23
23
  end
24
24
 
25
- # Set the template name
25
+ # Sets the template name
26
26
  def name(str)
27
27
  info[:name] = str
28
28
  end
29
29
 
30
- # Set the template description
30
+ # Sets the template description
31
31
  def desc(str)
32
32
  info[:desc] = str
33
33
  end
34
34
 
35
- # Add files to the ignore list by expanding the given glob(s)
35
+ # Adds files to the ignore list by expanding the given glob(s)
36
36
  def ignore(*files)
37
37
  @ignore_files |= files.map do |file|
38
38
  Dir.glob(File.join(@dir, file)).map { |f| f.sub(@dir + '/', '') }
39
39
  end.flatten
40
40
  end
41
41
 
42
- # Add files to the ERB list by expanding the given glob(s)
42
+ # Adds files to the ERB list by expanding the given glob(s)
43
43
  def use_erb(*files)
44
44
  @erb_files |= files.map do |file|
45
45
  Dir.glob(File.join(@dir, file)).map { |f| f.sub(@dir + '/', '') }
@@ -7,7 +7,7 @@ module Temp
7
7
 
8
8
  attr_reader :conf, :tempfile, :path, :filename, :name, :desc, :files
9
9
 
10
- # Return an array of all templates in the template directory
10
+ # Returns an array of all templates in the template directory
11
11
  def self.all(conf)
12
12
  Dir.entries(conf.template_dir).map do |t|
13
13
  unless t == '.' || t == '..'
@@ -16,7 +16,7 @@ module Temp
16
16
  end.compact
17
17
  end
18
18
 
19
- # Load a template
19
+ # Loads a template
20
20
  def initialize(name, conf)
21
21
  @conf = conf
22
22
  @path = File.join(@conf.template_dir, name)
@@ -29,7 +29,7 @@ module Temp
29
29
  @files = Template.find_files(@path) - @tempfile.ignore_files
30
30
  end
31
31
 
32
- # Find all of the files in a path
32
+ # Finds all of the files in a path
33
33
  def self.find_files(path)
34
34
  path = File.expand_path(path)
35
35
  Dir.glob(File.join(path, '**/*')).map { |f| f.sub(path + '/', '') }
@@ -1,3 +1,3 @@
1
1
  module Temp
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -39,6 +39,40 @@ describe Temp do
39
39
  FileUtils.rm_r(@test_dir)
40
40
  end
41
41
 
42
+ describe Temp::Options do
43
+
44
+ it 'should have the correct arguments' do
45
+ Temp::Options.parse(['foo', 'bar'])[:args].should == ['foo', 'bar']
46
+ end
47
+
48
+ it 'should have the correct options' do
49
+ options = Temp::Options.parse(['-f', '-bar-baz'])
50
+ options[:f].should be_true
51
+ options[:bar_baz].should be_true
52
+ end
53
+
54
+ it 'should set "no" options to false' do
55
+ Temp::Options.parse(['-no-baz'])[:baz].should be_false
56
+ end
57
+
58
+ it 'should stop parsing options after a --' do
59
+ options = Temp::Options.parse(['-foo', '--', '-bar'])
60
+ options[:foo].should be_true
61
+ options[:args].should == ['-bar']
62
+ end
63
+
64
+ it 'should stop parsing options after an option with --' do
65
+ options = Temp::Options.parse(['--stop-hammertime', '-foo'])
66
+ options[:args].should == ['-foo']
67
+ options[:stop_hammertime].should be_true
68
+ end
69
+
70
+ it 'should set an option to a given value' do
71
+ Temp::Options.parse(['-foo=bar'])[:foo].should == 'bar'
72
+ end
73
+
74
+ end
75
+
42
76
  describe Temp::Config do
43
77
 
44
78
  it 'should load the config file' do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Austin Gatchell
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-10-09 00:00:00 -05:00
17
+ date: 2011-10-16 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -50,6 +50,7 @@ files:
50
50
  - lib/temp.rb
51
51
  - lib/temp/config.rb
52
52
  - lib/temp/exceptions.rb
53
+ - lib/temp/options.rb
53
54
  - lib/temp/project.rb
54
55
  - lib/temp/runner.rb
55
56
  - lib/temp/tempfile.rb