epitools 0.5.13 → 0.5.14

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/Rakefile CHANGED
@@ -1,22 +1,20 @@
1
- ### Require all gems...
2
- %w[
3
-
4
- rubygems
5
-
6
- rake
7
- rake/rdoctask
8
- rspec/core
9
- rspec/core/rake_task
10
- jeweler
11
-
12
- ].each { |mod| require mod }
13
-
14
- desc 'Default: specs.'
15
- task :default => :spec
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ #require 'bundler'
5
+ # begin
6
+ # Bundler.setup(:default, :development)
7
+ # rescue Bundler::BundlerError => e
8
+ # $stderr.puts e.message
9
+ # $stderr.puts "Run `bundle install` to install missing gems"
10
+ # exit e.status_code
11
+ # end
12
+ require 'rake'
16
13
 
17
14
  #
18
15
  # Jewelerrrr
19
16
  #
17
+ require 'jeweler'
20
18
  Jeweler::Tasks.new do |gem|
21
19
  # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
22
20
  gem.name = "epitools"
@@ -36,25 +34,3 @@ Jeweler::Tasks.new do |gem|
36
34
  gem.add_development_dependency "sqlite3-ruby"
37
35
  end
38
36
  Jeweler::RubygemsDotOrgTasks.new
39
-
40
- desc 'Run all the specs.'
41
- RSpec::Core::RakeTask.new(:spec) do |spec|
42
- spec.pattern = FileList['spec/**/*_spec.rb']
43
- end
44
-
45
- desc 'Run rcov code coverage'
46
- RSpec::Core::RakeTask.new(:rcov) do |spec|
47
- spec.pattern = 'spec/**/*_spec.rb'
48
- spec.rcov = true
49
- end
50
-
51
-
52
- desc 'Generate documentation for rdoc.'
53
- Rake::RDocTask.new do |rdoc|
54
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
55
-
56
- rdoc.rdoc_dir = 'rdoc'
57
- rdoc.title = "epitools #{version}"
58
- rdoc.rdoc_files.include('README*')
59
- rdoc.rdoc_files.include('lib/**/*.rb')
60
- end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.13
1
+ 0.5.14
data/epitools.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "epitools"
8
- s.version = "0.5.13"
8
+ s.version = "0.5.14"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["epitron"]
12
- s.date = "2012-11-13"
12
+ s.date = "2012-11-25"
13
13
  s.description = "Miscellaneous utility libraries to make my life easier."
14
14
  s.email = "chris@ill-logic.com"
15
15
  s.extra_rdoc_files = [
@@ -96,6 +96,17 @@ class Array
96
96
 
97
97
  alias_method :unzip, :transpose
98
98
 
99
+ #
100
+ # Convert the array to a hash
101
+ #
102
+ def to_h
103
+ if self.first.is_a? Array
104
+ Hash[self]
105
+ else
106
+ Hash[*self]
107
+ end
108
+ end
109
+
99
110
  end
100
111
 
101
112
 
data/lib/epitools/path.rb CHANGED
@@ -58,12 +58,27 @@ require 'epitools/core_ext/string'
58
58
  #
59
59
  class Path
60
60
 
61
+ # The directories in the path, split into an array. (eg: ['usr', 'src', 'linux'])
62
+ attr_reader :dirs
63
+
64
+ # The filename without an extension
65
+ attr_reader :base
66
+
67
+ # The file extension, including the . (eg: ".mp3")
68
+ attr_reader :ext
69
+
61
70
  ## initializers
62
71
 
63
72
  def initialize(newpath, hints={})
64
73
  self.send("path=", newpath, hints)
65
74
  end
66
75
 
76
+ def initialize_copy(other)
77
+ @dirs = other.dirs.dup
78
+ @base = other.base.dup
79
+ @ext = other.ext.dup
80
+ end
81
+
67
82
  def self.glob(str)
68
83
  Dir[str].map { |entry| new(entry) }
69
84
  end
@@ -99,6 +114,26 @@ class Path
99
114
  attr_writer :base
100
115
  attr_writer :dirs
101
116
 
117
+ #
118
+ # Clear out the internal state of this object, so that it can be reinitialized.
119
+ #
120
+ def reset!
121
+ [:@dirs, :@base, :@ext].each { |var| remove_instance_variable var }
122
+ self
123
+ end
124
+
125
+
126
+ #
127
+ # Reload this path (update cached values.)
128
+ #
129
+ def reload!
130
+ temp = path
131
+ reset!
132
+ self.path = temp
133
+ self
134
+ end
135
+
136
+
102
137
  #
103
138
  # This is the core that initializes the whole class.
104
139
  #
@@ -159,15 +194,6 @@ class Path
159
194
 
160
195
  ## getters
161
196
 
162
- # The directories in the path, split into an array. (eg: ['usr', 'src', 'linux'])
163
- attr_reader :dirs
164
-
165
- # The filename without an extension
166
- attr_reader :base
167
-
168
- # The file extension, including the . (eg: ".mp3")
169
- attr_reader :ext
170
-
171
197
  # Joins and returns the full path
172
198
  def path
173
199
  if d = dir
@@ -488,6 +514,7 @@ class Path
488
514
  def read_json
489
515
  JSON.load(io)
490
516
  end
517
+ alias_method :from_json, :read_json
491
518
 
492
519
  # Convert the object to JSON and write it to the file (overwriting the existing file).
493
520
  def write_json(object)
@@ -498,6 +525,7 @@ class Path
498
525
  def read_html
499
526
  Nokogiri::HTML(io)
500
527
  end
528
+ alias_method :from_html, :read_html
501
529
 
502
530
 
503
531
  # Convert the object to YAML and write it to the file (overwriting the existing file).
@@ -509,6 +537,7 @@ class Path
509
537
  def read_yaml
510
538
  YAML.load(io)
511
539
  end
540
+ alias_method :from_yaml, :read_yaml
512
541
 
513
542
 
514
543
  def read_xml
@@ -533,6 +562,14 @@ class Path
533
562
  write BSON.serialize(object)
534
563
  end
535
564
 
565
+ #
566
+ # Change into the directory. If a block is given, it changes into
567
+ # the directory for the duration of the block, then puts you back where you
568
+ # came from once the block is finished.
569
+ #
570
+ def cd(&block)
571
+ Path.cd(path, &block)
572
+ end
536
573
 
537
574
  #
538
575
  # Examples:
@@ -577,10 +614,6 @@ raise "Broken!"
577
614
  end
578
615
  alias_method :mv!, :rename_to!
579
616
 
580
- def reload!
581
- self.path = to_s
582
- end
583
-
584
617
  #
585
618
  # Generate two almost identical methods: mkdir and mkdir_p
586
619
  #
@@ -592,16 +625,13 @@ raise "Broken!"
592
625
  def #{method}
593
626
  if exists?
594
627
  if directory?
595
- Path[path]
628
+ reload!
596
629
  else
597
630
  raise "Error: A file by this name already exists."
598
631
  end
599
632
  else
600
- #{command}(path)
601
- #Path[path]
602
- p [:path, path]
603
- self.path = path # regenerate object
604
- p [:path, path]
633
+ #{command} path # Make the directory
634
+ reload!
605
635
  self
606
636
  end
607
637
  end
@@ -926,7 +956,26 @@ raise "Broken!"
926
956
  @@dir_stack.pop
927
957
  end
928
958
 
929
- def self.cd(dest); Dir.chdir(dest); end
959
+ #
960
+ # Change into the directory "dest". If a block is given, it changes into
961
+ # the directory for the duration of the block, then puts you back where you
962
+ # came from once the block is finished.
963
+ #
964
+ def self.cd(dest, &block)
965
+ dest = Path[dest]
966
+
967
+ raise "Can't 'cd' into #{dest}" unless dest.dir?
968
+
969
+ if block_given?
970
+ orig = pwd
971
+
972
+ Dir.chdir(dest)
973
+ yield
974
+ Dir.chdir(orig)
975
+ else
976
+ Dir.chdir(dest)
977
+ end
978
+ end
930
979
 
931
980
  def self.ls(path); Path[path].ls end
932
981
 
data/spec/path_spec.rb CHANGED
@@ -12,6 +12,14 @@ describe Path do
12
12
  path.base.should == "hello"
13
13
  end
14
14
 
15
+ it "dups itself" do
16
+ path = Path.new "/whatever/blah/stuff.ext"
17
+ path2 = path.dup
18
+ path2.path.should == path.path
19
+ path.dirs.pop
20
+ path2.path.should_not == path.path
21
+ end
22
+
15
23
  it "works with relative paths" do
16
24
  path = Path.new("../hello.mp3/blah")
17
25
 
@@ -187,6 +195,14 @@ describe Path do
187
195
  path.read.should == "rawr"
188
196
  end
189
197
 
198
+ it "cds into directories" do
199
+ path = Path["/etc"]
200
+ start = Path.pwd
201
+ path.should_not == Path.pwd
202
+ path.cd { path.should == Path.pwd }
203
+ Path.pwd.should == start
204
+ end
205
+
190
206
  it "renames" do
191
207
  path = Path.tmpfile
192
208
 
@@ -288,7 +304,7 @@ describe Path do
288
304
  Path.which("ruby").should_not be_nil
289
305
  Path.which("asdfasdfhkajlsdhfkljashdf").should be_nil
290
306
  Path.which("ruby").class.should == Path
291
- Path.which("cat", "ls", "rm").should == ["/bin/cat", "/bin/ls", "/bin/rm"]
307
+ Path.which("ps", "sh", "tar").should == ["/bin/ps", "/bin/sh", "/bin/tar"]
292
308
  end
293
309
 
294
310
  it "Path[]s another path" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epitools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.13
4
+ version: 0.5.14
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-13 00:00:00.000000000 Z
12
+ date: 2012-11-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec