Module: Middleman::Util

Defined in:
lib/middleman-core/util.rb

Defined Under Namespace

Classes: Cache

Class Method Summary collapse

Class Method Details

.all_files_under(*paths) ⇒ Array

Get a recusive list of files inside a set of paths. Works with symlinks.

Parameters:

  • path

    A path string or Pathname

Returns:

  • (Array)

    An array of filenames



107
108
109
110
111
112
113
114
115
# File 'lib/middleman-core/util.rb', line 107

def self.all_files_under(*paths)
  paths.flatten!
  paths.map! { |p| Pathname(p) }
  files = paths.select { |p| p.file? }
  paths.select {|p| p.directory? }.each do |dir|
    files << all_files_under(dir.children)
  end
  files.flatten
end

.extract_response_text(response) ⇒ String

Extract the text of a Rack response as a string. Useful for extensions implemented as Rack middleware.

Parameters:

  • response

    The response from #call

Returns:

  • (String)

    The whole response as a string.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/middleman-core/util.rb', line 68

def self.extract_response_text(response)
  case(response)
  when String
    response
  when Array
    response.join
  when Rack::Response
    response.body.join
  when Rack::File
    File.read(response.path)
  else
    response.to_s
  end
end

.instrument(name, payload = {}, &block) ⇒ Object

Facade for ActiveSupport/Notification



29
30
31
32
# File 'lib/middleman-core/util.rb', line 29

def self.instrument(name, payload={}, &block)
  name << ".middleman" unless name =~ /\.middleman$/
  ::ActiveSupport::Notifications.instrument(name, payload, &block)
end

.logger(*args) ⇒ Middleman::Logger

The logger

Returns:



20
21
22
23
24
25
26
# File 'lib/middleman-core/util.rb', line 20

def self.logger(*args)
  if !@_logger || args.length > 0
    @_logger = ::Middleman::Logger.new(*args)
  end

  @_logger
end

.normalize_path(path) ⇒ String

Normalize a path to not include a leading slash

Parameters:

  • path (String)

Returns:

  • (String)


59
60
61
62
# File 'lib/middleman-core/util.rb', line 59

def self.normalize_path(path)
  # The tr call works around a bug in Ruby's Unicode handling
  path.sub(/^\//, "").tr('','')
end

.path_match(matcher, path) ⇒ Boolean

Takes a matcher, which can be a literal string or a string containing glob expressions, or a regexp, or a proc, or anything else that responds to #match or #call, and returns whether or not the given path matches that matcher.

Parameters:

  • matcher

    A matcher string/regexp/proc/etc

  • path

    A path as a string

Returns:

  • (Boolean)

    Whether the path matches the matcher



92
93
94
95
96
97
98
99
100
# File 'lib/middleman-core/util.rb', line 92

def self.path_match(matcher, path)
  if matcher.respond_to? :match
    matcher.match path
  elsif matcher.respond_to? :call
    matcher.call path
  else
    File.fnmatch(matcher.to_s, path)
  end
end

.recursively_enhance(data) ⇒ Thor::CoreExt::HashWithIndifferentAccess

Recursively convert a normal Hash into a HashWithIndifferentAccess

Parameters:

  • data (Hash)

    Normal hash

Returns:

  • (Thor::CoreExt::HashWithIndifferentAccess)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/middleman-core/util.rb', line 39

def self.recursively_enhance(data)
  if data.is_a? Hash
    data = ::Thor::CoreExt::HashWithIndifferentAccess.new(data)
    data.each do |key, val|
      data[key] = recursively_enhance(val)
    end
    data
  elsif data.is_a? Array
    data.each_with_index do |val, i|
      data[i] = recursively_enhance(val)
    end
    data
  else
    data
  end
end