build-files 1.0.4 → 1.0.5

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: 5a59583c2fd0e5a52a7049d0e709ae3e50cfeb4d
4
- data.tar.gz: c59a6c774e10e2cf53765ee02dda6f79394792d9
3
+ metadata.gz: dc9ede33f6827de41ac819213833225aee230195
4
+ data.tar.gz: 433c52df04cd60a80317f87a923089a246f9965e
5
5
  SHA512:
6
- metadata.gz: 0b8f1df86d30a44caa6c4192bc5443a660e2e9b98e81341690c675b0f6eab0ab75d26ae94ee90e5184b3c5fb6121b499a841973293c78dee54eca745be70890b
7
- data.tar.gz: b06d12304725fd2327374cc27cc26dc1d390b2561400ef1fe049438dd35928d91a9c33a3e142410a56ecd689ecaa18dc1dfc26213a4b39e7d91a538640536b18
6
+ metadata.gz: d024fae752d2ec7a58748170f9c5f79c3af8edbd9d4b631360dcda2bc1b401a7fdf59c96a1f0813bdea7e34cd9508a8c092446926535857c227c219cf1af53a3
7
+ data.tar.gz: 4bc8f6ad273d381bc42efddd03cf1a4db18b6b4d170ba50d66e11d4c1e3c02ee76aaf806bdba86e01fcf5fe35b1155fc5f14361410fc0670fb270e08f278f650
@@ -22,6 +22,14 @@ module Build
22
22
  module Files
23
23
  # Represents a file path with an absolute root and a relative offset:
24
24
  class Path
25
+ def self.split(path)
26
+ # Effectively dirname and basename:
27
+ dirname, separator, filename = path.rpartition(File::SEPARATOR)
28
+ filename, dot, extension = filename.rpartition('.')
29
+
30
+ return dirname + separator, filename, dot + extension
31
+ end
32
+
25
33
  # Returns the length of the prefix which is shared by two strings.
26
34
  def self.prefix_length(a, b)
27
35
  [a.size, b.size].min.times{|i| return i if a[i] != b[i]}
@@ -128,9 +136,21 @@ module Build
128
136
  self.class.new(File.join(root, relative_path), root)
129
137
  end
130
138
 
131
- def with(root: @root, extension: nil)
132
- # self.relative_path should be a string so using + to add an extension should be fine.
133
- relative_path = extension ? self.relative_path + extension : self.relative_path
139
+ def with(root: @root, extension: nil, basename: false)
140
+ relative_path = self.relative_path
141
+
142
+ if basename
143
+ dirname, filename, _ = self.class.split(relative_path)
144
+
145
+ # Replace the filename if the basename is supplied:
146
+ filename = basename if basename.is_a? String
147
+
148
+ relative_path = dirname + filename
149
+ end
150
+
151
+ if extension
152
+ relative_path = relative_path + extension
153
+ end
134
154
 
135
155
  self.class.new(File.join(root, relative_path), root, relative_path)
136
156
  end
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Build
22
22
  module Files
23
- VERSION = "1.0.4"
23
+ VERSION = "1.0.5"
24
24
  end
25
25
  end
@@ -42,6 +42,20 @@ module Build::Files::PathSpec
42
42
  end
43
43
  end
44
44
 
45
+ describe Build::Files::Path.new("/foo/bar.txt") do
46
+ it "should replace existing file extension" do
47
+ expect(subject.with(extension: '.jpeg', basename: true)).to be == "/foo/bar.jpeg"
48
+ end
49
+
50
+ it "should append file extension" do
51
+ expect(subject.with(extension: '.jpeg')).to be == "/foo/bar.txt.jpeg"
52
+ end
53
+
54
+ it "should change basename" do
55
+ expect(subject.with(basename: 'baz', extension: '.txt')).to be == "/foo/baz.txt"
56
+ end
57
+ end
58
+
45
59
  describe Build::Files::Path.new("/foo/bar/baz", "/foo") do
46
60
  it "should be inspectable" do
47
61
  expect(subject.inspect).to be_include subject.root.to_s
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: build-files
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-15 00:00:00.000000000 Z
11
+ date: 2016-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler