Module: Borrower::DSL

Defined in:
lib/borrower.rb

Instance Method Summary collapse

Instance Method Details

#borrow(path, options = {}, &block) ⇒ Void

borrow a file and put it somewhere by adding a merge: argument of true borrower will parse the contents for additional borrow statements to merge

Examples:

borrow source to destination

borrow "source/file", to: "destination/file"

borrow and merge source to destination

borrow "source/file", to: "file/destination", merge: true

borrow and gsub all exclamation marks

borrow "source/file", to: "file/destination" do |content|
  content.gsub("!", '.')
end

Parameters:

  • path (String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :to (String)

    the destination path

  • :merge (Boolean)

    wether to merge or not, defaults to ‘false`

Returns:

  • (Void)


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/borrower.rb', line 37

def borrow path, options={}, &block
  destination = options.delete(:to) { raise ArgumentError, "missing 'to:' argument" }
  on_conflict = options.delete(:on_conflict) { nil }
  content = Borrower.take(path)

  if content.valid_encoding?
    content = Borrower.merge(content, options) if options.fetch(:merge) { false }
    content = yield content if block_given?
  end

  Borrower.put *[content, destination, on_conflict].compact
end