benzo 1.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  *Take the edge off when doing (command) lines.*
4
4
 
5
+ **This is the unstable/2.0.0 branch. Docs may be incomplete or completely incorrect.
6
+ This is your warning.**
7
+
5
8
  Wrapping [Cocaine](https://github.com/thoughtbot/cocaine), this library will
6
9
  greatly simplify building complex and conditional commandline arguments.
7
10
 
@@ -11,7 +14,7 @@ This is especially useful when creating wrappers for other commandline tools.
11
14
 
12
15
  Add this line to your application's Gemfile:
13
16
 
14
- gem 'benzo'
17
+ gem 'benzo', '~> 2.0.0'
15
18
 
16
19
  And then execute:
17
20
 
@@ -33,14 +36,11 @@ in your code, you may want to have certain arguments shown or not shown:
33
36
  @file = 'app_prod-dump'
34
37
 
35
38
  # build the Cocaine::Commandline with Benzo
36
- line = Benzo.line('pg_dump', {
39
+ Benzo.run!('pg_dump', {
37
40
  '-v' => @verbose,
38
41
  '--schema-only' => @schema_only, # note, @schema_only is nil
39
42
  '-f :filename' => @file,
40
43
  ':db_name' => @database
41
- })
42
-
43
- line.run # execute the command
44
44
  ```
45
45
 
46
46
  Benzo takes 2 arguments: `command` and `options_map`. The command is, like in
@@ -50,8 +50,41 @@ the data necessary to build the commandline arguments.
50
50
  Any value in the hash that evaluates to `false` (this includes `nil`) will be
51
51
  omitted from the command.
52
52
 
53
+ ## A More Complex Example
54
+
55
+ ```ruby
56
+ # the state of our app:
57
+ @verbose = true
58
+ @database = 'app_production'
59
+ @file = 'app_prod-dump'
60
+
61
+ # build a Benzo object:
62
+ b = Benzo.new('pg_dump',
63
+ '-v' => @verbose,
64
+ '--schema-only' => @schema_only, # note, @schema_only is nil
65
+ '-f :filename' => @file,
66
+ ':db_name' => @database
67
+ )
68
+
69
+ b.run # => runs the command: "pg_dump -v -f 'app_prod-dump' 'app_production'"
70
+
71
+ b.options_map['--schema-only'] = true
72
+
73
+ b.command # => pg_dump -v --schema-only -f 'app_prod-dump' 'app_production'
74
+
75
+ c = b.cocaine # => returns a Cocaine::CommandLine object
76
+
77
+ c.run(:db_name => 'other_db', :filename => 'other_db-dump') # => run it with different options
78
+ ```
79
+
80
+ After creating a Benzo object, you can modify the `options_map` on the fly and
81
+ Benzo will re-map it when calling `#command` or `#run`. There is also a `#cocaine`
82
+ function that will return the `Cocaine::CommandLine` object.
83
+
53
84
  ## How options_map works
54
85
 
86
+ See **Limitations and Gotchas** section below if you're not using Benzo in Ruby 1.9.
87
+
55
88
  `options_map` is a hash that gets processed by `Benzo` to create the
56
89
  `Cocaine::CommandLine` object. The hash gets iterated over and any values that
57
90
  evaluate to `false` are discarded, then the keys are concatinated together with
@@ -95,6 +128,12 @@ rescue Cocaine::ExitStatusError => e
95
128
  end
96
129
  ```
97
130
 
131
+ ## Limitations and Gotchas
132
+
133
+ Because Benzo uses Hash keys to build the commandline string and that order may matter,
134
+ if you're not running Ruby 1.9, you should pass an [OrderedHash](https://rubygems.org/gems/orderedhash)
135
+ for the `options_hash`.
136
+
98
137
  ## Contributing
99
138
 
100
139
  1. Fork it
data/benzo.gemspec CHANGED
@@ -15,5 +15,5 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Benzo::VERSION
17
17
 
18
- gem.add_dependency 'cocaine', "~> 0.3.2"
18
+ gem.add_dependency 'cocaine', "~> 0.4.2"
19
19
  end
data/lib/benzo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Benzo
2
- VERSION = "1.0.1"
2
+ VERSION = "2.0.0"
3
3
  end
data/lib/benzo.rb CHANGED
@@ -6,27 +6,52 @@ class Benzo
6
6
  attr_accessor :command, :options_map
7
7
  attr_accessor :line, :vars
8
8
 
9
- def initialize(command, options_map)
9
+ def initialize(command, options_map={})
10
+ Benzo.show_version_warning
10
11
  @command = command
11
12
  @options_map = options_map
12
13
  @line = []
13
14
  @vars = {}
15
+ end
14
16
 
15
- map!
17
+ # given a +command+ and +options_map+, interpolate the variables and
18
+ # run the command.
19
+ # returns the output as a string (as returned from Cocaine)
20
+ def self.run!(command, options_map)
21
+ show_version_warning
22
+ b = new(command, options_map)
23
+ r = b.run
24
+
25
+ b = nil # dealocate object
26
+
27
+ return r
16
28
  end
17
29
 
18
- # convenience method for creating a new Benzo object with the given
19
- # arguments and returning the Cocaine::Commandline
20
- def self.line(command, options_map)
30
+ # given a +command+ and +options_map+, interpolate the variables and
31
+ # return the command that will be run as a string.
32
+ def self.command!(command, options_map)
33
+ show_version_warning
21
34
  b = new(command, options_map)
22
- line = b.to_cocaine
35
+ c = b.command
23
36
 
24
- b = nil
25
- line
37
+ b = nil # dealocate object
38
+
39
+ return c
40
+ end
41
+
42
+ # run the +Cocaine::CommandLine+ command and return the output.
43
+ def run
44
+ cocaine.run(@vars)
26
45
  end
27
46
 
28
- # convert this object into a Cocaine::CommandLine
29
- def to_cocaine
47
+ # return the +Cocaine::CommandLine+ command as a string
48
+ def command
49
+ cocaine.command(@vars)
50
+ end
51
+
52
+ # map the variables and return a +Cocaine::CommandLine+ object
53
+ def cocaine
54
+ map!
30
55
  ::Cocaine::CommandLine.new(@command, @line.join(' '), @vars)
31
56
  end
32
57
 
@@ -35,6 +60,10 @@ class Benzo
35
60
  # iterate over the options map and build
36
61
  # this object's mapping.
37
62
  def map!
63
+ # re-initialize the state
64
+ @line = []
65
+ @vars = {}
66
+
38
67
  @options_map.each do |k,v|
39
68
  sym = if k.is_a? Symbol
40
69
  k
@@ -64,5 +93,13 @@ class Benzo
64
93
  :"#{sym}"
65
94
  end
66
95
 
96
+ # check the current version of ruby
97
+ # if it's a 1.8.x, then show a warning.
98
+ def self.show_version_warning
99
+ if RUBY_VERSION.match /^1\.8/
100
+ $stderr.puts "WARNING: Benzo 2.0 requires ruby 1.9.x unless you use an OrderedHash for the options map!"
101
+ end
102
+ end
103
+
67
104
 
68
105
  end
data/spec/benzo_spec.rb CHANGED
@@ -4,6 +4,54 @@ describe Benzo do
4
4
 
5
5
  let(:benzo) { Benzo.new 't', {} }
6
6
 
7
+ context "::run!" do
8
+
9
+ it "should not error out" do
10
+ lambda { Benzo.run! 'echo', 'hello world' => true }.should_not raise_error
11
+ end
12
+
13
+ it "should return the correct output" do
14
+ Benzo.run!('echo', '-n hello world' => true ).should == 'hello world'
15
+ end
16
+
17
+ end
18
+
19
+ context "::command!" do
20
+
21
+ it "should not error out" do
22
+ lambda { Benzo.command! 'echo', 'hello world' => true }.should_not raise_error
23
+ end
24
+
25
+ it "should return the correct command" do
26
+ Benzo.command!('echo', 'hello world' => true).should == 'echo hello world'
27
+ end
28
+
29
+ end
30
+
31
+ context "#cocaine" do
32
+ let(:benzo) { Benzo.new 'echo', '-n' => true, 'hello world' => true }
33
+
34
+ it "should return a Cocaine::CommandLine object" do
35
+ benzo.cocaine.class.should == ::Cocaine::CommandLine
36
+ end
37
+
38
+ it "should return a Cocaine::CommandLine object with the correct things set" do
39
+ benzo.cocaine.instance_variable_get('@params').should == '-n hello world'
40
+ end
41
+
42
+ it "should return a Cocaine::CommandLine object with the right Cocaine options"
43
+
44
+ end
45
+
46
+ context "#command" do
47
+ let(:benzo) { Benzo.new 'echo', 'hello world' => true }
48
+
49
+ it "should properly interpolate variables" do
50
+ benzo.command.should == 'echo hello world'
51
+ end
52
+
53
+ end
54
+
7
55
  context "#map!" do
8
56
 
9
57
  it "should append to @line if v evaluates to true" do
@@ -88,4 +136,10 @@ describe Benzo do
88
136
 
89
137
  end
90
138
 
139
+ context "when checking the ruby version" do
140
+
141
+ it "should print a warning if the ruby version is 1.8"
142
+
143
+ end
144
+
91
145
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: benzo
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-16 00:00:00.000000000 Z
12
+ date: 2012-12-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cocaine
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 0.3.2
21
+ version: 0.4.2
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: 0.3.2
29
+ version: 0.4.2
30
30
  description: Take the edge off when doing (command) lines.
31
31
  email:
32
32
  - me@spike.cx