jbuilder的set!方法重构接口
来源:互联网 发布:淘宝上买处方药打电话 编辑:程序博客网 时间:2024/05/02 20:46
https://github.com/rails/jbuilder 的set!方法重构接口,
因为grape没法使用 jBuilder 的缓存,所以直接用 Rails 写 API
(1)多个图片
images: { poster: { url: "http://r4.ykimg.com/0510000057CD17166714C04FDC0628CE", md5: "ee63b0a11cb1e39331d5ce11d38fceec"}, right_top_corner: { url: "http://r1.ykimg.com/0510000057CEA19C6714C063660BEB44", md5: "e50d520de494538d4633086d46845a81" }},
原来的方法为
json.images do component.pictures.each do |p| if p.name == 'poster' json.poster do json.url "http://r#{rand(4) + 1}.xxx.com/#{p.url}" json.md5 p.md5 end end if p.name == 'icon' json.icon do json.url "http://r#{rand(4) + 1}.xxx.com/#{p.url}" json.md5 p.md5 end end if p.name == 'background' json.background do json.url "http://r#{rand(4) + 1}.xxx.com/#{p.url}" json.md5 p.md5 end end end end
精简后为
json.images do component.pictures.each do |p| json.set! p.name do json.set! :url, "http://r#{rand(4) + 1}.ykimg.com/#{p.url}" json.set! :md5, p.md5 end end end
(2)hints 是一个 hash,遍历 Hash 的文档 http://ruby-doc.org/core-2.3.1/Hash.html#method-i-each
hint: { left: "left", right: "111"},
精简前
json.hint do if component.hints.key?("left") json.left component.hints["left"] end if component.hints.key?("center") json.center component.hints["center"] end if component.hints.key?("right") json.right component.hints["right"] end end
精简后
json.hint do component.hints.each do |key,value| json.set! key, value end end
0 0
- jbuilder的set!方法重构接口
- JBuilder 重构工具
- 重构Oc的get,set方法
- jbuilder的json.partial!方法
- iOS @property 重构set、get方法
- Set接口的iterator方法源码探究
- Set接口的add,addAll,retainAll方法
- Set 接口没有自己特有的方法
- 重构案例积累系列:get set方法重构
- 用JBuilder 2005实现重构之认识重构
- 使用Jbuilder重构功能阅读混淆过的Java代码。
- Jbuilder 中 Tomcat 数据源的配置方法
- JBuilder + Tomcat 数据库连接池的配置方法
- JBuilder + Tomcat 数据库连接池的配置方法
- 用JBuilder开发web service的方法
- 重构set redis
- Set的子类接口特有方法和使用方法
- JAVA--Set接口的两种遍历方法
- ActiveReports中如何使用Excel数据源
- leetcode之比较两颗二叉树是否完全一样
- Android Studio Gradle Error - Protocol Family Unavailable
- Power按键流程分析
- C# 解压缩.zip文件
- jbuilder的set!方法重构接口
- ThinkPHP PHPExcel基本使用(一)
- ionic2 使用slides制作滑动效果的类型选择栏
- 直播协议的选择:RTMP vs. HLS
- Java、 JS中split分割字符串
- 最快让你上手ReactiveCocoa之基础篇
- 多个Activity之间带数据跳转
- ROLAP MOLAP HOLAP
- HSV颜色模型