epitools 0.5.39 → 0.5.41

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8f1ff3a3fd6c4acf8419fa95fb87354aad10def9
4
- data.tar.gz: 2cb1a53c82da0c000945fa678fcf2c6cff755956
3
+ metadata.gz: 11d3c2b4e8f320ff75a614b617aa3571a8e4a89c
4
+ data.tar.gz: a33291591096efd021c4d8647d5e73e23dffa745
5
5
  SHA512:
6
- metadata.gz: 8805fe2849572feaa48d050842c02f4993a055011c514fb96adf66f31e01f7bbed7800ba54302199061d777b8142925eb251de97d91c93a989f266329c828511
7
- data.tar.gz: 54739fab89cc8e77d25bb3d8bc0e79197dde2caf1c3ca9fd2451a0699521d74754a1fe3a095288bae05b0a4bff19728b2404808ff8f1a5e1c7f0c23d81466de9
6
+ metadata.gz: 276e60d3e8028ebf5ebbac51ba8c33575f9c64bfc7997d828c13a8fd2a0a47d23a78e5f6f39b10d2de91cd16d8f7edb6435c842b3e274b0136604bbb9d45ff03
7
+ data.tar.gz: 284f2fa58e0fd4dc8e536b66515fce57a61a1fd7035623ea4360ebdf787c97c938f47eff235ca5ab47e4f424ea778e519fe8d0c306a372947a4aa4e3a1adca6f
data/Rakefile CHANGED
@@ -11,3 +11,7 @@ end
11
11
  task :install => :build do
12
12
  system "gem install epitools-#{gem_version}.gem"
13
13
  end
14
+
15
+ task :pry do
16
+ system "pry --gem"
17
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.39
1
+ 0.5.41
@@ -10,6 +10,7 @@ require 'epitools/core_ext/enumerable'
10
10
  require 'epitools/core_ext/hash'
11
11
  require 'epitools/core_ext/numbers'
12
12
  require 'epitools/core_ext/truthiness'
13
+ require 'epitools/core_ext/range'
13
14
  require 'epitools/core_ext/misc'
14
15
 
15
16
  ##############################################################################
@@ -169,28 +169,6 @@ end
169
169
 
170
170
 
171
171
 
172
- class Range
173
-
174
- #
175
- # Pick a random number from the range.
176
- #
177
- def rand
178
- Kernel.rand(self)
179
- end
180
-
181
- #
182
- # The middle element of this range.
183
- #
184
- def middle
185
- (min + max) / 2
186
- end
187
-
188
-
189
-
190
- end
191
-
192
-
193
-
194
172
  class Struct
195
173
 
196
174
  #
@@ -0,0 +1,35 @@
1
+ class Range
2
+
3
+ #
4
+ # Pick a random number from the range.
5
+ #
6
+ def rand
7
+ Kernel.rand(self)
8
+ end
9
+
10
+ #
11
+ # The middle element of this range.
12
+ #
13
+ def middle
14
+ (min + max) / 2
15
+ end
16
+
17
+ #
18
+ # Return a new range which is the intersection of the two ranges
19
+ #
20
+ def &(other)
21
+ mins, maxes = minmax.zip(other.minmax)
22
+
23
+ (mins.max..maxes.min)
24
+ end
25
+
26
+ #
27
+ # Return a new range which is the union of the two ranges
28
+ #
29
+ def |(other)
30
+ mins, maxes = minmax.zip(other.minmax)
31
+
32
+ (mins.min..maxes.max)
33
+ end
34
+
35
+ end
@@ -175,8 +175,13 @@ class Path
175
175
  def ext=(newext)
176
176
  if newext.blank?
177
177
  @ext = nil
178
- elsif newext.startswith('.')
179
- @ext = newext[1..-1]
178
+ return
179
+ end
180
+
181
+ newext = newext[1..-1] if newext.startswith('.')
182
+
183
+ if newext['.']
184
+ self.filename = basename + '.' + newext
180
185
  else
181
186
  @ext = newext
182
187
  end
@@ -473,7 +478,7 @@ class Path
473
478
 
474
479
  IO.popen(cmd, "rb", :err=>[:child, :out]) do |io|
475
480
  result = io.each_line.to_a
476
- error = {cmd: cmd, result: result.to_s}.inspect
481
+ error = {:cmd => cmd, :result => result.to_s}.inspect
477
482
  raise error if result.any?
478
483
  end
479
484
  end
@@ -692,34 +697,106 @@ class Path
692
697
  Path.cd(path, &block)
693
698
  end
694
699
 
700
+ #
701
+ # A private method for handling arguments to mv and rename.
702
+ #
703
+ def arg_to_path(arg)
704
+ case arg
705
+ when String, Path
706
+ Path[arg]
707
+ when Hash
708
+ self.with(arg)
709
+ else
710
+ raise "Error: argument must be a path (a String or a Path), or a hash of attributes to replace in the Path."
711
+ end
712
+ end
713
+ private :arg_to_path
714
+
715
+ #
716
+ # Renames the file, but doesn't change the current Path object, and returns a Path that points at the new filename.
695
717
  #
696
718
  # Examples:
719
+ # Path["file"].rename("newfile") #=> Path["newfile"]
697
720
  # Path["SongySong.mp3"].rename(:basename=>"Songy Song")
698
721
  # Path["Songy Song.mp3"].rename(:ext=>"aac")
699
722
  # Path["Songy Song.aac"].rename(:dir=>"/music2")
700
723
  # Path["/music2/Songy Song.aac"].exists? #=> true
701
724
  #
702
- def rename(options)
703
- raise "Options must be a Hash" unless options.is_a? Hash
704
- dest = self.with(options)
725
+ def rename(arg)
726
+ dest = arg_to_path(arg)
705
727
 
706
728
  raise "Error: destination (#{dest.inspect}) already exists" if dest.exists?
729
+ raise "Error: can't rename #{self.inspect} because source location doesn't exist." unless exists?
730
+
707
731
  File.rename(path, dest)
732
+ dest
733
+ end
734
+ alias_method :ren, :rename
708
735
 
709
- update(dest)
736
+ #
737
+ # Works the same as "rename", but the destination can be on another disk.
738
+ #
739
+ def mv(arg)
740
+ dest = arg_to_path(arg)
710
741
 
711
- self
742
+ raise "Error: can't move #{self.inspect} because source location doesn't exist." unless exists?
743
+
744
+ FileUtils.mv(path, dest)
745
+ dest
746
+ end
747
+ alias_method :move, :mv
748
+
749
+ #
750
+ # Rename the file and change this Path object so that it points to the destination file.
751
+ #
752
+ def rename!(arg)
753
+ update(rename(arg))
754
+ end
755
+ alias_method :ren!, :rename!
756
+
757
+ #
758
+ # Moves the file (overwriting the destination if it already exists). Also points the current Path object at the new destination.
759
+ #
760
+ def mv!(arg)
761
+ update(mv(arg))
762
+ end
763
+ alias_method :move!, :mv!
764
+
765
+ #
766
+ # Find a backup filename that doesn't exist yet by appending "(1)", "(2)", etc. to the current filename.
767
+ #
768
+ def numbered_backup_file
769
+ return self unless exists?
770
+
771
+ n = 1
772
+ loop do
773
+ new_file = with(:basename => "#{basename} (#{n})")
774
+ return new_file unless new_file.exists?
775
+ n += 1
776
+ end
777
+ end
778
+
779
+ #
780
+ # Return a copy of this Path with ".bak" at the end
781
+ #
782
+ def backup_file
783
+ with(:filename => filename+".bak")
712
784
  end
713
785
 
714
786
  #
715
- # Renames the file the specified full path (like Dir.rename.)
787
+ # Rename this file, "filename.ext", to "filename (1).ext" (or (2), or (3), or whatever number is available.)
788
+ # (Does not modify this Path object.)
716
789
  #
717
- def rename_to(path)
718
- rename :path=>path.to_s
790
+ def numbered_backup!
791
+ rename(numbered_backup_file)
719
792
  end
720
793
 
721
- def rename_to!(path)
722
- rename! :path=>path.to_s
794
+ #
795
+ # Rename this file, "filename.ext", to "filename.ext.bak".
796
+ # (Does not modify this Path object.)
797
+ #
798
+ def backup!
799
+ rename(backup_file)
723
800
  end
724
801
 
725
802
  #
@@ -748,10 +825,7 @@ class Path
748
825
 
749
826
  def cp_r(dest)
750
827
  FileUtils.cp_r(path, dest) #if Path[dest].exists?
751
- end
752
-
753
- def mv(dest)
754
- FileUtils.mv(path, dest)
828
+ dest
755
829
  end
756
830
 
757
831
  def ln_s(dest)
@@ -107,6 +107,8 @@ module WM
107
107
  # string is made up of regular text, plus <>'d keypresses
108
108
  # eg: "Hello<Ctrl-T><Ctrl-L><Ctrl-Shift-K><Return>!!!"
109
109
  #
110
+ # TODO: add `xdotool` support
111
+ #
110
112
  def send_keys(keys)
111
113
  xse(keys)
112
114
  end
@@ -636,6 +636,13 @@ describe Range do
636
636
  50.times { r.includes?(r.rand).should == true }
637
637
  end
638
638
 
639
+ it "does | and &" do
640
+ a = (1..10)
641
+ b = (5...21)
642
+ a & b == (5..10)
643
+ a | b == (1..20)
644
+ end
645
+
639
646
  end
640
647
 
641
648
  describe ObjectSpace do
@@ -55,6 +55,10 @@ describe Path do
55
55
  path.ext.should == "mkv"
56
56
 
57
57
  path.filename[-4..-1].should == ".mkv"
58
+
59
+ path.ext += ".extra"
60
+ path.filename.should == "hello.mkv.extra"
61
+ path.ext.should == "extra"
58
62
  end
59
63
 
60
64
  it "replaces filename" do
@@ -208,7 +212,7 @@ describe Path do
208
212
  Path.pwd.should == start
209
213
  end
210
214
 
211
- it "renames" do
215
+ it "renames/mvs" do
212
216
  path = Path.tmp
213
217
 
214
218
  path.rm
@@ -218,10 +222,49 @@ describe Path do
218
222
 
219
223
  old_name = path.to_s
220
224
 
221
- path.rename(:ext=>".dat")
225
+ dest = path.rename(:ext=>".dat")
226
+
227
+ dest.to_s.should == old_name+".dat"
228
+ path.to_s.should == old_name
229
+ dest.to_s.should_not == old_name
230
+
231
+ dest.exists?.should == true
232
+ path.exists?.should == false
222
233
 
223
- path.to_s.should == old_name+".dat"
234
+ path.touch
235
+ lambda { path.rename(:ext=>".dat") }.should raise_error
236
+
237
+ dest.rm
238
+ path.rename!(:ext=>".dat")
224
239
  path.to_s.should_not == old_name
240
+ path.exists?.should == true
241
+
242
+ path.rm
243
+ end
244
+
245
+ it "backups" do
246
+ path = Path.tmp
247
+ path.rm
248
+ path.touch
249
+
250
+ dest = path.backup!
251
+ path.exists?.should == false
252
+ dest.exists?.should == true
253
+
254
+ dest.rm
255
+
256
+ path.touch
257
+ p path.numbered_backup_file
258
+
259
+ dest = path.numbered_backup!
260
+ path.touch
261
+ dest2 = path.numbered_backup!
262
+
263
+ dest.should_not == dest2
264
+ path.should_not == dest
265
+
266
+ dest.rm
267
+ dest2.rm
225
268
  end
226
269
 
227
270
  it "rms" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epitools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.39
4
+ version: 0.5.41
5
5
  platform: ruby
6
6
  authors:
7
7
  - epitron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-26 00:00:00.000000000 Z
11
+ date: 2013-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -58,6 +58,7 @@ files:
58
58
  - lib/epitools/core_ext/misc.rb
59
59
  - lib/epitools/core_ext/numbers.rb
60
60
  - lib/epitools/core_ext/object.rb
61
+ - lib/epitools/core_ext/range.rb
61
62
  - lib/epitools/core_ext/string.rb
62
63
  - lib/epitools/core_ext/truthiness.rb
63
64
  - lib/epitools/ezdb.rb