如何在葡萄树林中将camelcase改为snakecase

时间:2016-02-16 15:42:48

标签: ruby grape

在我的Grape端点中,我有一个名为user_type的必需参数。另一方面,我的公共API将此参数声明为userType,即驼峰案例。有没有办法改变这个?

 module MyAPI
      module V1
        class SignUp < Grape::API
          desc 'Create new user account'
          params do
           requires :email, type: String
           requires :password, type: String
           requires :user_type, type: String
          end
          post :sign_up do
            {}
          end
        end
      end
    end

1 个答案:

答案 0 :(得分:0)

这有点难看,但它会做到这一点,所以你可以把它弹出一个方法。

def sym_to_lower_camel(sym)
  sym.to_s.split('_').map{|x| x.capitalize}.join.sub(/^./, sym.to_s[0].downcase)
end
相关问题