borrower 0.1.0 → 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.
@@ -1,29 +1,4 @@
1
1
  module Borrower
2
-
3
- class << self
4
- def manifest &block
5
- @_manifest ||= Manifest.new
6
- yield @_manifest if block_given?
7
- end
8
-
9
- def find file
10
- path = @_manifest.files.fetch(file) { false }
11
- return path if path
12
-
13
- matches = []
14
- @_manifest.directories.each do |dir|
15
- Dir[File.join( dir, '*' )].each do |possibility|
16
- matches << possibility if possibility.match( file )
17
- end
18
- end
19
-
20
- path = matches.sort { |a,b| a.length <=> b.length }.first
21
- return path if path
22
-
23
- raise "Could not file #{file}"
24
- end
25
- end
26
-
27
2
  class Manifest
28
3
 
29
4
  attr_reader :files
@@ -42,5 +17,33 @@ module Borrower
42
17
  @directories.push dir
43
18
  end
44
19
 
20
+ def find file
21
+ return file if Path.remote?(file) || Path.exists?(file)
22
+
23
+ path = check_for_file_in_manifest_files(file) || false
24
+ return path if path
25
+
26
+ path = check_for_file_in_manifest_directories(file) || false
27
+ return path if path
28
+
29
+ raise "Could not file #{file}"
30
+ end
31
+
32
+ private
33
+
34
+ def check_for_file_in_manifest_files file
35
+ files.fetch(file) { false }
36
+ end
37
+
38
+ def check_for_file_in_manifest_directories file
39
+ matches = []
40
+ directories.each do |dir|
41
+ Dir[File.join( dir, '*' )].each do |possibility|
42
+ matches << possibility if possibility.match( file )
43
+ end
44
+ end
45
+ path = matches.sort { |a,b| a.length <=> b.length }.first
46
+ end
47
+
45
48
  end
46
49
  end
@@ -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
- ::Borrower::Path.contents(::Borrower.find(path))
26
+ Path.contents(path)
32
27
  end
33
28
 
34
29
  end
data/lib/borrower/path.rb CHANGED
@@ -6,15 +6,15 @@ module Borrower
6
6
  # @param path [String]
7
7
  # @return [String] the contents
8
8
  def contents path
9
- if exists? path
10
- if remote? path
11
- fetch_from_remote(path).body
12
- else
13
- return IO.read(path)
14
- end
9
+ path = Borrower.find(path)
10
+
11
+ if remote?(path) && exists?(path)
12
+ return fetch_from_remote(path).body
15
13
  else
16
- raise "nothing exists at the provided path '#{path}'"
14
+ return IO.read(path)
17
15
  end
16
+
17
+ raise "nothing exists at the provided path '#{path}'"
18
18
  end
19
19
 
20
20
  # check if the file exists for borrowing
@@ -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
@@ -2,8 +2,9 @@ module Borrower
2
2
  module Transport
3
3
 
4
4
  # routes a file from -> to
5
- # @param from [String]
6
- # @param args [Hash]
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 from [String]
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 content [String]
22
- # @param to [String]
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|
@@ -1,3 +1,3 @@
1
1
  module Borrower
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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
- # call syntax:
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
- # Borrower.borrow "path/to/file", to: "place/to/put/file"
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
@@ -6,6 +6,7 @@ describe Borrower::Manifest do
6
6
  given_files [ "baz.txt", "files/baz.txt", "files/woo.txt", "files/foo.txt" ]
7
7
  Borrower.manifest do |m|
8
8
  m.file "me", File.join( TMP, "test.txt" )
9
+ m.file "remote", "https://gist.github.com/stevenosloan/5578606/raw/97ab1305184bdeac33472f9f1fcc1c9e278a1bb3/dummy.txt"
9
10
  m.dir File.join( TMP )
10
11
  m.dir File.join( TMP, 'files' )
11
12
  end
@@ -19,6 +20,14 @@ describe Borrower::Manifest do
19
20
  Borrower.find("me").should == File.join( TMP, "test.txt" )
20
21
  end
21
22
 
23
+ it "returns remote paths if given fully resolved" do
24
+ Borrower.find("http://google.com").should == "http://google.com"
25
+ end
26
+
27
+ it "finds named remote files" do
28
+ Borrower.find("remote").should == "https://gist.github.com/stevenosloan/5578606/raw/97ab1305184bdeac33472f9f1fcc1c9e278a1bb3/dummy.txt"
29
+ end
30
+
22
31
  it "creates list of files from directories" do
23
32
  Borrower.find("baz.txt").should == File.join( TMP, "baz.txt" )
24
33
  Borrower.find("files/baz.txt").should == File.join( TMP, "files/baz.txt" )
@@ -18,17 +18,29 @@ describe Borrower do
18
18
 
19
19
  it "borrows local files" do
20
20
  borrow local_file, to: file_destination
21
- Borrower::Transport.take( file_destination ).should == "Hi I'm a file"
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::Transport.take( file_destination ).should == "Hello I'm a file"
26
+ Borrower.take( file_destination ).should == "Hello I'm a file"
27
+ end
28
+
29
+ it "borrows named local and remote files" do
30
+ Borrower.manifest do |m|
31
+ m.file "local", local_file
32
+ m.file "remote", remote_file
33
+ end
34
+ borrow "local", to: file_destination
35
+ borrow "remote", to: File.join( TMP, "remote.txt" )
36
+
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"
27
39
  end
28
40
 
29
41
  it "merges files if merge is set to true" do
30
42
  borrow merge_file, to: file_destination, merge: true
31
- Borrower::Transport.take( file_destination ).should == "Hi I'm a file"
43
+ Borrower.take( file_destination ).should == "Hi I'm a file"
32
44
  end
33
45
 
34
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.1.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-07 00:00:00.000000000 Z
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