borrower 0.1.1 → 0.2.0
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/lib/borrower/manifest.rb +17 -25
- data/lib/borrower/merge.rb +2 -7
- data/lib/borrower/public_api.rb +64 -0
- data/lib/borrower/transport.rb +6 -5
- data/lib/borrower/version.rb +1 -1
- data/lib/borrower.rb +13 -5
- data/spec/borrower_spec.rb +5 -5
- metadata +3 -2
data/lib/borrower/manifest.rb
CHANGED
@@ -1,9 +1,20 @@
|
|
1
1
|
module Borrower
|
2
|
+
class Manifest
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
attr_reader :files
|
5
|
+
attr_reader :directories
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@files = {}
|
9
|
+
@directories = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def file name, path
|
13
|
+
@files[name] = path
|
14
|
+
end
|
15
|
+
|
16
|
+
def dir dir
|
17
|
+
@directories.push dir
|
7
18
|
end
|
8
19
|
|
9
20
|
def find file
|
@@ -21,37 +32,18 @@ module Borrower
|
|
21
32
|
private
|
22
33
|
|
23
34
|
def check_for_file_in_manifest_files file
|
24
|
-
|
35
|
+
files.fetch(file) { false }
|
25
36
|
end
|
26
37
|
|
27
38
|
def check_for_file_in_manifest_directories file
|
28
39
|
matches = []
|
29
|
-
|
40
|
+
directories.each do |dir|
|
30
41
|
Dir[File.join( dir, '*' )].each do |possibility|
|
31
42
|
matches << possibility if possibility.match( file )
|
32
43
|
end
|
33
44
|
end
|
34
45
|
path = matches.sort { |a,b| a.length <=> b.length }.first
|
35
46
|
end
|
36
|
-
end
|
37
|
-
|
38
|
-
class Manifest
|
39
|
-
|
40
|
-
attr_reader :files
|
41
|
-
attr_reader :directories
|
42
|
-
|
43
|
-
def initialize
|
44
|
-
@files = {}
|
45
|
-
@directories = []
|
46
|
-
end
|
47
|
-
|
48
|
-
def file name, path
|
49
|
-
@files[name] = path
|
50
|
-
end
|
51
|
-
|
52
|
-
def dir dir
|
53
|
-
@directories.push dir
|
54
|
-
end
|
55
47
|
|
56
48
|
end
|
57
49
|
end
|
data/lib/borrower/merge.rb
CHANGED
@@ -1,11 +1,6 @@
|
|
1
1
|
module Borrower
|
2
|
-
class << self
|
3
|
-
def merge content, options={}
|
4
|
-
Merge.new( content, options ).output
|
5
|
-
end
|
6
|
-
end
|
7
|
-
|
8
2
|
class Merge
|
3
|
+
|
9
4
|
def initialize content, options={}
|
10
5
|
@content = content
|
11
6
|
@comment_symbol = options.fetch(:comment) { default_comment_symbol }
|
@@ -28,7 +23,7 @@ module Borrower
|
|
28
23
|
end
|
29
24
|
|
30
25
|
def contents_from_file path
|
31
|
-
|
26
|
+
Path.contents(path)
|
32
27
|
end
|
33
28
|
|
34
29
|
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# the public api methods for borrower
|
2
|
+
module Borrower
|
3
|
+
module PublicAPI
|
4
|
+
|
5
|
+
# retrieve the contents of a file
|
6
|
+
# that is remote or local
|
7
|
+
#
|
8
|
+
# @param [String] from the path to the file
|
9
|
+
# @return [String] contents of the file
|
10
|
+
def take from
|
11
|
+
Transport.take from
|
12
|
+
end
|
13
|
+
|
14
|
+
# write the content to a destination file
|
15
|
+
#
|
16
|
+
# @param [String] content content for the file
|
17
|
+
# @param [String] to path to write contents to
|
18
|
+
# @return [Void]
|
19
|
+
def put content, to
|
20
|
+
Transport.put content, to
|
21
|
+
end
|
22
|
+
|
23
|
+
# parse through the content and merge
|
24
|
+
# matched file contents
|
25
|
+
#
|
26
|
+
# @param [String] content content to parse
|
27
|
+
# @param [Hash] options merging options
|
28
|
+
# @option options [String] :comment the comment delimeter to match against
|
29
|
+
# @return [String] the merged content
|
30
|
+
def merge content, options={}
|
31
|
+
Merge.new( content, options ).output
|
32
|
+
end
|
33
|
+
|
34
|
+
# returns the manifest to add lookup
|
35
|
+
# directories or named files
|
36
|
+
# @example add a file alias and directory
|
37
|
+
#
|
38
|
+
# manifest do |m|
|
39
|
+
# m.file "jquery", "http://code.jquery.com/jquery-1.10.2.min.js"
|
40
|
+
# # => adds an alias for jquery
|
41
|
+
# m.dir "vendor"
|
42
|
+
# # => adds the vendor dir to the lookup paths
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# @yield [Manifest]
|
46
|
+
# @return [Manifest]
|
47
|
+
def manifest &block
|
48
|
+
@_manifest ||= Manifest.new
|
49
|
+
yield @_manifest if block_given?
|
50
|
+
return @_manifest
|
51
|
+
end
|
52
|
+
|
53
|
+
# lookup a file or path in the manifest
|
54
|
+
#
|
55
|
+
# @param [String] file
|
56
|
+
# @return [String] path to the file
|
57
|
+
def find file
|
58
|
+
manifest.find file
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
extend PublicAPI
|
64
|
+
end
|
data/lib/borrower/transport.rb
CHANGED
@@ -2,8 +2,9 @@ module Borrower
|
|
2
2
|
module Transport
|
3
3
|
|
4
4
|
# routes a file from -> to
|
5
|
-
# @param
|
6
|
-
# @param
|
5
|
+
# @param [String] from
|
6
|
+
# @param [String] to
|
7
|
+
# @param [Hash] args
|
7
8
|
def move from, to, args={}
|
8
9
|
content = take(from)
|
9
10
|
content = Borrower.merge(content) if args[:merge]
|
@@ -11,15 +12,15 @@ module Borrower
|
|
11
12
|
end
|
12
13
|
|
13
14
|
# handles taking a file
|
14
|
-
# @param
|
15
|
+
# @param [String] from
|
15
16
|
# @return [String]
|
16
17
|
def take from
|
17
18
|
Path.contents(from)
|
18
19
|
end
|
19
20
|
|
20
21
|
# puts a file somewhere
|
21
|
-
# @param
|
22
|
-
# @param
|
22
|
+
# @param [String] content
|
23
|
+
# @param [String] to
|
23
24
|
def put content, to
|
24
25
|
FileUtils.mkdir_p( File.dirname(to) )
|
25
26
|
File.open( to, 'w', internal_encoding: content.encoding ) do |file|
|
data/lib/borrower/version.rb
CHANGED
data/lib/borrower.rb
CHANGED
@@ -10,17 +10,25 @@ require 'borrower/manifest'
|
|
10
10
|
require 'borrower/merge'
|
11
11
|
require 'borrower/path'
|
12
12
|
require 'borrower/transport'
|
13
|
+
require 'borrower/public_api'
|
13
14
|
|
14
15
|
module Borrower::DSL
|
15
16
|
|
16
17
|
# borrow a file and put it somewhere
|
17
|
-
#
|
18
|
+
# by adding a merge: argument of true borrower will
|
19
|
+
# parse the contents for additional borrow statements
|
20
|
+
# to merge
|
21
|
+
# @example borrow source to destination
|
22
|
+
# borrow "source/file", to: "destination/file"
|
18
23
|
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
# @param path [String]
|
22
|
-
# @param args [Hash] only to: is looked at
|
24
|
+
# @example borrow and merge source to destination
|
25
|
+
# borrow "source/file", to: "file/destination", merge: true
|
23
26
|
#
|
27
|
+
# @param [String] path
|
28
|
+
# @param [Hash] args
|
29
|
+
# @option args [String] :to the destination path
|
30
|
+
# @option args [Boolean] :merge wether to merge or not, defaults to `false`
|
31
|
+
# @return [Void]
|
24
32
|
def borrow path, args
|
25
33
|
destination = args.delete(:to) { raise ArgumentError, "missing 'to:' argument" }
|
26
34
|
Borrower::Transport.move path, destination, args
|
data/spec/borrower_spec.rb
CHANGED
@@ -18,12 +18,12 @@ describe Borrower do
|
|
18
18
|
|
19
19
|
it "borrows local files" do
|
20
20
|
borrow local_file, to: file_destination
|
21
|
-
Borrower
|
21
|
+
Borrower.take( file_destination ).should == "Hi I'm a file"
|
22
22
|
end
|
23
23
|
|
24
24
|
it "borrows remote files" do
|
25
25
|
borrow remote_file, to: file_destination
|
26
|
-
Borrower
|
26
|
+
Borrower.take( file_destination ).should == "Hello I'm a file"
|
27
27
|
end
|
28
28
|
|
29
29
|
it "borrows named local and remote files" do
|
@@ -34,13 +34,13 @@ describe Borrower do
|
|
34
34
|
borrow "local", to: file_destination
|
35
35
|
borrow "remote", to: File.join( TMP, "remote.txt" )
|
36
36
|
|
37
|
-
Borrower
|
38
|
-
Borrower
|
37
|
+
Borrower.take( file_destination ).should == "Hi I'm a file"
|
38
|
+
Borrower.take( File.join( TMP, "remote.txt" ) ).should == "Hello I'm a file"
|
39
39
|
end
|
40
40
|
|
41
41
|
it "merges files if merge is set to true" do
|
42
42
|
borrow merge_file, to: file_destination, merge: true
|
43
|
-
Borrower
|
43
|
+
Borrower.take( file_destination ).should == "Hi I'm a file"
|
44
44
|
end
|
45
45
|
|
46
46
|
it "raises an error if missing arg with key 'to'" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: borrower
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
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: 2013-07-
|
12
|
+
date: 2013-07-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: net-ssh
|
@@ -38,6 +38,7 @@ files:
|
|
38
38
|
- lib/borrower/manifest.rb
|
39
39
|
- lib/borrower/merge.rb
|
40
40
|
- lib/borrower/path.rb
|
41
|
+
- lib/borrower/public_api.rb
|
41
42
|
- lib/borrower/transport.rb
|
42
43
|
- lib/borrower/version.rb
|
43
44
|
- lib/borrower.rb
|