module ReqTools

Public Instance Methods

method_missing(m, *args, &block) click to toggle source
Calls superclass method
# File lib/erb_component/req_tools.rb, line 38
def method_missing(m, *args, &block)
  return super unless m.to_s[0].upcase == m.to_s[0]
  m = m.to_s
  str = Kernel.const_defined?("#{self.class}::#{m}") ? "#{self.class}::#{m}" : m
  clazz = Kernel.const_get(str)
  if args.size > 0
    component = clazz.new(req, *args)
  else
    component = clazz.new(req)
  end
  component.render
end
params() click to toggle source
# File lib/erb_component/req_tools.rb, line 25
def params
  @params ||= begin
    res = @req.params
    res.merge!(JSON.parse(req.body.read)) if req.post? || req.put? || req.patch?
    res.merge!(path_hash)
    if res.respond_to? :with_indifferent_access
      res.with_indifferent_access
    else
      res
    end
  end
end
path() click to toggle source
# File lib/erb_component/req_tools.rb, line 2
def path
  @req.path
end
path_hash() click to toggle source
# File lib/erb_component/req_tools.rb, line 6
def path_hash
  @path_hash ||= begin
    split = path.split('/')
    split.shift

    res = {}
    split.size.times do |i|
      if split[i].to_i.to_s == split[i]
        res[split[i - 1].singularize + "_id"] = split[i]
      end
    end
    if res.respond_to? :with_indifferent_access
      res.with_indifferent_access
    else
      res
    end
  end
end