rprogram 0.1.8 → 0.2.0

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.
@@ -0,0 +1,39 @@
1
+ require 'rprogram/options/class_methods'
2
+ require 'rprogram/non_option'
3
+ require 'rprogram/option'
4
+
5
+ module RProgram
6
+ module Options
7
+ def self.included(base)
8
+ base.send :extend, ClassMethods
9
+ end
10
+
11
+ #
12
+ # @see self.has_non_option?
13
+ #
14
+ def has_non_option?(name)
15
+ self.class.has_non_option?(name)
16
+ end
17
+
18
+ #
19
+ # @see self.get_non_option
20
+ #
21
+ def get_non_option(name)
22
+ self.class.get_non_option(name)
23
+ end
24
+
25
+ #
26
+ # @see self.has_option?
27
+ #
28
+ def has_option?(name)
29
+ self.class.has_option?(name)
30
+ end
31
+
32
+ #
33
+ # @see self.get_option
34
+ #
35
+ def get_option(name)
36
+ self.class.get_option(name)
37
+ end
38
+ end
39
+ end
@@ -1,146 +1,2 @@
1
- require 'rprogram/extensions/meta'
2
- require 'rprogram/non_option'
3
- require 'rprogram/option'
4
-
5
- module RProgram
6
- module Options
7
- def self.included(base)
8
- base.metaclass_eval do
9
- #
10
- # @return [Hash]
11
- # All defined non-options of the class.
12
- #
13
- def non_options
14
- @non_options ||= {}
15
- end
16
-
17
- #
18
- # Searches for the non-option with the matching name in the class
19
- # and it's ancestors.
20
- #
21
- # @param [Symbol, String] name
22
- # The name to search for.
23
- #
24
- # @return [true, false]
25
- # Specifies whether the non-option with the matching name was
26
- # defined.
27
- #
28
- def has_non_option?(name)
29
- name = name.to_sym
30
-
31
- ancestors.each do |base|
32
- if base.include?(RProgram::Options)
33
- return true if base.non_options.include?(name)
34
- end
35
- end
36
-
37
- return false
38
- end
39
-
40
- #
41
- # Searches for the non-option with the matching name in the class
42
- # and it's ancestors.
43
- #
44
- # @param [Symbol, String] name
45
- # The name to search for.
46
- #
47
- # @return [NonOption]
48
- # The non-option with the matching name.
49
- #
50
- def get_non_option(name)
51
- name = name.to_sym
52
-
53
- ancestors.each do |base|
54
- if base.include?(RProgram::Options)
55
- if base.non_options.has_key?(name)
56
- return base.non_options[name]
57
- end
58
- end
59
- end
60
-
61
- return nil
62
- end
63
-
64
- #
65
- # @return [Hash]
66
- # All defined options for the class.
67
- #
68
- def options
69
- @options ||= {}
70
- end
71
-
72
- #
73
- # Searches for the option with the matching name in the class and
74
- # it's ancestors.
75
- #
76
- # @param [Symbol, String] name
77
- # The name to search for.
78
- #
79
- # @return [true, false]
80
- # Specifies whether the option with the matching name was defined.
81
- #
82
- def has_option?(name)
83
- name = name.to_sym
84
-
85
- ancestors.each do |base|
86
- if base.include?(RProgram::Options)
87
- return true if base.options.has_key?(name)
88
- end
89
- end
90
-
91
- return false
92
- end
93
-
94
- #
95
- # Searches for the option with the matching name in the class and
96
- # it's ancestors.
97
- #
98
- # @param [Symbol, String] name
99
- # The name to search for.
100
- #
101
- # @return [Option]
102
- # The option with the matching name.
103
- #
104
- def get_option(name)
105
- name = name.to_sym
106
-
107
- ancestors.each do |base|
108
- if base.include?(RProgram::Options)
109
- return base.options[name] if base.options.has_key?(name)
110
- end
111
- end
112
-
113
- return nil
114
- end
115
- end
116
- end
117
-
118
- #
119
- # @see self.has_non_option?
120
- #
121
- def has_non_option?(name)
122
- self.class.has_non_option?(name)
123
- end
124
-
125
- #
126
- # @see self.get_non_option
127
- #
128
- def get_non_option(name)
129
- self.class.get_non_option(name)
130
- end
131
-
132
- #
133
- # @see self.has_option?
134
- #
135
- def has_option?(name)
136
- self.class.has_option?(name)
137
- end
138
-
139
- #
140
- # @see self.get_option
141
- #
142
- def get_option(name)
143
- self.class.get_option(name)
144
- end
145
- end
146
- end
1
+ require 'rprogram/options/class_methods'
2
+ require 'rprogram/options/options'
@@ -65,7 +65,7 @@ module RProgram
65
65
  #
66
66
  # @return [Program, nil]
67
67
  # Returns the newly created Program object. If the given path was
68
- # not a valid file, +nil+ will be returned.
68
+ # not a valid file, `nil` will be returned.
69
69
  #
70
70
  # @example
71
71
  # Program.find_with_path('/bin/cd')
@@ -99,7 +99,7 @@ module RProgram
99
99
  #
100
100
  # @return [Program, nil]
101
101
  # Returns the newly created Program object. If none of the given
102
- # paths were valid files, +nil+ will be returned.
102
+ # paths were valid files, `nil` will be returned.
103
103
  #
104
104
  # @example
105
105
  # Program.find_with_paths(['/bin/cd','/usr/bin/cd'])
@@ -116,7 +116,7 @@ module RProgram
116
116
  end
117
117
 
118
118
  #
119
- # Finds and creates the program using it's +program_names+.
119
+ # Finds and creates the program using it's `program_names`.
120
120
  #
121
121
  # @param [Array] arguments
122
122
  # Additional arguments to initialize the program object with.
@@ -132,7 +132,7 @@ module RProgram
132
132
  # The newly created program object.
133
133
  #
134
134
  # @raise [ProgramNotFound]
135
- # Non of the +program_names+ represented valid programs on the system.
135
+ # Non of the `program_names` represented valid programs on the system.
136
136
  #
137
137
  # @example
138
138
  # Program.find
@@ -144,7 +144,8 @@ module RProgram
144
144
  # end
145
145
  #
146
146
  def self.find(*arguments,&block)
147
- path = Compat.find_program_by_names(*self.program_names)
147
+ path = self.path
148
+ path ||= Compat.find_program_by_names(*self.program_names)
148
149
 
149
150
  unless path
150
151
  names = self.program_names.map { |name| name.dump }.join(', ')
@@ -186,7 +187,7 @@ module RProgram
186
187
  # Specifies whether the program exited successfully.
187
188
  #
188
189
  # @raise [ProgramNotFound]
189
- # Indicates that the +sudo+ program could not be located.
190
+ # Indicates that the `sudo` program could not be located.
190
191
  #
191
192
  # @since 0.1.8
192
193
  #
data/lib/rprogram/task.rb CHANGED
@@ -72,7 +72,7 @@ module RProgram
72
72
  # Specifies whether the task will be ran under sudo.
73
73
  #
74
74
  # @return [Boolean]
75
- # Returns +true+ if sudo is enabled, returns +false+ otherwise.
75
+ # Returns `true` if sudo is enabled, returns `false` otherwise.
76
76
  #
77
77
  # @since 0.1.8
78
78
  #
@@ -230,7 +230,7 @@ module RProgram
230
230
 
231
231
  self.non_options[name] = NonOption.new(options)
232
232
 
233
- class_def(name) do
233
+ define_method(name) do
234
234
  if options[:multiple]
235
235
  @options[name] ||= []
236
236
  else
@@ -238,7 +238,7 @@ module RProgram
238
238
  end
239
239
  end
240
240
 
241
- class_def("#{name}=") do |value|
241
+ define_method("#{name}=") do |value|
242
242
  @options[name] = value
243
243
  end
244
244
  end
@@ -254,7 +254,7 @@ module RProgram
254
254
  #
255
255
  # @option options [Symbol] :name
256
256
  # The name of the option. Defaults to the flag_namify'ed form of
257
- # <tt>options[:flag]</tt>, if not given.
257
+ # `options[:flag]`, if not given.
258
258
  #
259
259
  # @option options [true, false] :multiply (false)
260
260
  # Specifies that the option may appear multiple times in the
@@ -327,7 +327,7 @@ module RProgram
327
327
 
328
328
  self.options[method_name] = Option.new(options,&block)
329
329
 
330
- class_def(method_name) do
330
+ define_method(method_name) do
331
331
  if options[:sub_options]
332
332
  @options[method_name] ||= OptionList.new
333
333
  elsif options[:multiple]
@@ -337,7 +337,7 @@ module RProgram
337
337
  end
338
338
  end
339
339
 
340
- class_def("#{method_name}=") do |value|
340
+ define_method("#{method_name}=") do |value|
341
341
  if options[:sub_options]
342
342
  @options[method_name] = OptionList.new(value)
343
343
  else
@@ -1,4 +1,4 @@
1
1
  module RProgram
2
2
  # Version of RProgram
3
- VERSION = '0.1.8'
3
+ VERSION = '0.2.0'
4
4
  end
data/rprogram.gemspec ADDED
@@ -0,0 +1,104 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rprogram}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Postmodern"]
12
+ s.date = %q{2010-10-03}
13
+ s.description = %q{RProgram is a library for creating wrappers around command-line programs. RProgram provides a Rubyful interface to programs and all their options or non-options. RProgram can also search for programs installed on a system.}
14
+ s.email = %q{postmodern.mod3@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "ChangeLog.md",
17
+ "LICENSE.txt",
18
+ "README.md"
19
+ ]
20
+ s.files = [
21
+ ".gitignore",
22
+ ".specopts",
23
+ ".yardopts",
24
+ "ChangeLog.md",
25
+ "LICENSE.txt",
26
+ "README.md",
27
+ "Rakefile",
28
+ "lib/rprogram.rb",
29
+ "lib/rprogram/compat.rb",
30
+ "lib/rprogram/exceptions.rb",
31
+ "lib/rprogram/exceptions/program_not_found.rb",
32
+ "lib/rprogram/extensions.rb",
33
+ "lib/rprogram/nameable.rb",
34
+ "lib/rprogram/nameable/class_methods.rb",
35
+ "lib/rprogram/nameable/nameable.rb",
36
+ "lib/rprogram/non_option.rb",
37
+ "lib/rprogram/option.rb",
38
+ "lib/rprogram/option_list.rb",
39
+ "lib/rprogram/options.rb",
40
+ "lib/rprogram/options/class_methods.rb",
41
+ "lib/rprogram/options/options.rb",
42
+ "lib/rprogram/program.rb",
43
+ "lib/rprogram/rprogram.rb",
44
+ "lib/rprogram/task.rb",
45
+ "lib/rprogram/version.rb",
46
+ "lib/rprogram/yard.rb",
47
+ "rprogram.gemspec",
48
+ "spec/classes/aliased_program.rb",
49
+ "spec/classes/ls_program.rb",
50
+ "spec/classes/ls_selinux_task.rb",
51
+ "spec/classes/ls_task.rb",
52
+ "spec/classes/named_program.rb",
53
+ "spec/compat_spec.rb",
54
+ "spec/nameable_spec.rb",
55
+ "spec/non_option_spec.rb",
56
+ "spec/option_examples.rb",
57
+ "spec/option_list_spec.rb",
58
+ "spec/option_spec.rb",
59
+ "spec/program_spec.rb",
60
+ "spec/rprogram_spec.rb",
61
+ "spec/spec_helper.rb",
62
+ "spec/task_spec.rb"
63
+ ]
64
+ s.has_rdoc = %q{yard}
65
+ s.homepage = %q{http://github.com/postmodern/rprogram}
66
+ s.rdoc_options = ["--charset=UTF-8"]
67
+ s.require_paths = ["lib"]
68
+ s.rubygems_version = %q{1.3.7}
69
+ s.summary = %q{A library for creating wrappers around command-line programs.}
70
+ s.test_files = [
71
+ "spec/classes/ls_program.rb",
72
+ "spec/classes/named_program.rb",
73
+ "spec/classes/ls_selinux_task.rb",
74
+ "spec/classes/aliased_program.rb",
75
+ "spec/classes/ls_task.rb",
76
+ "spec/nameable_spec.rb",
77
+ "spec/program_spec.rb",
78
+ "spec/rprogram_spec.rb",
79
+ "spec/option_examples.rb",
80
+ "spec/non_option_spec.rb",
81
+ "spec/compat_spec.rb",
82
+ "spec/option_list_spec.rb",
83
+ "spec/spec_helper.rb",
84
+ "spec/option_spec.rb",
85
+ "spec/task_spec.rb"
86
+ ]
87
+
88
+ if s.respond_to? :specification_version then
89
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
90
+ s.specification_version = 3
91
+
92
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
93
+ s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
94
+ s.add_development_dependency(%q<yard>, [">= 0.5.3"])
95
+ else
96
+ s.add_dependency(%q<rspec>, [">= 1.3.0"])
97
+ s.add_dependency(%q<yard>, [">= 0.5.3"])
98
+ end
99
+ else
100
+ s.add_dependency(%q<rspec>, [">= 1.3.0"])
101
+ s.add_dependency(%q<yard>, [">= 0.5.3"])
102
+ end
103
+ end
104
+
@@ -52,4 +52,32 @@ describe Nameable do
52
52
  obj.program_names.should == ['ls', 'dir']
53
53
  end
54
54
  end
55
+
56
+ describe "path" do
57
+ subject { NamedProgram }
58
+ after(:all) { NamedProgram.path = nil }
59
+
60
+ it "should not have a path by default" do
61
+ subject.path.should be_nil
62
+ end
63
+
64
+ it "should allow setting the path" do
65
+ new_path = '/usr/bin/ls'
66
+
67
+ subject.path = new_path
68
+ subject.path.should == new_path
69
+ end
70
+
71
+ it "should expand paths" do
72
+ subject.path = '/usr/../bin/ls'
73
+
74
+ subject.path.should == '/bin/ls'
75
+ end
76
+
77
+ it "should allow setting the path to nil" do
78
+ subject.path = nil
79
+
80
+ subject.path.should be_nil
81
+ end
82
+ end
55
83
  end
data/spec/program_spec.rb CHANGED
@@ -4,22 +4,19 @@ require 'spec_helper'
4
4
  require 'classes/ls_program'
5
5
 
6
6
  describe Program do
7
- it "should create a Program from a path" do
8
- prog = Program.new('/usr/bin/ruby')
7
+ subject { Program.new('/usr/bin/cc') }
8
+ after(:all) { LS.path = nil }
9
9
 
10
- prog.should_not be_nil
10
+ it "should create a Program from a path" do
11
+ subject.should_not be_nil
11
12
  end
12
13
 
13
14
  it "should derive the program name from a path" do
14
- prog = Program.new('/usr/bin/ruby')
15
-
16
- prog.name.should == 'ruby'
15
+ subject.name.should == 'cc'
17
16
  end
18
17
 
19
18
  it "should return the program path when converted to a String" do
20
- prog = Program.new('/usr/bin/ruby')
21
-
22
- prog.to_s.should == '/usr/bin/ruby'
19
+ subject.to_s.should == '/usr/bin/cc'
23
20
  end
24
21
 
25
22
  it "should raise an exception for invalid paths" do
@@ -29,7 +26,7 @@ describe Program do
29
26
  end
30
27
 
31
28
  it "should find a program from a path" do
32
- prog = Program.find_with_path('/usr/bin/dir')
29
+ prog = Program.find_with_path('/usr/bin/cc')
33
30
 
34
31
  prog.should_not be_nil
35
32
  end
@@ -41,12 +38,20 @@ describe Program do
41
38
  end
42
39
 
43
40
  it "should be able to find a program based on the program names" do
44
- ls = nil
41
+ ls = LS.find
42
+
43
+ File.executable?(ls.path).should == true
44
+ end
45
45
 
46
+ it "should raise a ProgramNotFound exception if no path/name is valid" do
46
47
  lambda {
47
- ls = LS.find
48
- }.should_not raise_error(ProgramNotFound)
48
+ Program.find
49
+ }.should raise_error(ProgramNotFound)
50
+ end
49
51
 
50
- File.executable?(ls.path).should == true
52
+ it "should allow using a default path" do
53
+ LS.path = '/usr/bin/dir'
54
+
55
+ LS.find.path.should == LS.path
51
56
  end
52
57
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'rubygems'
2
- gem 'rspec', '>=1.2.8'
2
+ gem 'rspec', '>=1.3.0'
3
3
  require 'spec'
4
4
 
5
5
  require 'rprogram/version'