pathutil 0.2.0 → 0.3.0

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: 669c8ad0cd1c1453270dc9f60b8cab8a1df7402b
4
- data.tar.gz: 5e29a250d54e9aea640c556f929cc8b83940db71
3
+ metadata.gz: b3e9ed6bd89df225dff1533ada9ef90893a1ea1e
4
+ data.tar.gz: 1050aaa4480d2ba08ad7bcb644491e45803cb8cd
5
5
  SHA512:
6
- metadata.gz: 19cebb9ac016da538e72374cba8c51f5d1a9f229e0cea0f022560257d6b96eac66a43d6a078c7fa7c9ef674df8768213df87950d1b60f489b91ab75f7a2b272c
7
- data.tar.gz: 7d118f6675c9d116b5b5bc670c35fe79a8ba1cd62c0ccd815fb7c4011fb57c9a4eb08ed7a070855097a2fa00a154cbf81cd9fef262681ff084a1e90ff419e83a
6
+ metadata.gz: 0231e98d42cb869ebda208e0ea204ee012537eb1ad47e745bd34b7ad1990213262f204a2e26f1d53e09fc0e6b43891bdd7d083f133584461ce657c51e55113de
7
+ data.tar.gz: 20164fdc20c09cf57513d8073ec2ec0ce076c5c960bd8b9460bf67fa733d67b1ae477adfa971cb8365032474496e069b5d8faf9d5017265a813094887bd92d2e
data/Rakefile CHANGED
@@ -9,9 +9,12 @@ require "simple/ansi"
9
9
  require "pathutil"
10
10
  require "json"
11
11
 
12
- task :default => [:spec]
13
- RSpec::Core::RakeTask.new :spec
12
+ task :default => [
13
+ ENV["BENCHMARK"] ? :benchmark : :spec
14
+ ]
15
+
14
16
  BenchmarkTask.new :benchmark
17
+ RSpec::Core::RakeTask.new :spec
15
18
  task :test => :spec
16
19
 
17
20
  # ----------------------------------------------------------------------------
@@ -4,19 +4,23 @@
4
4
  # Encoding: utf-8
5
5
  # ----------------------------------------------------------------------------
6
6
 
7
+ require "pathutil/helpers"
7
8
  require "forwardable/extended"
8
9
  require "find"
9
10
 
10
11
  #
11
12
 
12
13
  class Pathutil
13
- extend Forwardable::Extended
14
14
  attr_writer :encoding
15
+ extend Forwardable::Extended
16
+ extend Helpers
15
17
 
16
18
  # --------------------------------------------------------------------------
17
19
 
18
20
  def initialize(path)
19
- @path = (path.respond_to?(:to_path) ? path.to_path : path.to_s).dup
21
+ return @path = path if path.is_a?(String)
22
+ return @path = path.to_path if path.respond_to?(:to_path)
23
+ return @path = path.to_s
20
24
  end
21
25
 
22
26
  # --------------------------------------------------------------------------
@@ -44,7 +48,7 @@ class Pathutil
44
48
  file = self.class.new(file)
45
49
  if yield(file)
46
50
  ary.push(
47
- self
51
+ file
48
52
  )
49
53
  end
50
54
 
@@ -62,29 +66,11 @@ class Pathutil
62
66
 
63
67
  # --------------------------------------------------------------------------
64
68
 
65
- def read_yaml(safe: true, whitelist_classes: [], whitelist_symbols: [], throw_missing: false, aliases: true)
66
- require "yaml"
67
-
68
- unless safe
69
- return YAML.load(
70
- read
71
- )
72
- end
73
-
74
- if !YAML.respond_to?(:safe_load)
75
- setup_safe_yaml whitelist_classes
76
- SafeYAML.load(
77
- read
78
- )
69
+ def read_yaml(throw_missing: false, **kwd)
70
+ self.class.load_yaml(
71
+ read, **kwd
72
+ )
79
73
 
80
- else
81
- YAML.safe_load(
82
- read,
83
- whitelist_classes,
84
- whitelist_symbols,
85
- aliases
86
- )
87
- end
88
74
  rescue Errno::ENOENT
89
75
  throw_missing ? raise : (
90
76
  return {}
@@ -429,7 +415,8 @@ class Pathutil
429
415
  # --------------------------------------------------------------------------
430
416
 
431
417
  def parent
432
- self.class.new(File.join(
418
+ return self if @path == "/"
419
+ self.class.new(absolute?? File.dirname(@path) : File.join(
433
420
  @path, ".."
434
421
  ))
435
422
  end
@@ -450,9 +437,7 @@ class Pathutil
450
437
  # --------------------------------------------------------------------------
451
438
 
452
439
  def sub_ext(ext)
453
- self.class.new(
454
- "#{@path.gsub(/\..+$/, "")}#{ext}"
455
- )
440
+ self.class.new(@path.chomp(File.extname(@path)) + ext)
456
441
  end
457
442
 
458
443
  # --------------------------------------------------------------------------
@@ -666,21 +651,21 @@ class Pathutil
666
651
 
667
652
  # --------------------------------------------------------------------------
668
653
 
669
- private
670
- def setup_safe_yaml(whitelist_classes)
671
- warn "WARN: SafeYAML will be removed when Ruby 2.0 goes EOL."
672
- require "safe_yaml/load"
654
+ class << self
655
+ attr_writer :encoding
673
656
 
674
- SafeYAML.restore_defaults!
675
- whitelist_classes.map(&SafeYAML.method(
676
- :whitelist_class!
677
- ))
678
- end
657
+ # ------------------------------------------------------------------------
658
+ # Get the current directory that Ruby knows about.
659
+ # ------------------------------------------------------------------------
679
660
 
680
- # --------------------------------------------------------------------------
661
+ def pwd
662
+ new(
663
+ Dir.pwd
664
+ )
665
+ end
681
666
 
682
- class << self
683
- attr_writer :encoding
667
+ alias gcwd pwd
668
+ alias cwd pwd
684
669
 
685
670
  # ------------------------------------------------------------------------
686
671
  # Aliases the default system encoding to us so that we can do most read
@@ -708,15 +693,24 @@ class Pathutil
708
693
 
709
694
  # ------------------------------------------------------------------------
710
695
 
711
- def make_tmpname(prefix = "", suffix = nil)
712
- prefix = prefix.gsub(/\-\Z/, "") + "-" unless prefix.empty?
696
+ def tmpdir(*args)
697
+ rtn = new(make_tmpname(*args)).tap(&:mkdir)
698
+ ObjectSpace.define_finalizer(rtn, proc do
699
+ rtn.rm_rf
700
+ end)
713
701
 
714
- File.join(
715
- Dir::Tmpname.tmpdir,
716
- Dir::Tmpname.make_tmpname(
717
- prefix, suffix
718
- )
719
- )
702
+ rtn
703
+ end
704
+
705
+ # ------------------------------------------------------------------------
706
+
707
+ def tmpfile(*args)
708
+ rtn = new(make_tmpname(*args)).tap(&:touch)
709
+ ObjectSpace.define_finalizer(rtn, proc do
710
+ rtn.rm_rf
711
+ end)
712
+
713
+ rtn
720
714
  end
721
715
  end
722
716
 
@@ -0,0 +1,64 @@
1
+ class Pathutil
2
+ module Helpers
3
+ extend self
4
+
5
+ # ------------------------------------------------------------------------
6
+ # Wraps around YAML and SafeYAML to provide alternatives to Rubies.
7
+ # @note We default aliases to yes so we can detect if you explicit true.
8
+ # ------------------------------------------------------------------------
9
+
10
+ def load_yaml(data, safe: true, whitelist_classes: [], whitelist_symbols: [], aliases: :yes)
11
+ require "yaml"
12
+
13
+ unless safe
14
+ return YAML.load(
15
+ data
16
+ )
17
+ end
18
+
19
+ if !YAML.respond_to?(:safe_load)
20
+ setup_safe_yaml whitelist_classes, aliases
21
+ SafeYAML.load(
22
+ data
23
+ )
24
+
25
+ else
26
+ YAML.safe_load(
27
+ data,
28
+ whitelist_classes,
29
+ whitelist_symbols,
30
+ aliases
31
+ )
32
+ end
33
+ end
34
+
35
+ # ------------------------------------------------------------------------
36
+
37
+ def make_tmpname(prefix = "", suffix = nil)
38
+ prefix = prefix.gsub(/\-\Z/, "") + "-" unless prefix.empty?
39
+
40
+ File.join(
41
+ Dir::Tmpname.tmpdir,
42
+ Dir::Tmpname.make_tmpname(
43
+ prefix, suffix
44
+ )
45
+ )
46
+ end
47
+
48
+ # ------------------------------------------------------------------------
49
+ # rubocop:disable Metrics/LineLength
50
+ # ------------------------------------------------------------------------
51
+
52
+ private
53
+ def setup_safe_yaml(whitelist_classes, aliases)
54
+ warn "#{self.class.name}:WARN: SafeYAML does not support disabling of aliases." if aliases && aliases != :yes
55
+ warn "#{self.class.name}:WARN: SafeYAML will be removed when Ruby 2.0 goes EOL."
56
+ require "safe_yaml/load"
57
+
58
+ SafeYAML.restore_defaults!
59
+ whitelist_classes.map(&SafeYAML.method(
60
+ :whitelist_class!
61
+ ))
62
+ end
63
+ end
64
+ end
@@ -5,5 +5,5 @@
5
5
  # ----------------------------------------------------------------------------
6
6
 
7
7
  class Pathutil
8
- VERSION = "0.2.0"
8
+ VERSION = "0.3.0"
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pathutil
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordon Bedwell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-22 00:00:00.000000000 Z
11
+ date: 2016-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: forwardable-extended
@@ -35,6 +35,7 @@ files:
35
35
  - LICENSE
36
36
  - Rakefile
37
37
  - lib/pathutil.rb
38
+ - lib/pathutil/helpers.rb
38
39
  - lib/pathutil/version.rb
39
40
  homepage: http://github.com/envygeeks/pathutils
40
41
  licenses: