Module: Rack::App::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/rack/app/utils.rb

Instance Method Summary collapse

Instance Method Details

#camel_case(snake_case) ⇒ Object



38
39
40
# File 'lib/rack/app/utils.rb', line 38

def camel_case(snake_case)
  snake_case.to_s.split('_').collect(&:capitalize).join
end

#deep_dup(object) ⇒ Object



89
90
91
# File 'lib/rack/app/utils.rb', line 89

def deep_dup(object)
  ::Rack::App::Utils::DeepDup.duplicate(object)
end

#deep_merge(hash, oth_hash) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rack/app/utils.rb', line 93

def deep_merge(hash,oth_hash)
  oth_hash.each_pair do |current_key, other_value|

    this_value = hash[current_key]

    hash[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash)
                          deep_merge(this_value, other_value)
                        else
                          other_value
                        end
  end

  hash
end

#deprecate(object, name, repl, year, month) ⇒ Object

Simple deprecation method that deprecates name by wrapping it up in a dummy method. It warns on each call to the dummy method telling the user of repl (unless repl is :none) and the year/month that it is planned to go away.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rack/app/utils.rb', line 141

def deprecate(object, name, repl, year, month)
  object.class_eval {
    old = "_deprecated_#{name}"
    alias_method old, name
    define_method name do |*args, &block|
      klass = self.kind_of? Module
      target = klass ? "#{self}." : "#{self.class}#"
      msg = [ "NOTE: #{target}#{name} is deprecated",
        repl == :none ? " with no replacement" : "; use #{repl} instead",
        ". It will be removed on or after %4d-%02d-01." % [year, month],
        "\n#{target}#{name} called from #{Gem.location_of_caller.join(":")}",
      ]
      warn "#{msg.join}."
      __send__(old, *args, &block)
    end
  }
end

#devnull_pathObject



108
109
110
# File 'lib/rack/app/utils.rb', line 108

def devnull_path
  RUBY_PLATFORM =~ /mswin|mingw/ ? 'NUL:' : '/dev/null'
end

#encode_www_form(enum) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rack/app/utils.rb', line 112

def encode_www_form(enum)
  enum.map do |k, v|
    if v.nil?
      encode_www_form_component(k)
    elsif v.respond_to?(:to_ary)
      v.to_ary.map do |w|
        str = encode_www_form_component(k)
        unless w.nil?
          str << '='
          str << encode_www_form_component(w)
        end
      end.join('&')
    else
      str = encode_www_form_component(k)
      str << '='
      str << encode_www_form_component(v)
    end
  end.join('&')
end

#encode_www_form_component(str) ⇒ Object



132
133
134
# File 'lib/rack/app/utils.rb', line 132

def encode_www_form_component(str)
  CGI.escape(str.to_s)
end

#expand_path(file_path) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rack/app/utils.rb', line 66

def expand_path(file_path)
  case file_path

    when /^\.\//
      File.expand_path(File.join(File.dirname(caller[1]), file_path))

    when /^[^\/]/
      File.join(namespace_folder(caller[1]), file_path)

    when /^\//
      from_project_root_path = pwd(file_path)
      File.exist?(from_project_root_path) ? from_project_root_path : file_path

  end
end

#join(*url_path_parts) ⇒ Object



61
62
63
64
# File 'lib/rack/app/utils.rb', line 61

def join(*url_path_parts)
  url_path_parts = [url_path_parts].flatten.compact.map(&:to_s)
  File.join(*url_path_parts).tr(File::Separator, '/').sub(/^\/?(.*)$/, '/\1')
end

#namespace_folder(file_path_info) ⇒ Object



82
83
84
85
86
# File 'lib/rack/app/utils.rb', line 82

def namespace_folder(file_path_info)
  basename = File.basename(file_path_info).split('.')[0]
  directory = File.dirname(file_path_info)
  File.join(directory,basename)
end

#normalize_path(path) ⇒ Object

Normalizes URI path.

Strips off trailing slash and ensures there is a leading slash.

normalize_path("/foo")  # => "/foo"
normalize_path("/foo/") # => "/foo"
normalize_path("foo")   # => "/foo"
normalize_path("")      # => "/"


17
18
19
20
21
22
23
# File 'lib/rack/app/utils.rb', line 17

def normalize_path(path)
  path = "/#{path}"
  path.squeeze!('/')
  path.sub!(%r{/+\Z}, '')
  path = '/' if path == ''
  path
end

#pwd(*path_parts) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rack/app/utils.rb', line 42

def pwd(*path_parts)

  root_folder = if ENV['BUNDLE_GEMFILE']
                  ENV['BUNDLE_GEMFILE'].split(File::Separator)[0..-2].join(File::Separator)
                else
                  Dir.pwd.to_s
                end

  return File.join(root_folder, *path_parts)

end

#snake_case(camel_cased_word) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/rack/app/utils.rb', line 29

def snake_case(camel_cased_word)
  word = camel_cased_word.to_s.gsub('::', '/')
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end

#split_path_info(path_info) ⇒ Object



25
26
27
# File 'lib/rack/app/utils.rb', line 25

def split_path_info(path_info)
  path_info.to_s.split('/').tap { |a| a.empty? && a.push('') }
end

#uuidObject



54
55
56
57
58
59
# File 'lib/rack/app/utils.rb', line 54

def uuid
  ary = SecureRandom.random_bytes(16).unpack("NnnnnN")
  ary[2] = (ary[2] & 0x0fff) | 0x4000
  ary[3] = (ary[3] & 0x3fff) | 0x8000
  "%08x-%04x-%04x-%04x-%04x%08x" % ary
end