mysql,Linux,HighPerformance,ruby on Rails

2009年10月30日星期五

今天发现竟然可以通过邮件来发布blog,这样又可以不翻墙发文了。


yeah!万圣节快乐。
今天找了一个学respec的好例子,直接在github上下载即可练习spec.
http://github.com/ultrasaurus/test-first-teaching/
我是直接clone下来,然后练习的。这主竟然还提供了solution,如果在本地使用呢?需要使用git的命令。
pull,branch啊,熟吗?不熟,这主又提供了一份git参考mv:
http://www.gitcasts.com/posts/railsconf-git-talk
这视频应该是刚刚的,当然要想学好,需要点耐性。最好记下笔记。可没有快进的。

--
tommy xiao
E-mail: xiaods(AT)gmail.com

2009年10月29日星期四

在rails中attr_accessible是Mass Assignment exploit的救星

查railscast一则教程,写到使用curl可以方便模拟一个包来hack

curl -X PUT可以设置HTTP 命令

curl -d可以POST数据

attr_protected有如鸡肋,在has many下竟然可以级联update其它属性.直接使用attr_accessible就可以指定赋值对象了.

所以在model中要使用attr_accessible,限制更新字段.

2009年10月28日星期三

君子欲行先于言,故迟言而速行也--作有生产力的人



早上翻了翻twitter,看到这篇文章,深受启发,看来要改改自已的毛病了.
Produce before you Consume

文中作者说到,一早起来就应该着手作自已应该作的事情,然后再浏览咨询,这样才能有效的提高生产力.精彩的tips

2009年10月26日星期一

TaobaoTOP调用实战之Json操作总结,ruby

首先需要调用这个plugin,http://github.com/xiaods/open-taobao
第一步当然想显示taobao的商品分类,就是说需要调用taobao.shopcats.list.get:获取前台展示的店铺类目
我使用json返回结果集,并且想在页面里显示出来,但json在parse之后如何能iterate显示每一条数据呢?
在网上查了查,写出如下测试,成功。
# taobao.shopcats.list.get:获取前台展示的店铺类目
def test_taobao_shopcats_list_get
result = OpenTaobao.get_with(:method => 'taobao.shopcats.list.get') #结果集是json包

shop_cats = result['rsp']['shop_cats']
# puts shop_cats.inspect
shop_cats.each do |cat|
puts cat["parent_cid"]+" "+cat["name"]+" "+cat["is_parent"].to_s+" "+cat["cid"]
end
end

就像Array一样操作数据。真是挺方便。其实因为json是需要与前台互动的,应该是使用javascript来操作json更为理想。当然这里就不表了。以上就是使用ruby操作json数据集。

参考这里:
http://snippets.dzone.com/posts/show/6133

2009年10月22日星期四

学习Behavior Driven Development 的来龙去脉

是的,我不会BDD,因为要解决自已的测试需求,所以我找到了rspec,所以我遇到了BDD.
什么是BDD,有现成的不用介绍.我只说一下为什么要BDD.这起源于我的一个场景下的需求
如果我使用rails,好像只用得着unit test,并没有什么必要需求使用BDD.但最近继续写这个Plugin

http://github.com/xiaods/open-taobao
就是调用open.taobao.com 插件,更方便的调用taobao open api.现在这个插件还只有一个架子,也没有测试,所以我想通过TDD来驱动开发taobao TOP的API,API支付GET/POST,所以使用unit test行不通.所以需要一种更自然的方式来作.哪种呢,经过Digg之后,我认为就是BDD.BDD需要和TDD结合才算完美.其实本质上来说,就是换个方法来TDD,所以本无区分.只需要会用就用.那现在就要问了:如何踏出第一步呢:
* Simple Ruby and RSpec Tutorial – This tutorial will bring you through the basics of the process while developing a simple state machine. It’s a fun tutorial.
* RSpec on Rails – Luke Redpath’s awesome introduction to using RSpec with your Rails application development.
* RSpec Mocks and Models – Peepcode screencast explaining Mocks and Models. You can view a small sample of the screencast, but I suggest forking out the $9. It’s definitely worth it. This especially helped me out as I’ve been writing a Ruby-based library for accessing the FamilySearch Public API.

其中,Simple Ruby and RSpec Tutorial我是亲自看了一遍,讲的非常清楚,基本看完就可以建立基本的概念观.也欢迎有高人与我分享.可以通过Twitter来告诉我.我的帐号是xds2000

参考:
http://jimmyzimmerman.com/blog/2007/11/simple-tutorials-for-learning-bdd-and-rspec.html

github上学来的git merge技巧.

场景,我fork了一个项目,然后自已写了一些test,然后就放在github里了没动过.最近不忙了,想重新拿起来,继续把Taobao TOP的api rails plugin继续写,所以希望先从fork源那原来的那哥们的代码更新一下,找来找去,找到这最有用的:
git clone git@github.com:bricoleurs/bricolage.git
cd bricolage
git remote add merger git://github.com/username/bricolage.git
git pull merger master
git push origin master
git remote rm merger

就是这个有用的,不用介绍,你也会merge别人的代码了.呵呵.加油,努力.
参考:
http://wiki.github.com/bricoleurs/bricolage/merging-with-git

一直使用twitter,blog更新就很懒了.

2009年10月21日星期三

看JavaScript: The Good Parts的一些碎念,写下来

JavaScript: The Good Parts,是我这几天读的书。主要是梳理一下对javascript的理解。当然之前完全没有系统的学习javascript的语法,在web开发中会感觉不能准确把握需要的效果。现在好了,专心看了几章,对它建立了基本的认识。所以我想应该在blog中提一下,有心人如果想学javascript,应该从这本读起,比较来的直接,有中文版的。
另外,其书中建立的基础好像和Jquery一脉相承,有人知道来龙去脉吗?希望有人相告。

2009年10月14日星期三

关于CustomValidates的使用demo,及一个ruby中的The splat operator.

CustomValidates,源自看advanced rails recipes里的recipe 10:
Write Your Own Custom Validations .
书中使用的rails版本已经很老,所以在rails 2.3下使用书中讲的过不了。
参考这里:
http://chrisblunt.com/blog/2009/04/18/rails-writing-dry-custom-validators/
把validates放在config/initializers里,不需要在environment.rb里加require就OK了。
直接按书中写test unit,直接通过。:-)
然后涉及到给class里加mothod的方法,看这里
http://blog.codevader.com/2008/09/27/including-and-extending-modules-in-ruby/
讲了一遍,我看了遍,记了一下。
然后又涉及到一个 *号,splat operator,
看这里
http://theplana.wordpress.com/2007/03/03/ruby-idioms-the-splat-operator/
注意一下comments里的一个例子:
It may also help to note that ’splat’ returns a copy of the instance that it’s used with, whereas not using ’splat’ returns a reference to the instance.
说的就是加*的是返回一个实例的copy,不加*的是返回一个实例的reference,下面代码一目了然。

Ex.\
[
>group = ["blue", "green", "pink"]
>painting = ["paintbrush", "canvas", group]
>group[2] = “purple”
>puts painting

gives us: paintbrush, canvas, blue, green, purple
]

[
>group = ["blue", "green", "pink"]
>painting = ["paintbrush", "canvas", *group]
>group[2] = “purple”
>puts painting

gives us: paintbrush, canvas, blue, green, pink
]

2009年10月9日星期五

route里的:member和:collection是怎么用的

以下内容基于rails,route里经常会用到collection和member,但已经忘了它是何物了,可见不练的后果。查之,

这里的对话可以直接让我明白,相信你也会明白.
总结下来,想在RESTful的7种action之外再增加其它action,就需要使用:collection,如果想针对单个object作操作,请使用:member.

RubyHunt: What’s the use of the :collection and :member methods within a route? What’s the difference between the two?
melvinram: Collection adds routes to the entire collection
RubyHunt: So the collection is used to add another action inside the controller
melvinram: If you had a resource of Contacts, a collection route you might add would be delete_all_older_than_6_months
melvinram: and the route to get to it would be be /contacts/delete_all_older_than_6_months/
RubyHunt: Which otherwise would be restricted to only 7 actions ?
melvinram: yes
melvinram: A member method applies only to one record. For example, you might add a member method called upgrade_to_vip that would update one record to have a status of VIP or something…
melvinram: And the route would be /contacts/1/upgrade_to_vip
RubyHunt: So the :collection is used to add action inside a RESTful controller and the :member would be used to apply action for individual object.