Sha256: d48d2f8a214d2668d66e7106a37feddcc0bb33d4ce866ff47f2175788e5e924f

Contents?: true

Size: 941 Bytes

Versions: 1

Compression:

Stored size: 941 Bytes

Contents

module ReqTools
  def path
    @req.path
  end

  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
      res.with_indifferent_access
    end
  end

  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)
      res.with_indifferent_access
    end
  end

  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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
erb_component-0.1.9 lib/erb_component/req_tools.rb