发新话题
打印

我的第一关rake文件

我的第一关rake文件

早就想找个机会写写rake文件,但是接触到的项目都不怎么需要,所以一直也没有去学习一下。这两天一个项目需要把客户给的很多excel文件导入到数据中(我用的好似MySQL),到网上找了一些工具,感觉都不好,因为客户的excel格式比较乱(特别是日期的格式,竟然是中文数字和阿拉伯数字都有的...),所以就想干脆仔细写一个算了。这是正好想到可以顺便学习一下rake,于是就动手啦。

网上关于rake的文章有不少。如果想快速上手,那么Rails Envy的Ruby on Rails Rake Tutorial是绝佳的入门教程;如果想从更高的角度理解rake,那么当然要看Martin Fowler大大的Using the Rake Build Language了。

好了,看完了入门教程,开始动手!先去看看rails项目根目录下的Rakefile这个文件,里面就是require了一大堆东西,没什么意思,有用的反而是文件开头的注释:


ruby 代码
复制内容到剪贴板
代码:
# Add your own tasks in files placed in lib/tasks ending in .rake,   
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.  
明白了吧,把rake文件写成.rake扩展名的,然后放到lib/tasks下面,rails就可以自动加载你的rake文件了。于是到lib/tasks下面创建我的文件data.rake:

ruby 代码
复制内容到剪贴板
代码:
require "lib/data_importer.rb"  
  
namespace :eva do  
  desc 'Import init data from csv files.'   
  task :import_data => :environment do  
    DataImporter.import_teachers   
  end  
end  
以上代码非常容易理解,这也多亏了ruby强大的DSL能力。我就是创建了一个名为import_data的任务,然后在这个任务里面调用我自己写的DataImporter中的方法把excel中的数据导入到数据库中。

namespace用来为任务创建一个名字空间,这样我写的import_data任务就是eva:import_data,别人也可以在其它名字空间下创建相同名字的任务。rake db:migrate中的db就是这么个名字空间。

而:import_data => :environment这里声明了任务间的依赖关系,也就是import_data依赖于enviroment。那么enviroment是什么呢?执行一下rake --tasks查看所有的rake任务,没有叫这个名字的任务啊。怎么回事儿呢?其实你可以在RUBY_ROOT/lib/ruby/gems/1.8/gems/rails-<version>/lib/tasks下面找到一个rails自带的所有rake文件,打开其中的misc.rake,内容如下:


ruby 代码
复制内容到剪贴板
代码:
task :default => :test  
task :environment do  
  require(File.join(RAILS_ROOT, 'config', 'environment'))   
end  
那么environment任务实际上是把当前rails项目的整个环境加载进来,这样就可以使用model啊、ActionSupport啊之类的东西了。

那么为什么这个任务在rake --tasks中没有显示呢?那是因为这个任务没有响应的描述(可以理解为注释)。看看misc.rake和我的rake文件的区别就能发现,我的rake文件在任务前面有一行desc语句,它用来描述任务是做什么的,只有加了描述的任务才会在rake --tasks产生的列表中出现。

有人可能会问,这不是很让人迷惑吗?可以显示这些任务,同时描述部分显示为空就好了啊。其实不然,从逻辑上看,不加desc的task应该是程序内部使用的,不应该被其它人使用,例如environment这个task,用来加载rails环境,如果显示在rake --tasks中,万一被别人滥用了,就不好了,所以最好不要显示,干脆让别人不知道有这个东东。这样一来,意在让别人使用的task可以加desc,不想让别人使用的task不加desc(可以加单纯的注释),岂不是两全其美?
这世界总有你不明白

TOP

TOP

腭式破碎机 颚式破碎机

机制砂生产线、碎石设备生产线、机制砂生产线、碎石机、破碎机、第三代制沙机

TOP

支持

喜欢支持一下,喜欢支持一下,喜欢支持一下,喜欢支持一下,喜欢支持一下,喜欢支持一下,

TOP

选矿生产线工艺图

选矿生产线工艺图 联系方式:郭经理 13838506408
河南天宇重型机械有限公司专业生产颚式破碎机、球磨机.手机13838506408

TOP

omg it is so cheap

hi,friend,do you want the cheap gold of the game you play?now we give you a choose.you can see follow link:
<a href=http://www.fcsgame.com/>cheap wow gold</a>
<a href=http://www.fast-wowgold.com/Final-Fantasy-Gil.htm/>cheap Final Fantasy XI Gil</a>
<a href=http://www.fast-wowgold.com/2moons-dil.htm>buy cheap 2moons dil</a>
if you play this game youc an see it ^^ of course if you dont play it we have more :
<a href=http://www.fast-wowgold.com/buy-lotro-gold.htm>buy cheap lotro gold</a>
<a href=http://www.fast-wowgold.com/cabal-gold.htm>buy cheap cabal online alz</a>
oh so many cheap game .you can choose which you play,if it is not you play,you can in my website .to see you game.thank you

TOP

信用卡代办QQ474883319是个骗子,大家小心

淳美 09:02:06
你好 可以帮忙办卡吗

信用卡代办 09:02:26
可以
信用卡代办 09:02:28
你是那里的
淳美 09:03:07
湖南

信用卡代办 09:03:29
想办那个行的办多少额度呢
淳美 09:03:39
5000的好办吗  

信用卡代办 09:04:01
没有问题
信用卡代办 09:04:10
三千到五万的都可以办理
淳美 09:05:00
那多久可以下卡啊  

淳美 09:05:08
你的代理费用要多少啊  

信用卡代办 09:05:08
10到15天
信用卡代办 09:05:15
一万额度以下的费用是200
淳美 09:05:20
没那么快吧  

信用卡代办 09:05:34
我是直接把资料传到信用卡中心的朋友手上的
信用卡代办 09:05:39
然后加急办理
淳美 09:05:47


信用卡代办 09:05:53
如果你自己申请寄来寄去的就用时间
淳美 09:05:57
如果10-15天还不下卡 怎么办  

信用卡代办 09:06:03
这是什么话
信用卡代办 09:06:16
我说了十到十五天。
淳美 09:06:30
我问了 都是说20-30天的啊  

信用卡代办 09:06:39
那你找他们办好吗
淳美 09:07:29
我只是问问啥  

淳美 09:07:45
如果卡办不下来怎么办?

信用卡代办 09:08:31
我说了我是保证下卡的,我出证明然后找信用卡中心的朋友办理。我给他钱他帮我批卡。我们合作三年了都没有问题到你这就办不下来了?

淳美 09:08:58
哦 那如何付款给你

信用卡代办 09:09:09
费用是先收一部分
淳美 09:09:30
哦 你的卡号呢 等会我网上转给你  

信用卡代办 09:09:38
你要先给我资料的
信用卡代办 09:09:44
我现在收你的钱做什么
信用卡代办 09:09:55
你先把资料传给我
淳美 09:10:01
稍等  

淳美 09:10:07
要我提供哪些资料?

信用卡代办 09:10:19
你的身份证复印件,电话,地址,联系人这些基本的你提供
信用卡代办 09:10:23
财力证明我帮你出
淳美 09:10:41
哦 稍等  

信用卡代办 09:14:11
文件“身份证.rar”(0.1KB)已经发送完毕。
淳美 09:17:33
QQ9535***是我们老板
还给你3天时间 如果办卡还没任何消息
会让你的办卡信息 满天下
真不知道你们这种人 怎么想的 那么都正路不走 走这样的路 为了100块 失了尊严

TOP

新加的空白文章14

I come from a family of teachers.<a href=http://www.fast-wowgold.com/knight-Gold.htm>knight powerleveling</a>
My father was a teacher, my sister is a teacher, and I am a teacher. Why do we have strong passion for this profession?<a href=http://www.fast-wowgold.com/knight-Gold.htm>knight online power leveling</a>
For one thing, I consider myself not only a teacher, but also a learner. Being a teacher allows me, in fact forces me, to be a learner. First, <a href=http://www.fast-wowgold.com/knight-Gold.htm>knight online powerleveling</a>
I must find ways of helping my students to understand their assignments.Thirdly, I must then try to understand and respond in a helpful way to my students' ideas. Fourthly, I must force myself to be creative, because if I deliver a boring lesson,<a href=http://www.fast-wowgold.com/cabal-gold.htm>cabal alz</a>
students' ideas. Fourthly, I must force myself to be creative, because if I deliver a boring lesson,
my students will feel bored. And to be creative, I must learn more.<a href=http://www.fast-wowgold.com/cabal-gold.htm>buy cabal alz</a>Moreover, a teacher's job is not (and does not need to be) a routine.

TOP

新加的空白文章2

Good morning everyone,i am glad see you here again,now we are waiting you as usualy^^
wow gold
Final Fantasy XI Gil
as we know the game is the best way in my free time,
guild wars gold
cheap 2moons dil
sell dofus kamas
now the game here is the best chose,and we do it for my best for you,if you have anyquestion you

TOP

真的很好,我顶你兄弟

流动推车式电动大拉丝棉花糖机

08年最新款式,改变了传统大拉丝机器的笨重机身,更加小巧,方便携带、适合流动作业,却具有同等拉丝效果!高盆设计免除繁琐防风罩,操作过程可加糖并可无限拉长丝!最新离心偏轴物理技术设计,免除皮带或齿轮传动,目前最先进的独特传动方式,全国独有!
注意:本系列机机头和传动设计,仿制品没有防甩头设计,并无传动轴设计,更无弹簧防偏头设计!

  
优秀特点:
1.内含大容量电瓶设计,带充电器并自动充电功能,充电一夜可连续工作1-2天,机内含液化气钢瓶;
2.可调节机头转数,独特数字转数记忆,,最新安全机头设计,离心偏轴出糖器,防止出现偏头甩糖不均匀;无皮带式传动,绝不飞头!
3.节能斜口设计机头,不仅节省液化气而且不造成糖的浪费,,拉丝绝不断丝设计,可以在拉丝过程中不断加糖,直至做到最大的棉花糖;
4.电子打火设计,火力调节及火力记忆设计;
5.,可以在任何地点作业,行李外观设计,折叠把手,流动作业时可以随时回避城市管理人员!
规格:950X320X320mm
转动系统:照明电或内置电瓶  内置燃气加热系统 充电器 变压器 调速装置
原料:白砂糖 绵白糖以及冰糖(采用冰糖为原料是独特设计可制造独特蓬松效果)
彩色多口味棉花糖制作工艺随机赠送,色彩、口味不再单一!
单价:1720元(含独特精致2公斤液化气罐和12V电瓶及充电器装置,开机即可使用)    量大优惠
欢迎您的来电:024-82116562 QQ:491753951 http://www.086hh.com
沈阳宏海

TOP

cabal online

Direction:For this part,you are allowed 30 minutes to write a composition in three paragraphs.You are given the first sentence of the three paragrapgs.Your part of the composition should be no less than 100 words,not including the words given.Remember to write clearly.<a href=http://www.fast-wowgold.com/cabal-gold.htm>buy cheap cabal online alz</a>
cience is important to most people living in the modern world for a number of reasons.Science is important to world peace, to the understanding<a href=http://www.fast-wowgold.com/cabal-gold.htm>cabal online power leveling</a>
of technology,and to our understanding of the natural world. Science is also an interesting area of study for a number of reasons. Some areas of science are espacially interesting because they deal with questions all men<a href=http://www.fast-wowgold.com/cabal-gold.htm>cabal online powerleveling</a>
nd women ask themselves.Science tries to procide some explanations for what happens to people. <a href=http://www.fast-wowgold.com/cabal-gold.htm>cabal power leveling</a>
Science is hard to study.One of the reason is that there are so many questions without answers.M<a href=http://www.fast-wowgold.com/cabal-gold.htm>cabal powerleveling</a>Much of sciece involves asking questions, and then studying and doing research to try to find the answers.Some questions have been studied by research scientists for many years,and the answers are still not known.However,scientists are slowly but surely able to answer more and more questions about life,and these answers change our lives and also explain them.

TOP

如此激的动人心的时刻,首页占个位置不容易啊

楼主不错的,大家支持你!


不得不佩服  太棒了哦






办公室装潢设计-你的为你的办公室做装潢设计嘉选
室内设计装潢-室内设计装修效果图欣赏
SW-8A/12A/24A型耐洗色牢度试验机-SW-8A/12A/24A型耐洗色牢度试验机
布样裁切机-专业生产布样裁切机

TOP

红五月到底会有多红

在利多政策家庭装修的推动下,4月下旬起沪深办公楼装修股市终于止住了持续下跌的乐华空调维修行情,开始厂房装修较有力度的反弹。

TOP

顶楼主

送你一个美女,顶你。



个性签名:
------------------------------------------------------------------------------------------------------------------------------
aoc power leveling,aoc powerleveling,AGE OF CONAN PowerLeveling,
AGE OF CONAN Power Leveling,world of warcraft power leveling

TOP

发新话题