visionmedia-rext 0.0.6 → 0.0.7

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.
data/History.rdoc CHANGED
@@ -1,4 +1,9 @@
1
1
 
2
+ === 0.0.7 / 2009-04-08
3
+
4
+ * Added Hash#switchify
5
+ * Added String#switchify
6
+
2
7
  === 0.0.6 / 2009-04-01
3
8
 
4
9
  * Added String#from
data/README.rdoc CHANGED
@@ -59,6 +59,7 @@ Below are the methods currently provided by Rext.
59
59
  - file
60
60
  - files
61
61
  - path
62
+ - switchify
62
63
 
63
64
  * Integer
64
65
  - ordanalize
@@ -72,6 +73,7 @@ Below are the methods currently provided by Rext.
72
73
 
73
74
  * Hash
74
75
  - delete_at / extract!
76
+ - switchify
75
77
 
76
78
  * Time
77
79
  - in_words_since / in_words_since_now
@@ -20,4 +20,23 @@ class Hash
20
20
  end
21
21
  alias :extract! :delete_at
22
22
 
23
+ ##
24
+ # Return an array of switches and their arguments.
25
+ #
26
+ # === Examples
27
+ #
28
+ # { :use_foobar => true }.switchify # => ['--use-foobar']
29
+ # { :use_foobar => false }.switchify # => []
30
+ # { :interval => 15, :icon => :jpeg } # => ['--interval', '15', '--icon', 'jpeg']
31
+ #
32
+
33
+ def switchify
34
+ inject [] do |args, (key, value)|
35
+ next args unless value
36
+ args << key.to_s.switchify
37
+ args << (String === value ? value.inspect : value.to_s) unless value === true
38
+ args
39
+ end
40
+ end
41
+
23
42
  end
@@ -213,4 +213,29 @@ class String
213
213
  self[n, length]
214
214
  end
215
215
 
216
+ ##
217
+ # Replace all underscores with hyphens.
218
+
219
+ def dasherize
220
+ tr '_', '-'
221
+ end
222
+
223
+ ##
224
+ # Returns the switch equivilant of this string.
225
+ #
226
+ # === Examples
227
+ #
228
+ # 'foo_bar'.switchify # => --foo-bar
229
+ # 'lots_of_foobar'.switchify # => --lots-of-foobar
230
+ # 't'.switchify # => -t
231
+ # ''.switchify # => InvalidSwitchError
232
+ #
233
+
234
+ class InvalidSwitchError < StandardError; end
235
+
236
+ def switchify
237
+ raise InvalidSwitchError, 'switch must have a length > 0' if length.zero?
238
+ length > 1 ? "--#{dasherize}" : "-#{self}"
239
+ end
240
+
216
241
  end
data/lib/rext/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
 
2
2
  module Rext
3
- VERSION = '0.0.6'
3
+ VERSION = '0.0.7'
4
4
  end
data/rext.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{rext}
5
- s.version = "0.0.6"
5
+ s.version = "0.0.7"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["TJ Holowaychuk"]
9
- s.date = %q{2009-04-01}
9
+ s.date = %q{2009-04-08}
10
10
  s.description = %q{Ruby extensions}
11
11
  s.email = %q{tj@vision-media.ca}
12
12
  s.extra_rdoc_files = ["lib/rext/all.rb", "lib/rext/array/helpers.rb", "lib/rext/array.rb", "lib/rext/compat/basic_object.rb", "lib/rext/compat.rb", "lib/rext/date/helpers.rb", "lib/rext/date.rb", "lib/rext/enumerable/helpers.rb", "lib/rext/enumerable.rb", "lib/rext/hash/helpers.rb", "lib/rext/hash.rb", "lib/rext/integer/helpers.rb", "lib/rext/integer.rb", "lib/rext/module/helpers.rb", "lib/rext/module.rb", "lib/rext/numeric/bytes.rb", "lib/rext/numeric/time.rb", "lib/rext/numeric.rb", "lib/rext/object/helpers.rb", "lib/rext/object/metaclass.rb", "lib/rext/object.rb", "lib/rext/proc/helpers.rb", "lib/rext/proc.rb", "lib/rext/string/escape.rb", "lib/rext/string/helpers.rb", "lib/rext/string.rb", "lib/rext/time/helpers.rb", "lib/rext/time.rb", "lib/rext/version.rb", "lib/rext.rb", "README.rdoc", "tasks/benchmark.rake", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
data/spec/hash_spec.rb CHANGED
@@ -12,5 +12,24 @@ describe Hash do
12
12
  does_not_exist.should be_nil
13
13
  end
14
14
  end
15
+
16
+ describe "#switchify" do
17
+ it "should work with bools" do
18
+ { :use_foobar => true }.switchify.should == ['--use-foobar']
19
+ { :use_foobar => false }.switchify.should be_empty
20
+ end
21
+
22
+ it "should stringify numbers and symbols" do
23
+ { :interval => 15, :icon => :jpeg }.switchify.should == ['--interval', '15', '--icon', 'jpeg']
24
+ end
25
+
26
+ it "should work with little switches" do
27
+ { :T => true }.switchify.should == ['-T']
28
+ end
29
+
30
+ it "should add quotes around strings" do
31
+ { :glob => 'lib/**/*.rb' }.switchify.should == ['--glob', '"lib/**/*.rb"']
32
+ end
33
+ end
15
34
  end
16
35
  end
data/spec/string_spec.rb CHANGED
@@ -4,6 +4,20 @@ require 'rext/string'
4
4
  describe String do
5
5
  describe "helpers" do
6
6
 
7
+ describe "#switchify" do
8
+ it "should return a switch version of the string" do
9
+ 'some_foo_bar'.switchify.should == '--some-foo-bar'
10
+ end
11
+
12
+ it "should return small switches when only a single char" do
13
+ 't'.switchify.should == '-t'
14
+ end
15
+
16
+ it "should raise an InvalidSwitchError when length 0" do
17
+ lambda { ''.switchify }.should raise_error(String::InvalidSwitchError)
18
+ end
19
+ end
20
+
7
21
  describe "#base64_encode" do
8
22
  it "should base64 encode a string" do
9
23
  'tj'.base64_encode.should == 'dGo='
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: visionmedia-rext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - TJ Holowaychuk
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-01 00:00:00 -07:00
12
+ date: 2009-04-08 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15